R Markdown: the Definitive Guide Yihui Xie, J.J
Total Page:16
File Type:pdf, Size:1020Kb
R Markdown The Definitive Guide Chapman & Hall/CRC The R Series Series Editors John M. Chambers, Department of Statistics Stanford University Stanford, California, USA Torsten Hothorn, Division of Biostatistics University of Zurich Switzerland Duncan Temple Lang, Department of Statistics University of California, Davis, California, USA Hadley Wickham, RStudio, Boston, Massachusetts, USA Recently Published Titles bookdown: Authoring Books and Technical Documents with R Markdown Yihui Xie Testing R Code Richard Cotton R Primer, Second Edition Claus Thorn Ekstrøm Flexible Regression and Smoothing: Using GAMLSS in R Mikis D. Stasinopoulos, Robert A. Rigby, Gillian Z. Heller, Vlasios Voudouris, Fernanda De Bastiani The Essentials of Data Science: Knowledge Discovery Using R Graham J. Williams blogdown: Creating Websites with R Markdown Yihui Xie, Alison Presmanes Hill, Amber Thomas Handbook of Educational Measurement and Psychometrics Using R Christopher D. Desjardins, Okan Bulut Displaying Time Series, Spatial, and Space-Time Data with R, Second Edition Oscar Perpinan Lamigueiro Reproducible Finance with R Jonathan K. Regenstein, Jr R Markdown: The Definitive Guide Yihui Xie, J.J. Allaire, Garrett Grolemund For more information about this series, please visit: https://www.crcpress.com/go/the-r-series R Markdown The Definitive Guide Yihui Xie J. J. Allaire Garrett Grolemund CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 © 2019 by Taylor & Francis Group, LLC CRC Press is an imprint of Taylor & Francis Group, an Informa business No claim to original U.S. Government works Printed on acid-free paper Version Date: 20180702 International Standard Book Number-13: 978-1-138-35933-8 (Paperback) International Standard Book Number-13: 978-1-138-35942-0 (Hardback) This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been granted a photocopy license by the CCC, a separate system of payment has been arranged. Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identification and explanation without intent to infringe. Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com To Jung Jae-sung (1982 – 2018), a remarkably hard-working badminton player with a remarkably simple playing style Contents List of Tables xvii List of Figures xix Preface xxiii About the Authors xxxiii I Get Started 1 1 Installation 3 2 Basics 5 2.1 Example applications . 9 2.1.1 Airbnb’s knowledge repository . 9 2.1.2 Homework assignments on RPubs . 10 2.1.3 Personalized mail . 10 2.1.4 2017 Employer Health Benefits Survey . 11 2.1.5 Journal articles . 12 2.1.6 Dashboards at eelloo . 12 2.1.7 Books . 12 2.1.8 Websites . 13 2.2 Compile an R Markdown document . 14 2.3 Cheat sheets . 16 2.4 Output formats . 16 2.5 Markdown syntax . 19 2.5.1 Inline formatting . 19 2.5.2 Block-level elements . 20 2.5.3 Math expressions . 23 2.6 R code chunks and inline R code . 24 2.6.1 Figures . 27 2.6.2 Tables . 29 2.7 Other language engines . 30 vii viii Contents 2.7.1 Python ......................... 32 2.7.2 Shell scripts . 34 2.7.3 SQL . 35 2.7.4 Rcpp . 37 2.7.5 Stan . 39 2.7.6 JavaScript and CSS . 39 2.7.7 Julia . 40 2.7.8 C and Fortran . 40 2.8 Interactive documents . 41 2.8.1 HTML widgets . 41 2.8.2 Shiny documents . 42 II Output Formats 47 3 Documents 49 3.1 HTML document . 49 3.1.1 Table of contents . 50 3.1.2 Section numbering . 51 3.1.3 Tabbed sections . 51 3.1.4 Appearance and style . 52 3.1.5 Figure options . 54 3.1.6 Data frame printing . 55 3.1.7 Code folding . 56 3.1.8 MathJax equations . 57 3.1.9 Document dependencies . 58 3.1.10 Advanced customization . 59 3.1.11 Shared options . 62 3.1.12 HTML fragments . 62 3.2 Notebook . 63 3.2.1 Using Notebooks . 63 3.2.2 Saving and sharing . 72 3.2.3 Notebook format . 73 3.3 PDF document . 78 3.3.1 Table of contents . 79 3.3.2 Figure options . 80 3.3.3 Data frame printing . 80 3.3.4 Syntax highlighting . 81 3.3.5 LaTeX options . 81 3.3.6 LaTeX packages for citations . 82 3.3.7 Advanced customization . 83 Contents ix 3.3.8 Other features . 84 3.4 Word document . 85 3.4.1 Other features . 85 3.5 OpenDocument Text document . 86 3.5.1 Other features . 86 3.6 Rich Text Format document . 87 3.6.1 Other features . 87 3.7 Markdown document . 88 3.7.1 Markdown variants . 88 3.7.2 Other features . 89 3.8 R package vignette . 90 4 Presentations 93 4.1 ioslides presentation . 93 4.1.1 Display modes . 95 4.1.2 Incremental bullets . 95 4.1.3 Visual appearance . 96 4.1.4 Code highlighting . 98 4.1.5 Adding a logo . 98 4.1.6 Tables . 99 4.1.7 Advanced layout . 100 4.1.8 Text color . 100 4.1.9 Presenter mode . 101 4.1.10 Printing and PDF output . 101 4.1.11 Custom templates . 101 4.1.12 Other features . 102 4.2 Slidy presentation . 102 4.2.1 Display modes . 104 4.2.2 Text size . 104 4.2.3 Footer elements . 105 4.2.4 Other features . 105 4.3 Beamer presentation . 106 4.3.1 Themes . 107 4.3.2 Slide level . 108 4.3.3 Other features . 109 4.4 PowerPoint presentation . 109 4.4.1 Custom templates . 112 4.4.2 Other features . 113 III Extensions 115 x Contents 5 Dashboards 117 5.1 Layout . 118 5.1.1 Row-based layouts . 120 5.1.2 Attributes on sections . 120 5.1.3 Multiple pages . 121 5.1.4 Story boards . 123 5.2 Components . 124 5.2.1 Value boxes . 125 5.2.2 Gauges . 126 5.2.3 Text annotations . 128 5.2.4 Navigation bar . 130 5.3 Shiny . 131 5.3.1 Getting started . 132 5.3.2 A Shiny dashboard example . 132 5.3.3 Input sidebar . 135 5.3.4 Learning more . 135 6 Tufte Handouts 137 6.1 Headings . 138 6.2 Figures . 139 6.2.1 Margin figures . 139 6.2.2 Arbitrary margin content . 139 6.2.3 Full-width figures . 140 6.2.4 Main column figures . 141 6.3 Sidenotes . 141 6.4 References . 142 6.5 Tables . 143 6.6 Block quotes . 143 6.7 Responsiveness . 144 6.8 Sans-serif fonts and epigraphs . 145 6.9 Customize CSS styles . 145 7 xaringan Presentations 147 7.1 Get started . 148 7.2 Keyboard shortcuts . ..