Original author(s) | Yihui Xie |
---|---|
Initial release | 17 January 2012 |
Stable release | |
Written in | R |
Type | Cross-platform |
License | GNU GPL |
Website | yihui.org/knitr/ |
R Markdown documents are fully reproducible (they can be automatically regenerated whenever underlying R code or data changes). This document describes R Markdown v2 based on knitr and pandoc, the workhorse that converts markdown to html and many other formats. Internally, the Pandoc output format of the current R Markdown document is stored in knitr::optsknit$get('rmarkdown.pandoc.to'), and the Pandoc input.
knitr is an engine for dynamic report generation with R.[1][2] It is a package in the programming language R that enables integration of R code into LaTeX, LyX, HTML, Markdown, AsciiDoc, and reStructuredText documents. The purpose of knitr is to allow reproducible research in R through the means of Literate Programming. It is licensed under the GNU General Public License.[3]
knitr was inspired by Sweave and written with a different design for better modularization, so it is easier to maintain and extend. Sweave can be regarded as a subset of knitr in the sense that all features of Sweave are also available in knitr. Some of knitr's extensions include the R Markdown format[4] (used in reports published on RPubs[5]), caching, TikZ graphics and support to other languages such as Python, Perl, C++, Shell scripts and CoffeeScript, and so on.
knitr is officially supported in the RStudioIDE for R, LyX, Emacs/ESS and the ArchitectIDE for data science.
Workflow of knitr[edit]
Thus, while R, markdown, R Markdown, and knitr were developed separately by independent groups of people, RStudio now quite deliberately maintains the.
R Markdown Knitr Options
Knitr consists of standard e.g. Markdown document with R-code chunks integrated in the document. The code chunks can be regarded as R-scripts that
- load data,
- performs data processing and
- creates output data (e.g. descriptive analysis) or output graphics (e.g. boxplot diagram).
The implementation of logical conditions in R can provide text elements for the dynamic report depended on the statistical analysis. For example:
The text fragments are selected according to the script's results. In this example, if the P-value was lower than the significance level, different text fragments would be inserted in the dynamic report. In particular, the second sentence would swap 'less' for 'greater,' and the third sentence would be replaced to reflect rejection of the null hypothesis. Using this workflow allows creating new reports simply by supplying new input data, ensuring the methodology is reproduced identically.
See also[edit]
References[edit]
R Markdown Knitr To Pdf Error
- ^Xie, Yihui (2015). Dynamic Documents with R and knitr, 2nd Edition. Chapman & Hall/CRC. ISBN9781498716963.
- ^Xie, Yihui. 'knitr: A General-Purpose Tool for Dynamic Report Generation in R'(PDF).
- ^https://cran.r-project.org/package=knitr
- ^RStudio, Inc. 'R Markdown — Dynamic Documents for R'.
- ^RStudio, Inc. 'Easy web publishing from R'.
External links[edit]
- Repository on GitHub
- Example code on GitHub
- knitr package on CRAN
Example
R-markdown code chunks
R-markdown is a markdown file with embedded blocks of R code called chunks. There are two types of R code chunks: inline and block.
Inline chunks are added using the following syntax:
R Markdown Knitr Pdf
They are evaluated and inserted their output answer in place.
Block chunks have a different syntax:
And they come with several possible options. Here are the main ones (but there are many others):
- echo (boolean) controls wether the code inside chunk will be included in the document
- include (boolean) controls wether the output should be included in the document
- fig.width (numeric) sets the width of the output figures
- fig.height (numeric) sets the height of the output figures
- fig.cap (character) sets the figure captions
They are written in a simple tag=value
format like in the example above.
R-markdown document example
Below is a basic example of R-markdown file illustrating the way R code chunks are embedded inside r-markdown.
Converting R-markdown to other formats
The R knitr
package can be used to evaluate R chunks inside R-markdown file and turn it into a regular markdown file.
The following steps are needed in order to turn R-markdown file into pdf/html:
- Convert R-markdown file to markdown file using
knitr
. - Convert the obtained markdown file to pdf/html using specialized tools like pandoc.
In addition to the above knitr
package has wrapper functions knit2html()
and knit2pdf()
that can be used to produce the final document without the intermediate step of manually converting it to the markdown format:
If the above example file was saved as income.Rmd
it can be converted to a pdf
file using the following R commands:
The final document will be similar to the one below.