We’ll use one of the most basic functions for creating tables, kable, which is from one of the most user-friendly packages for combining R code and output together, knitr. In this tutorial, I’ll cover examples for one common model (an analysis of variance, or ANOVA) and show you how you can get table and in-line output automatically. I’m writing this post in R Studio as an R-markdown document.Įven if you’ve never used markdown or R-markdown before, you can jump right in and start getting properly formatted output from R. I wrote my entire dissertation in R Studio, in fact, using sweave to integrate my R code with LaTeX typesetting. I almost never type out my results anymore I let R do it for me. These are called dynamic documents, and they’re awesome. Happily, R integrates beautifully with output documents, allowing you to ask the computer to fill in the numbers in your tables and text for you, so you never have to wake up in a cold sweat panicking about a typo in your correlation matrix. Through the years, I’ve learned that the only sure way to reduce human error is to give humans (including myself) as little opportunity to interfere in the process as possible. No matter how carefully I check my work, there’s always the nagging suspicion that I could have confused the contrasts for two different factors, or missed a decimal point or a negative sign.Īlthough I’m usually overreacting, I think my paranoia isn’t completely misplaced - little errors are much too easy to make, and they can have horrifying consequences. You can use percentages, e.g., out.I don’t know what fears keep you up at night, but for me it’s worrying that I might have copy-pasted the wrong values over from my output. Out.width and out.height: The output size of R plots in the output document. You can also specify the two options together in a single chunk option fig.dim, e.g., fig.dim = c(6, 4) means fig.width = 6 and fig.height = 4. R plots in code chunks are first recorded via a graphical device in knitr, and then written out to files. Caching can be handy but also tricky sometimes.įig.width and fig.height: The (graphical device) size of R plots in inches. However, I want to honestly remind you of the two hard problems in computer science (via Phil Karlton): naming things, and cache invalidation. If caching is enabled, the same code chunk will not be evaluated the next time the document is compiled (if the code chunk was not modified), which can save you time. When you are trying to set echo = FALSE, results = 'hide', warning = FALSE, and message = FALSE, chances are you simply mean a single option include = FALSE instead of suppressing different types of text output individually.Ĭache: Whether to enable caching. When include = FALSE, this whole code chunk is excluded in the output, but note that it will still be evaluated if eval = TRUE. Include: Whether to include anything from a code chunk in the output document. Similarly, when warning = FALSE or message = FALSE, these messages will be shown in the R console. Note that if you set error = FALSE, rmarkdown::render() will halt on error in a code chunk, and the error will be displayed in the R console. Warning, message, and error: Whether to show warnings, messages, and errors in the output document. The default collapse = FALSE means R expressions and their text output are separated into different blocks. This is mostly cosmetic: collapse = TRUE makes the output more compact, since the R source code and its text output are displayed in a single output block. By default, text output will be wrapped in verbatim elements (typically plain code blocks).Ĭollapse: Whether to merge text output and source code into a single code block in the output. Results: When set to 'hide', text output will be hidden when set to 'asis', text output is written “as-is,” e.g., you can write out raw Markdown text from R code (like cat('**Markdown** is cool.\n')). We list a subset of them below:Įcho: Whether to echo the source code in the output document (someone may not prefer reading your smart source code but only results). There are a large number of chunk options in knitr documented at.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |