Rmarkdown
Markdown language
According to wikipedia:
Markdown is a lightweight markup language, originally created by John Gruber with substantial contributions from Aaron Swartz, allowing people “to write using an easy-to-read, easy-to-write plain text format, then convert it to structurally valid XHTML (or HTML)”.
- Markup is a general term for content formatting - such as HTML - but markdown is a library that generates HTML markup.
- Nice summary from stackoverflow.com and more complete list from github.
- An example https://gist.github.com/jeromyanglim/2716336
- Convert mediawiki to markdown using online conversion tool from pandoc.
- R markdown file and use it in RStudio.
Github markdown Readme.md
- https://help.github.com/en/categories/writing-on-github
- https://guides.github.com/features/mastering-markdown/
- https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
- Online editor with preview https://jbt.github.io/markdown-editor/
- An example from pathwayHeatmap
How to nest code within a list using Markdown
https://meta.stackexchange.com/questions/3792/how-to-nest-code-within-a-list-using-markdown
Continuous publication
Open collaborative writing with Manubot Himmelstein et al 2019
Syntax
Comment: https://stackoverflow.com/questions/4823468/comments-in-markdown. The html method does not work. I need to try use Shift+ CMD + c.
Table
- https://www.tablesgenerator.com/markdown_tables
- An online tool to copy Table in Excel and Paste as a Markdown Table
Simple example
| Column 1 | Column 2 | Column 3 | | :------------- | :----------: | -----------: | | Cell Contents | More Stuff | And Again | | You Can Also | Put Pipes In | Like this \| |
Literate programming
- 文學程式設計 vs 結構化程式設計(Structured programming)
- Reproducibility and literate programming in R TJ McKinley
Rmarkdown
- R Markdown: The Definitive Guide
- R Markdown Cookbook
- R Markdown: Eight ways
- https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf
- https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf
- https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf
- Chunk options http://kbroman.org/knitr_knutshell/pages/Rmarkdown.html
HTML5 slides examples
- http://yihui.name/slides/knitr-slides.html
- http://yihui.name/slides/2012-knitr-RStudio.html
- http://yihui.name/slides/2011-r-dev-lessons.html#slide1
- http://inundata.org/R_talks/BARUG/#intro
Software requirement
- Rstudio
- knitr, XML, RCurl (See omegahat or this internal link for installation on Ubuntu)
- pandoc package This is a command line tool. I am testing it on Windows 7.
Slide #22 gives an instruction to create
- regular html file by using RStudio -> Knit HTML button
- HTML5 slides by using pandoc from command line.
Files:
- Rcmd source: 009-slides.Rmd Note that IE 8 was not supported by github. For IE 9, be sure to turn off "Compatibility View".
- markdown output: 009-slides.md
- HTML output: 009-slides.html
We can create Rcmd source in Rstudio by File -> New -> R Markdown.
There are 4 ways to produce slides with pandoc
- S5
- DZSlides
- Slidy
- Slideous
Use the markdown file (md) and convert it with pandoc
pandoc -s -S -i -t dzslides --mathjax html5_slides.md -o html5_slides.html
If we are comfortable with HTML and CSS code, open the html file (generated by pandoc) and modify the CSS style at will.
YAML
ymlthis package - write YAML for R Markdown, bookdown, blogdown, and more.
Some examples
--- title: "My Title" date: "`r format(Sys.time(), '%d %B, %Y')`" output: pdf_document: toc: true number_sections: true classoption: landscape ---
--- title: "My Title" date: "`r format(Sys.time(), '%d %B, %Y')`" output: latex_document ---
Debug by keep tex
Rmarkdown Retain .tex file. We can use this technique to see what goes wrong in the middle step.
output: pdf_document: keep_tex: true ---
HTML output
Useful YAML options for generating HTML reports in R
render(, params)
- Run RMarkdown with arguments on the command line
- Parameterized reports
- How to Automate PDF Reporting with R
Chunk options
- R code chunks and inline R code from R Markdown: The Definitive Guide.
- knitr page
- rpubs.com.
- Default knitr options and hooks
Some options:
- echo=FALSE. whether to include R source code in the output file
- message=FALSE. whether to preserve messages emitted by message() (similar to warning)
- results = 'hide'. hide results; this option only applies to normal R output (not warnings, messages or errors) like print() or cat().
- Note: if we spell the option incorrectly like result = 'hide' , the 'R markdown' window from running knitting will not show any warnings and the purpose of hiding the print result will not work either.
- include. whether to include the chunk output in the final output document;
- warning. whether to preserve warnings (produced by warning()) in the output like we run R code in a terminal (if FALSE, all warnings will be printed in the console instead of the output document).
- error. whether to preserve errors (from stop()); by default, the evaluation will not stop even in case of errors!! if we want R to stop on errors, we need to set this option to FALSE
- comment. Remove Hashes in R Output from R Markdown and Knitr
```{r setup, include=FALSE} knitr::opts_chunk$set(echo = FALSE, cache = TRUE, warning = FALSE, message = FALSE, verbose = FALSE) ```
Include all the code from the Rmd
Use knitr::all_labels().
Comment chunks not working
Comment out some chunks/part of Rmd file. Use 'eval = FALSE' works.
inline code
https://rmarkdown.rstudio.com/lesson-4.html
Language support (engine)
TinyTex for pdf output (works on Windows OS too)
https://github.com/yihui/tinytex and Debugging page for different solutions.
On NIH/biowulf, there is no 'pdflatex' program. So a pdf file cannot be generated.
I install tinytex. At the end, many latex executable files (pdflatex, bibtex, ...) are installed under ~/bin directory.
Step 1:
> install.packages("tinytex") trying URL 'https://yihui.name/gh/tinytex/tools/install-unx.sh' Content type 'text/plain; charset=utf-8' length 616 bytes ... tlmgr: package log updated: /spin1/home/linux/USERNAME/.TinyTeX/texmf-var/web2c/tlmgr.log TinyTeX installed to /spin1/home/linux/USERNAME/.TinyTeX You may have to restart your system after installing TinyTeX to make sure ~/bin appears in your PATH variable (https://github.com/yihui/tinytex/issues/16).
Step 2: I also need to run
> tinytex::install_tinytex() trying URL 'https://yihui.org/gh/tinytex/tools/install-unx.sh' ... tlmgr: package log updated: /home/brb/.TinyTeX/texmf-var/web2c/tlmgr.log TinyTeX installed to /home/brb/.TinyTeX You may have to restart your system after installing TinyTeX to make sure ~/bin appears in your PATH variable (https://github.com/yihui/tinytex/issues/16).
So we don't need to manually install Miktex on Windows.
On my Ubuntu 20.04, ~/.profile already adds $HOME/bin and $HOME/.local/bin to $PATH.
After changing the .profile file, you have to logout from your account and login, then it will be sourced once automatically.
On my Linux Mint, I got an error tlmgr: unexpected return value from verify_chcksum: -5 ! Latex Error: File multirow.sty not found when I run rmarkdown::render(). Missing package. "Remote repository newer than local", How do I upgrade TinyTeX yearly after I had installed it in the previous year? I got a message like “tlmgr: Remote repository is newer than local (2017 < 2018)“.
$ tlmgr --verify-repo=none install multirow # tlmgr: Local TeX Live (2019) is older than remote repository (2020). Cross release updates are only supported with update-tlmgr-latest(.sh/.exe) --update See https://tug.org/texlive/upgrade.html for details. R> tinytex::reinstall_tinytex() R> q() $ tlmgr --verify-repo=none install multirow
Built-in examples from rmarkdown
# This is done on my ODroid xu4 running Ubuntu Mate 15.10 (Wily) # I used sudo apt-get install pandoc in shell # and install.packages("rmarkdown") in R 3.2.3 library(rmarkdown) rmarkdown::render("~/R/armv7l-unknown-linux-gnueabihf-library/3.2/rmarkdown/rmarkdown/templates/html_vignette/skeleton/skeleton.Rmd") # the output <skeleton.html> is located under the same dir as <skeleton.Rmd>
Note that the image files in the html are embedded Base64 images in the html file. See
- http://stackoverflow.com/questions/1207190/embedding-base64-images
- Data URI scheme
- http://www.r-bloggers.com/embed-images-in-rd-documents/
- How to not embed Base64 images in RMarkdown
- Upload plots as PNG file to your wordpress
Templates
- https://github.com/rstudio/rticles/tree/master/inst/rmarkdown/templates
- https://github.com/rstudio/rticles/blob/master/inst/rmarkdown/templates/jss_article/resources/template.tex
Knit button
- It calls rmarkdown::render()
- R Markdown = knitr + Pandoc
- rmarkdown::render () = knitr::knit() + a system() call to pandoc
Exit knitting early
Pandoc's Markdown
Originally Pandoc is for html.
Extensions
- YAML metadata
- Latex Math
- syntax highlight
- embed raw HTML/Latex (raw HTML only works for HTML output and raw Latex only for Latex/pdf output)
- tables
- footnotes
- citations
Types of output documents
- Latex/pdf, HTML, Word
- beamer, ioslides, Slidy, reval.js
- Ebooks
- ...
Some examples:
pandoc test.md -o test.html pandoc test.md -s --mathjax -o test.html pandoc test.md -o test.docx pandoc test.md -o test.pdf pandoc test.md --latex-engine=xlelatex -o test.pdf pandoc test.md -o test.epb
Check out ?rmarkdown::pandoc_convert()/
When you click the Knit button in RStudio, you will see the actual command that is executed.
pandoc included in RStudio
RStudio included pandoc already. On my Ubuntu 20.04 it is included in "/usr/lib/rstudio/bin/pandoc/" (Sys.getenv("RSTUDIO_PANDOC")). Note the path is not in the global environment PATH.
Check pandoc availability and version
pander package
https://rapporter.github.io/pander/
Global options
Suppose I want to create a simple markdown only documentation without worrying about executing code, instead of adding eval = FALSE to each code chunks, I can insert the following between YAML header and the content. Even bash chunks will not be executed.
```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, eval = FALSE) ```
Examples/gallery
Some examples of creating papers (with references) based on knitr can be found on the Papers and reports section of the knitr website.
- https://rmarkdown.rstudio.com/gallery.html
- https://github.com/EBI-predocs/knitr-example
- https://github.com/timchurches/meta-analyses
- http://www.gastonsanchez.com/depot/knitr-slides
- Reproducible Research: Write your Clinical Chemistry paper using R Markdown
Read the docs Sphinx theme and journal article formats
http://blog.rstudio.org/2016/03/21/r-markdown-custom-formats/
- rticles package
- rmdformats package
rmarkdown news, floating TOC
- floating table of contents and tabbed sections. An example from the tximport vignette with its source code.
How to create TOC from HTML/pdf?
Some examples that have no TOC in vignettes
Is there any web app that can generate the TOC?
Useful tricks when including images
- Useful tricks when including images in Rmarkdown documents
- Image sizes in an R markdown Document
- External Graphics with knitr
- Improved accessibility with image alt text. knitr 1.32.
Space
Add horizontal space
Use See https://stackoverflow.com/a/36831500
Add vertical space
See here.
Figure
Avoid blank pages
Don't generate more than one plot in one chunk (learned by experience).
Reference a figure in latex
LaTeX examples: How to reference a figure or table
We can use this technique in rmarkdown. For example:
- In text: reference by \ref{fig1}
- In chunk:
```{r myLabel, fig.cap="My caption\\label{fig1}"} ```
Floating
See the solution Prevent figures from floating and the comment there
In general, we do not recommend that users force LaTeX to stop floating figures. This solution was included in this book by popular demand,2 but there could be some serious side-effects when LaTeX is unable to float figures.
Related to figures or tables floating, if we want to start in a new page, we can use \clearpage instead of \newpage; see Why is \newpage ignored sometimes ?
- https://stackoverflow.com/a/31081801. Add fig_caption: true to the YML section. In R chunk use {r, fig.cap=paste("Your caption.")}
- https://community.rstudio.com/t/figure-caption-in-r-markdown/6951/20
Figure size
Different ways to set figure size in RMarkdown
1. Define size in YAML header
--- title: "My Document" output: html_document: fig_width: 6 fig_height: 4 ---
2. Global chunk
knitr::opts_chunk$set(fig.width=12, fig.height=8)
3. Chunk options
{r fig1, fig.align = 'center', fig.height = 3, fig.width = 5} {r fig3, fig.width = 5, fig.asp = .62} {r fig4, out.width = '40%'}
4. use knitr::include_graphics(); see Tips and tricks for working with images and figures in R Markdown documents
```{r out.width = "50%"} include_graphics(img1_path) ```
Tables
Rmd --> md (by pandoc) --> tex --> pdf
The default output is a markdown table when kable() is used in an Rmd document. It is Pandoc that converts the markdown table to latex table.
Package | Example | Comment |
---|---|---|
kable | ||
kableExtra | kable() + piping. bsSimulscript.Rmd | |
xtable | bsSimulTable.Rmd | vignette is in Rnw. |
NB. In one case, I have a short table (2 rows only) and the table cannot be put on the same page of a figure which takes up a whole page and a lot of space was wasted at the top and bottom of the page. However, after I change the way of creating the table from xtable to knitr::kable() & knitrExtra, the problem is gone.
How to Make Beautiful Tables in R: gt, kable + kableExtra, formattable, DT, reactable, flextable.
kable
Quicker knitr kables in RStudio notebook
Caption and special character
When the caption (no matter we are using kable() or xtable()) contains special characters like an underscore character "_" (e.g. "HALLMARK_P53_PATHWAY"), the table cannot be rendered correctly (the pdf file will not be able to interpret \begin{table}[!h] and \caption{} ).
So a simple solution like gsub("_", "", genesetid) will solve this problem.
kableExtra
https://cran.r-project.org/web/packages/kableExtra/index.html
Example:
options(knitr.table.format = "latex", knitr.kable.NA = "") kable(x, booktabs = T, digits=4, caption = "deltaC from 10 splits\\label{tb1}") %>% kable_styling(font_size = 8, latex_options = c("striped", "hold_position"))
How to stop bookdown tables from floating to bottom of the page in pdf?
For HTML output, we can highlight elements in certain column using different colors. See this vignette from postpi.
gtsummary
xtable
The caption created by xtable() was under a table. Cf. The caption is above the table when we use kable().
The package assume the document type is html or pdf. Other types like doc does not work.
Example:
print(xtable(x, digits = 4, caption = "deltaC \\label{tb3}"), comment = FALSE, size="\\fontsize{7pt}{8pt}\\selectfont", floating = TRUE, latex.environments="center")
xtableList() can create a list of tables; see xtable List of Tables Gallery.
Below is an Rmarkdown example that would generate a pdf file with desired tables. Pay attention to various options here because the default options won't work.
--- title: "xtable in rmarkdown" date: "`r format(Sys.time(), '%d %B, %Y')`" output: pdf_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = FALSE, message=FALSE) ``` ```{r , results='asis'} require(xtable) data(mtcars) mtcars <- mtcars[, 1:6] mtcarsList <- split(mtcars, f = mtcars$cyl) ### Reduce the size of the list elements mtcarsList[[1]] <- mtcarsList[[1]][1,] mtcarsList[[2]] <- mtcarsList[[2]][1:2,] mtcarsList[[3]] <- mtcarsList[[3]][1:3,] attr(mtcarsList, "subheadings") <- paste0("Number of cylinders = ", names(mtcarsList)) attr(mtcarsList, "message") <- c("Line 1 of Message", "Line 2 of Message") xList <- xtableList(mtcarsList) print.xtableList(xList, comment=FALSE) ```
The following example is to give alternative colors on rows. Note the option header-includes and tables in YAML section
--- title: "testxtable" header-includes: - \usepackage{colortbl} output: pdf_document tables: true --- ```{r cars, results='asis'} library(xtable) mydf <- data.frame(id = 1:10, var1 = rnorm(10), var2 = runif(10)) rws <- seq(1, (nrow(mydf)-1), by = 2) col <- rep("\\rowcolor[gray]{0.95}", length(rws)) print(xtable(mydf), booktabs = TRUE, add.to.row = list(pos = as.list(rws), command = col)) ```
Tips
- Changing the font size of table using print.xtable (\fontsize{<size>}{<line space>})
print(xt, size="\\fontsize{9pt}{10pt}\\selectfont")
- Avoiding floating xtable in knitr hides the caption of the table. Only floating has captions.
print(xtable(.., caption = " "), floating = TRUE)
Gmisc: create Table 1 used in medical articles
https://cran.r-project.org/web/packages/Gmisc/index.html
tableone
https://cran.r-project.org/web/packages/tableone/
printr
https://cran.r-project.org/web/packages/printr/index.html
DT
https://cran.r-project.org/web/packages/DT/index.html
flextable
flextable. Create pretty tables for 'HTML', 'Microsoft Word' and 'Microsoft PowerPoint' documents.
sparkTable
https://cran.r-project.org/web/packages/sparkTable/
sparkTable: Generating Graphical Tables for Websites and Documents with R. It depends on the Rglpk package which requires the glpk library. However, sparkTable is not maintained anymore.
skimr for useful and tidy summary statistics is new and provide a histogram next to each variable.
mmtable2
mmtable2 allows you to create and combine tables with a ggplot2/patchwork syntax.
How to align table and plot in rmarkdown html_document
https://stackoverflow.com/a/54359010
Equations
equationmatic package
RMarkdown Template that Manages Academic Affiliations
RMarkdown Template that Manages Academic Affiliations
Converting Rmarkdown to F1000Research LaTeX Format
BiocWorkflowTools package and paper
Convert markdown to HTML
- Use Markdown For Easy Web Writing and markdown 1.0.1 (Dec 2004) perl script.
- https://www.markdownguide.org/tools/ and An overview of Markdown, how it works, and what you can do with it.
- Awesome markdown
Internal links
If my section header is written as "## my section Header". Then I can link to it by using "[linked phrase](#my-section-header)".
Note here
- Use one number sign (#) even it is a subsection
- Use the hyphen sign to connect the space character
- Use lower cases even the header contains capital letters
Another easier way to use the Heading identifiers as described in pandoc. In the header use "# section 1{#s1}" and in the toc or paragraph use '[my section name](#s1)".
Hyperlink color in pdf
Add below to the yaml for PDF documents. See R Markdown: The Definitive Guide.
urlcolor: blue
Colored text
Blue text . See How to apply color in Markdown?.
icons for rmarkdown
https://ropensci.org/technotes/2018/05/15/icon/
Reproducible data analysis
- http://blog.jom.link/implementation_basic_reproductible_workflow.html
- liftr package (John M. Chambers Statistical Software Award, 2018).
- Infrastructure and Tools for Teaching Computing Throughout the Statistical Curriculum Mine Çetinkaya-Rundel & Colin Rundel 2017
- R Markdown: 3 sources of reproducibility issues and options how to tackle them
- Rmd Workflows Make New Statistical Methods Accessible to Biomedical Researchers Michael Love 2020
html document
https://bookdown.org/yihui/rmarkdown/html-document.html
Search button and http server
It works on browser with a web server (including RStudio built-in). It does not work if we just open the index.html file directly in a web browser (e.g. browseURL("index.html") from R).
writeLines("<h1>Hi</H1>", "index.html") library(servr) servr::httd()
Interactive document: Shiny
When I follow the direction to add the code to the end of this Rmd file, I see
- I can't run "Build" anymore. An error will come out: Error in numericInput("n", "How many cars?", 5) : could not find function "numericInput".
- After I click "Run Document", the Rmd file will be displayed in either RStudio or a regular browser using R's built-in web server (http://127.0.0.1:YYYY/XXX.Rmd).
How to boost R Markdown interactivity with runtime Shiny and the article
Tooltip
tippy: Add Tooltips to 'R markdown' Documents or 'Shiny' Apps
Theme, CSS
- how to add new html themes from Bootswatch?
- HTML & CSS
- Academic theme
- R Colors in CSS for R Markdown HTML Documents
Dropdown menus in R Markdown with bsselectR
devtools::install_github("walkerke/bsselectR") library(bsselectR) state_plots <- list.files("~/Pictures", pattern = '*.png', full.names = T) names(state_plots) <- str_replace_all(state_plots, c("\\.png" = "", "plots/" = "")) bsselect(state_plots, type = "img")
Automatic document production with R
https://itsalocke.com/improving-automatic-document-production-with-r/
Documents with logos, watermarks, and corporate styles
http://ellisp.github.io/blog/2017/09/09/rmarkdown
rticles and pinp for articles
- https://cran.r-project.org/web/packages/rticles/index.html
- http://dirk.eddelbuettel.com/code/pinp.html
Presentation
- Slide Presentations
- https://bookdown.org/yihui/rmarkdown/presentations.html
- https://codingclubuc3m.rbind.io/post/2019-09-24/
PowerPoint slides
- https://rstudio.com/resources/webinars/make-powerpoint-presentations-with-r-markdown/
- https://bookdown.org/yihui/rmarkdown/powerpoint-presentation.html
- Rendering PowerPoint Presentations with RStudio
xaringan: presentation
- https://github.com/yihui/xaringan
- R markdown template for writing these research plans
- Create R Markdown reports and presentations even better with these 3 practical tips
- Live preview of R Markdown files with xaringan’s infinite_moon_reader(). The Ultimate Infinite Moon Reader for xaringan Slides.
- Creating beautiful, multi format reports directly from R scripts
- Advanced chunk options with useful effects
- Mastering R presentations
- Examples:
- xaringanExtra, useR! 2020 youtube video.
- Deploying xaringan Slides: A Ten-Step GitHub Pages Workflow
Create presentation file (beamer)
- http://rmarkdown.rstudio.com/beamer_presentation_format.html
- http://www.theresearchkitchen.com/archives/1017 (markdown and presentation files)
- http://rmarkdown.rstudio.com/
- Create Rmd file first in Rstudio by File -> R markdown. Select Presentation > choose pdf (beamer) as output format.
- Edit the template created by RStudio.
- Click 'Knit pdf' button (Ctrl+Shift+k) to create/display the pdf file.
An example of Rmd is
--- title: "My Example" author: You Know Me date: Dec 32, 2014 output: beamer_presentation --- ## R Markdown This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>. When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. ## Slide with Bullets - Bullet 1 - Bullet 2 - Bullet 3. Mean is $\frac{1}{n} \sum_{i=1}^n x_i$. $$ \mu = \frac{1}{n} \sum_{i=1}^n x_i $$ ## New slide ![picture of BDGE](/home/brb/Pictures/BDGEFinished.png) ## Slide with R Code and Output ```{r} summary(cars) ``` ## Slide with Plot ```{r, echo=FALSE} plot(cars) ```
Cache
- https://yihui.org/knitr/demo/cache/
- If my markdown file is called abc.Rmd, then two cache directories (abc_cache & abc_files) will be created by default. See rmarkdown’s site generator.
- *_files: figures.
- *_cache: *.RData, *.rdb, __packages.
- When knitting failed due to my error, I will rm these two directories, fix my code and knit again.
- Cache not work
- Examples
> system.time(rmarkdown::render("~/Downloads/tmp.Rmd")) # first time ... Output created: tmp.html user system elapsed 3.123 0.108 5.426 # It will create two directories: tmp_files, tmp_cache > system.time(rmarkdown::render("~/Downloads/tmp.Rmd")) # Second time ... Output created: tmp.html user system elapsed 0.239 0.019 0.317
warning
If we enable cache, be careful on the consequence of just modifying one chunk code. In the following example, if we just modify chunk1 chunk, it will not modify the result from the chunk2 cache.
```{r chunk1} x <- 1 ``` ```{r chunk2, echo=FALSE} print(x) ```
cache.extra option: dependence
- rmarkdown cookbook > Chunk > Cache. Some examples
- cache.extra = tools::md5sum('my-precious.csv')
- cache.extra = getRversion()
- cache.extra = Sys.Date()
- cache.extra = Sys.info()[['sysname']]
- We can use our own way to replace the cache design in knitr; see a simple example in the Cache subsection.
- How to make code chunks depend on all previous chunks in knitr/rmarkdown?
--- title: "myRmarkdown" output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE, cache = TRUE) ``` ```{r cars} x <- 1:5 ``` ```{r pressure, echo=FALSE, cache.extra = x} plot(x) ```
If we made a change on 'x', the 'pressure' chunk will be re-run when we knit the document.
Dependent on an R code
Use code = readLines('test.R') option in chunk; see How to make code chunks depend on all previous chunks in knitr/rmarkdown? and code chunk documentation.
interfere with RStudio
If I use RStudio to knit an Rmd file and the Rmd file has cache = TRUE, it will remember this option. For example, I also open another Rmd file and try to run rmarkdown::render("XXX.Rmd"), it will create XXX_cache folder even the new Rmd file has cache = FALSE. Solution: knit the new file in a terminal.
read knitr/Rmd cache
read knitr/Rmd cache in interactive session?. Use lazyLoad() function without specifying any extension.
lazyLoad("unnamed-chunk-1_3c3ad57469a118cd9c584c8d941d2c09")
Note:
- lazyLoad("~/Path/unnamed-chunk-1_3c3ad57469a118cd9c584c8d941d2c09") will give an error cannot open 'xxx.rdb:' No such file or directory. It does not recognize the symbol "~/".
- It is better to use a chunk name without "-" (or some special characters like ".") in order to find the file using the chunk name. Then I can use grep("myChunkName_.+\\.rdb", ., value=T) %>% sub("\\..*$", "", .) to find the chunk's filename. Also when we use pipe to run lazyLoad(), it does not work unless we specify an argument; see Pipe in magrittr package is not working for function load().
list.files('myProject/fileName_cache/latex', full.names = T) %>% grep("myChunkName_.+\\.rdb", ., value=T) %>% sub("\\..*$", "", .) %>% lazyLoad(envir = globalenv()) lazyLoad2 <- function(filedir, name) { # Example: # lazyLoad2("gse6532_cache/latex", "removeProg") # Warning: # This assumes the 'name' does not contain any special characters if (missing(filedir)) { cat("Example:") cat(" lazyLoad2(\"project_cache/latex\", \"allF\") \n") return(invisible()) } files <- grep(name, list.files(filedir, full.names = TRUE), value = TRUE)[1] file1 <- grep("\\.RData$", files, value = TRUE) lazyLoad(gsub("\\.RData$", "", file1 ), envir = globalenv()) }
_cache and _files folders
Files in these two folders do not work when I copy these two folders generated from macOS to Linux.
RStudio
RStudio is the best editor.
Markdown has two drawbacks: 1. it does not support TOC natively. 2. RStudio cannot show headers in the editor.
Therefore, use rmarkdown format instead of markdown.
Writing a R book and self-publishing it in Amazon
Create professional reports from R scripts, with custom styles
How to create professional reports from R scripts, with custom styles
Publish R results
5 amazing free tools that can help with publishing R results and blogging
Scheduling R Markdown Reports via Email
http://www.analyticsforfun.com/2016/01/scheduling-r-markdown-reports-via-email.html
R notebook vs R markdown in RStudio
Difference between R MarkDown and R NoteBook
There is no coding difference. The difference is in the rendering. The file extension is the same.
R notebook
- It adds html_notebook in the output option in the header.
- You can then preview the rendering quickly without having to knit it (does not execute any of your R code chunks). If you manually 'Run' the chunks, the result will be shown up in preview.
- It also refreshes the preview every time you save.
- However in that preview you don't have the code output (no figures, no tables..)
- You can mix several output options in your header so that you can keep the preview and keep your knit options for export
- R Notebook is everything and above what R MarkDown is
Table creating packages
- stargazer: Produces LaTeX code, HTML/CSS code and ASCII text for well-formatted tables that hold regression analysis results from several models side-by-side, as well as summary statistics
- Empirical economics with r (part b): confounders, proxies and sources of exogenous variations
Graphics Output in LaTeX Format
- Using tikzdevice to insert R Graphs into LaTeX
- tikzDevice: R Graphics Output in LaTeX Format
Landscape output
Break a long document
First World Problems: Very long RMarkdown documents
Programmatically create new headings and outputs
Programmatically create new headings and outputs in Rmarkdown
Request an early exit
https://stackoverflow.com/a/33711413
Bibliographies
- Bibliographies in RStudio Markdown are difficult – here’s how to make it easy
- A Roundup of R Tools for Handling BibTeX 2020
Docker
How to compile R Markdown documents using Docker
lazy load plots
lazyrmd package
Emoji
- https://en.wikipedia.org/wiki/Emoji
- emo package.
- An example from biocthis vignette
- Support Emoji in Markdown
- How New Emoji Are Born (and How to Propose Your Own)
- How to Make Your Own Emoji
Citation
knitcitations. knitcitations is an R package designed to add dynamic citations to dynamic documents created with Yihui's knitr package.
Video
ari: The Automated R Instructor
Render an R script
Chapter 19 Render an R script from Happy Git and GitHub for the useR by Jenny Bryan.
Render the R script through one of these methods:
- Click on the “notebook” icon in RStudio to “Compile Report”.
- In RStudio, do File > Knit Document.
- In R, do rmarkdown::render("YOURSCRIPT.R").
bookdown.org
The website is full of open-source books written with R markdown.
- Announce bookdown
- bookdown package: Authoring Books and Technical Documents with R Markdown. We can download bookdown-demo to write our book.
- R Graphics Cookbook. Check Makefile and workflow. 8GB is still too tight. Error: libpng warning: iCCP: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG.
- Compile R for Data Science to a PDF
It is easy to download the book. Check the download icon on top (Toggle Sidebar, Search, Font settings, Edit, Download, Info).
To build the book, either use the "Build" button on the top right panel or use the command line
bookdown::render_book("index.Rmd", "bookdown::gitbook") bookdown::render_book("index.Rmd", "bookdown::pdf_book")
For example, to build the r4ds book (website), using
# git clone https://github.com/hadley/r4ds.git # Open the project in RStudio # Using 'Build Book' button will give an error # Error in rmarkdown::render_site() : No site generator found devtools::install_github("hadley/r4ds") bookdown::render_book("index.Rmd", "bookdown::gitbook")
The generated folder _book is 25MB vs 51MB if we use webhttrack.
The bookdown website is easy to navigate using left/right arrow keys.
Figures in a bookdown are handled different from regular R markdown files. The full file path does not work. The "~/" or "../" symbol does not work. The symbolic link directories or files do not work. The only way it works is by creating a subdirectory under the bookdown index.Rmd file.
Also the way of including figures is different in R markdown and bookdown. In bookdown, we should follow this way. That is including knitr::include_graphics("images/myfile.png") in an R block. Recall that in R markdown file, we use ![](figures/myfile.png).
Alternatively we can use webhttrack to download the whole website/book without re-building the book in R.
TexLive
TexLive can be installed by 2 ways
- sudo apt install texlive It includes tlmgr utility for package manager.
- Official website
texlive-latex-extra
https://packages.debian.org/sid/texlive-latex-extra
For example, framed and titling packages are included.
tlmgr - TeX Live package manager
https://www.tug.org/texlive/tlmgr.html
Examples
- Tidy Text Mining with R by Julia Silge and David Robinson (one of many books hosted on BOOKDOWN website).
- Build 'R for Data Science by Garrett Grolemund, Hadley Wickham' 2019-0808
- Build gitbook is OK.
- Book will not compile for PDF. It shows an error about always_allow_html: yes.
- Run sudo apt install texlive-xetex on Ubuntu (See _output.yaml file). We need to use xelatex. If we change to use pdflatex, we will get an error Rendering of unicode characters fails.
- The pdf output _main.pdf is under _book/.
- R Markdown: The Definitive Guide
- Make sure we have done install.packages('tinytex'); tinytex::install_tinytex()
- Change the font "Source Code Pro" to "Ubuntu" in index.Rmd and rmarkdown.Rmd (I am working on Ubuntu)
- Change the font Palatino to Ubuntu in index.Rmd and rmarkdown.Rmd
YAML
Author with affiliation in bookdown: HTML and pdf
Create a website using R Markdown
R Markdown Websites, Files, R Markdown: The Definitive Guide -> rmarkdown’s site generator
- RStudio -> File -> New Project -> Simple R Markdown Website. This will create 3 files: _site.yml, index.Rmd and about.Rmd
- Click Build -> Build Website button or execute the R -q -e "rmarkdown::render_site()" function from within the directory containing your files to build _site, a directory of files ready to deploy as a standalone static website. In this simple example, it will generate index.html, about.html and a new directory site_libs. Note we are not supposed to manually edit any html files.
If we host these files on our server using Apache, we need to make sure the owner of the directory and files is www-data.
- # move/copy the content in the folder _site to /var/www/mySite
- cd /var/www/mySite # make sure the permission and owner of the directory are OK
- sudo chown www-data:www-data *.html # any explanation
- sudo chown -R www-data:www-data site_libs/ # this directory is generated by R
- sudo nano /etc/apache2/sites-enabled/mySite.conf # see Apache
- # sudo a2ensite mySite.conf
- sudo service apache2 reload
Examples
- https://rmarkdown.rstudio.com/gallery.html
- https://jules32.github.io/rmarkdown-website-tutorial/
- https://www.emilyzabor.com/tutorials/rmarkdown_websites_tutorial.html
pkgdown: create a website for your package
- Creating websites in R Emily C. Zabor
- Tip: After creating an R project with <_site.yml> and <index.Rmd> files, we need to quit and open the R project again in order to see the "Build" tab. This can be confirmed by clicking Tools -> Project Options -> Build Tools. The 'Project build tools' should show "Website" (other choices are "(None)", "Package", "Makefile", "Custom"). See also the RStudio IDE section from blogdown: Creating Websites with R Markdown.
- Building a website with pkgdown: a short guide
- Integrating 'pkgdown' with Your Personal Website
- Introduction to pkgdown. The source code is an Rmd file.
Blogdown
- https://github.com/rstudio/blogdown
- https://bookdown.org/yihui/blogdown/
- BLOGDOWN TUTORIAL Peter's blog
- Customizing Hugo / Blogdown RSS Templates
- Setting up our blog with RStudio and blogdown I: Creating the blog
- What to know before you adopt Hugo/blogdown
- 10 Things We Learned in Creating the Blog Guide with bookdown 2020
Jekyll and Github
Pagedown
pagedown, pagedown: Creating beautiful PDFs with R Markdown and CSS (video)
postcards
postcards - Create simple, beautiful personal websites and landing pages using only R Markdown.
Posterdown
posterdown: Use RMarkdown to generate PDF Conference Posters via HTML or LaTeX
rticles package
LaTeX Journal Article Templates for R Markdown
Latex tools
Mathpix Snip Take a screenshot of math and paste the LaTeX into your editor
Math
- Use \textrm{} for normal text in math mode. For example, $n_\textrm{training} = n_\textrm{test} = 500$.