Rmarkdown: Difference between revisions

From 太極
Jump to navigation Jump to search
No edit summary
 
(334 intermediate revisions by the same user not shown)
Line 11: Line 11:


* An example https://gist.github.com/jeromyanglim/2716336
* An example https://gist.github.com/jeromyanglim/2716336
* [http://daringfireball.net/projects/markdown/basics basics] and [http://daringfireball.net/projects/markdown/syntax syntax]
* [http://daringfireball.net/projects/markdown/basics basics] and [http://daringfireball.net/projects/markdown/syntax syntax]
* Convert mediawiki to markdown using online conversion tool from [http://johnmacfarlane.net/pandoc/try/ pandoc].
* Convert mediawiki to markdown using online conversion tool from [http://johnmacfarlane.net/pandoc/try/ pandoc].
* [http://support.mashery.com/docs/customizing_your_portal/Markdown_Cheat_Sheet Cheat sheet].
* [http://support.mashery.com/docs/customizing_your_portal/Markdown_Cheat_Sheet Cheat sheet].
* [http://dillinger.io/ Cloud-enabled HTML5 markdown editor]
* [http://dillinger.io/ Cloud-enabled HTML5 markdown editor]
* [http://www.crypti.cc/markdown-here/livedemo.html live demo]
* [http://www.crypti.cc/markdown-here/livedemo.html live demo]
* [https://github.com/dgrapov/TeachingDemos/blob/master/Demos/OPLS/OPLS%20example.md Example from hosted in github]
* [https://github.com/dgrapov/TeachingDemos/blob/master/Demos/OPLS/OPLS%20example.md Example from hosted in github]
* [https://www.makeuseof.com/why-is-markdown-popular-reasons-you-should-use-it/ Why Is Markdown So Popular? 7 Reasons You Should Use It]


* [http://www.rstudio.com/ide/docs/r_markdown R markdown file] and use it in [http://www.rstudio.com/ide/docs/authoring/using_markdown RStudio]. Customizing Chunk Options can be found in [http://yihui.name/knitr/options knitr page] and [http://rpubs.com/gallery/options rpubs.com]. For example, the following will hide the code and also hide any messages
== Editors ==
  {r eval=TRUE, echo=FALSE, message=FALSE}
[https://www.howtogeek.com/why-you-should-be-writing-everything-in-markdown/ Why You Should Be Writing Everything in Markdown] 2023/10/11.
 


== Github markdown Readme.md ==
== Github markdown Readme.md ==
Line 42: Line 36:


== Syntax ==
== Syntax ==
Comment: https://stackoverflow.com/questions/4823468/comments-in-markdown
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
* [https://thisdavej.com/copy-table-in-excel-and-paste-as-a-markdown-table/ An online tool to copy Table in Excel and Paste as a Markdown Table]
 
Simple example
<pre>
| Column 1      | Column 2    | Column 3    |
| :------------- | :----------: | -----------: |
|  Cell Contents | More Stuff  | And Again    |
| You Can Also  | Put Pipes In | Like this \| |
</pre>
 
== Include code ==
[https://markdown.land/markdown-code-block Markdown Code Block: Including Code In .md Files]
<ul>
<li>Inline code blocks:
<pre>
Use `print("Hello, world!")` to print a message to the screen.
</pre>
<li>Fenced code blocks:
<pre>
```python
print("Hello, world!")
for i in range(10):
    print(i)
```
</pre>
<li>Indented code blocks
<pre>
Here's some regular text. And now a code block:
 
    print("Hello, world!")
    if True:
        print('true!')
</pre>
</ul>
 
== Literate programming ==
* [https://zh.wikipedia.org/zh-tw/文学编程 文學程式設計] vs 結構化程式設計(Structured programming)
* [https://exeter-data-analytics.github.io/LitProg/literate-programming.html Reproducibility and literate programming in R] TJ McKinley


= Rmarkdown =
= Rmarkdown =


* http://rmarkdown.rstudio.com/html_document_format.html
* [https://bookdown.org/yihui/rmarkdown/ R Markdown: The Definitive Guide]
* [https://bookdown.org/yihui/rmarkdown-cookbook/ R Markdown Cookbook]
* [https://www.rstudio.com/resources/videos/r-markdown-eight-ways/ R Markdown: Eight ways]
* [https://www.rstudio.com/resources/videos/r-markdown-eight-ways/ R Markdown: Eight ways]
* https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf
* https://www.rstudio.com/wp-content/uploads/2015/02/rmarkdown-cheatsheet.pdf
Line 52: Line 88:
* https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.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
* Chunk options http://kbroman.org/knitr_knutshell/pages/Rmarkdown.html
* [https://itsfoss.com/r-markdown/ Beginner's Guide to R Markdown Syntax (With Cheat Sheet)]


HTML5 slides examples
HTML5 slides examples
Line 88: Line 125:
If we are comfortable with HTML and CSS code, open the html file (generated by pandoc) and modify the CSS style at will.
If we are comfortable with HTML and CSS code, open the html file (generated by pandoc) and modify the CSS style at will.


== TinyTex for pdf output ==
== Tips ==
https://github.com/yihui/tinytex
* [https://www.rstudio.com/blog/r-markdown-tips-tricks-4-looks-better-works-better/ R Markdown Lesser-Known Tips & Tricks #4: Looks Better, Works Better]
* [https://www.rstudio.com/blog/r-markdown-tips-and-tricks-3-time-savers/ R Markdown Tips and Tricks #3: Time-savers & Trouble-shooters]
*# Convert an R script into an R markdown document with knitr::spin()
*# Convert an R markdown document into an R script with knitr::purl()
*# Reuse code chunks throughout your document
*# Cache your chunks (with dependencies)
*# Save the content of a chunk elsewhere with the cat engine
*# Include parameters to easily change values
*# Create templates with knit_expand()
*# Exit knitting early with knit_exit()
* [https://www.rstudio.com/blog/r-markdown-tips-tricks-2-cleaning-up-your-code/ R Markdown Lesser-Known Tips & Tricks #2: Cleaning Up Your Code]
* [https://www.rstudio.com/blog/r-markdown-tips-tricks-1-rstudio-ide/ R Markdown Lesser-Known Tips & Tricks #1: Working in the RStudio IDE]
* [https://indrajeetpatil.github.io/RmarkdownTips/ RMarkdown Tips and Tricks]
* [https://raukr-2022-rmd-skills.netlify.app/ Improve your R Markdown Skills] 2022/6/13
* [https://www.r-bloggers.com/2023/04/11-tricks-to-level-up-your-rmarkdown-documents/ 11 tricks to level up your rmarkdown documents] 2023/4/15
* [https://readmedium.com/optimizing-rmarkdown-documents-tips-for-efficient-reporting-e8653116d5d9 Optimizing RMarkdown Documents: Tips for Efficient Reporting] 11/17/2023, [https://addons.mozilla.org/en-US/firefox/addon/medium-parser/ Medium parser] by Xatta Trone
 
=== Debug ===
[https://adv-r.hadley.nz/debugging.html#rmarkdown Debugging RMarkdown]
 
== YAML ==
* [https://github.com/r-lib/ymlthis ymlthis] package - write YAML for R Markdown, bookdown, blogdown, and more.
* '''R Markdown Crash Course''' [https://zsmith27.github.io/rmarkdown_crash-course/lesson-4-yaml-headers.html YAML Headers]
 
Some examples
<pre>
---
title: "My Title"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  pdf_document:
    toc: true
    number_sections: true
classoption: landscape   
---
</pre>
 
<pre>
---
title: "My Title"
date: "`r format(Sys.time(), '%d %B, %Y')`"
output: latex_document
---
</pre>
 
=== Debug by keep tex ===
[https://stackoverflow.com/a/37702315 Rmarkdown Retain .tex file]. We can use this technique to see what goes wrong in the middle step.
<pre>
output:
  pdf_document:
    keep_tex: true
---
</pre>
 
=== HTML output ===
[https://scienceloft.com/technical/useful-yaml-options-for-generating-html-reports-in-r/ Useful YAML options for generating HTML reports in R]
 
=== HTML Turn off title ===
[https://stackoverflow.com/a/59679986 Rmarkdown: Turn off title]
 
=== HTML with dynamic table of contents ===
* https://juba.github.io/rmdformats/
* https://bookdown.org/yihui/rmarkdown/rmdformats.html
<pre>
---
title: "My title"
output:
  rmdformats::robobook:
    highlight: tango
    number_sections: true
    lightbox: true
    gallery: true
    code_folding: hide
---
</pre>
 
=== Inlcude latex packages ===
* '''\usepackage'''
* [https://bookdown.org/yihui/rmarkdown-cookbook/latex-preamble.html 6.1 Add LaTeX code to the preamble]
* [https://stackoverflow.com/a/39552048 Is there a way to have conditional markdown chunk execution in Rmarkdown?]
** Note that we can use latex packages in Rmd and output Word files.
** How to use rmarkdown::render('''params''')
** How to use rmarkdown::render('''output_file''')
 
=== render(, params) ===
* [https://stackoverflow.com/a/49908054 Run RMarkdown with arguments on the command line]
* [https://bookdown.org/yihui/rmarkdown/parameterized-reports.html Parameterized reports]
** Declare parameters in the YAML section
** You can access the parameters within the knitting environment and the R console
** Knitting with parameters
* [https://www.business-science.io/code-tools/2020/10/21/automate-pdf-with-r.html How to Automate PDF Reporting with R]
 
<pre>
---
title: "Homework"
output:
  html_document:
    code_folding: hide
    toc: true
    toc_float: true
    theme: "flatly"
params:
  run: TRUE
---
 
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = params$run)
```
 
```{r chunk1}
dim(iris)
```
</pre>
We can use the following command to create an html file that does not evaluate the code for the whole document.
<pre>
rmarkdown::render("Homework.Rmd",
          params=list(run = FALSE),
          output_file = "Homework.html")
</pre>
 
=== Code folding ===
* [https://bookdown.org/yihui/rmarkdown-cookbook/fold-show.html 7.5 Fold all code blocks but show some initially]
<ul>
<li>[https://bioconductor.org/books/release/OSCA/overview.html#quick-start OSCA] ebook. How the "View session info" work?
<ul>
<li>[https://bioconductor.org/books/release/OSCA/normalization.html Normalization] chapter and its [https://github.com/OSCA-source/OSCA.basic/blob/master/inst/book/normalization.Rmd source code].
<pre>
```{r, echo=FALSE, results="asis"}
extractFromPackage("zeisel-brain.Rmd", package="OSCA.workflows",
    chunk="quality-control", objects="sce.zeisel")
```
</pre>
</li>
<li>[http://www.bioconductor.org/packages/devel/bioc/vignettes/rebook/inst/doc/userguide.html Writing a book with reusable contents] from the [https://bioconductor.org/packages/release/bioc/html/rebook.html rebook] package. [https://github.com/LTLA/rebook/search?q=extractFromPackage source code]. </li>
</ul>
</li>
</ul>
 
=== Center title in html output ===
[https://stackoverflow.com/a/43430281 How do I center my YAML heading in an R markdown html document?] Use CSS styling.
 
=== Office (Word, Powerpoint) output ===
<ul>
<li>[https://davidgohel.github.io/officer/ officer] R package
<pre>
# Load the officer and flextable packages
library(officer)
library(flextable)
 
# Create a new Word document
doc <- read_docx()
 
# Create some data
data <- iris[1:5, ]
 
# Add a title to the document
doc <- body_add_par(doc, "My Table", style = "heading 1")
 
# Create a table using the flextable() function
my_table <- flextable(data)
 
# Add the table to the Word document
doc <- body_add_flextable(doc, my_table)
 
# Write the Word document to a file
print(doc, target = "my_word_document.docx")
</pre>
<li>Landscape:
* [https://rmarkdown.rstudio.com/articles_docx.html Happy collaboration with Rmd to docx] and [https://stackoverflow.com/a/52722548 Unable to produce landscape orientation]. Works for me.
* [https://davidgohel.github.io/officedown/ officedown] package, [https://stackoverflow.com/a/73769125 R markdown landscape-only word document], [https://bookdown.org/yihui/rmarkdown-cookbook/word-officedown.html 8.3 Style individual elements]. The package was suggested by [https://cloud.r-project.org/web/packages/flextable/index.html flextable]. Not working for me.
 
<li>Use a customized word template. [https://fortune9.netlify.app/2024/01/28/rmarkdown-automate-word-document-generation-using-rmarkdown/ Automate word document generation using Rmarkdown]
</ul>
 
=== Examples ===
[https://github.com/tenglongli/ComBatCorr/tree/master/Example ComBatCorr], [https://htmlpreview.github.io/?https://github.com/tenglongli/ComBatCorr/blob/master/Example/Manuscript-Output.html Preview HTML]
<pre>
---
title: "..."
author: "..."
output:
  html_document:
    code_folding: hide
    toc: true
    toc_float: true
    theme: "flatly"
editor_options:
  chunk_output_type: console
---
</pre>
PDF output
<pre>
---
title: "..."
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  pdf_document:
    toc: true
    toc_depth: 3
    number_sections: true
    fig_caption: true
urlcolor: blue
---
</pre>
 
== Chunk options ==
* [https://bookdown.org/yihui/rmarkdown/r-code.html R code chunks and inline R code] from R Markdown: The Definitive Guide.
* [http://yihui.name/knitr/options knitr page]
* [http://rpubs.com/gallery/options rpubs.com].
* [https://www.jumpingrivers.com/blog/knitr-default-options-settings-hooks/ 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 (<strike>if FALSE, all warnings will be printed in the console instead of the output document</strike>). ''This seems to be useful''. Too many warnings will make the computation time much longer (eg. 3 hours vs 30 minutes) no to say the output file will be very large.
* error. whether to preserve errors (from stop()); If you want to show the errors without stopping R, you may use the chunk option error = TRUE. See [https://bookdown.org/yihui/rmarkdown-cookbook/opts-error.html Do not stop on error]
* comment. [https://stackoverflow.com/a/15081230 Remove Hashes in R Output from R Markdown and Knitr]
<pre>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, cache = TRUE, warning = FALSE,
                      message = FALSE, verbose = FALSE)
```
</pre>
 
=== 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.
<pre>
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
```
</pre>
 
=== Include sections inside a chunk ===
<pre>
```{r}
# Chunk section 1 ----
...
# Chunk section 2 ----
```
</pre>
 
=== Ignore duplicate chunk labels ===
I got an error '''Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block Execution halted''' because of duplicate chunk labels.
 
See [https://bookdown.org/yihui/rmarkdown-cookbook/duplicate-label.html Chapter 14 Miscellaneous knitr Tricks]
 
=== Include all the code from the Rmd ===
Use knitr::all_labels().
* [https://bookdown.org/yihui/rmarkdown-cookbook/code-appendix.html Put together all code in the appendix]. 
* [http://adamleerich.com/2020/10/30/report-with-code-appendix.html Rmd-based Reports with R Code Appendices]
 
=== Conditional `echo` (or eval or include) in rmarkdown chunks ===
[https://yihui.org/knitr/demo/output/ Control eval or output]. Following is an example that the chunk will be run if a "linux" machine is detected (e.g. the linux machine is some quality such as a larger RAM).
<pre>
```{r label}
get_os <- function(){
  sysinf <- Sys.info()
  if (!is.null(sysinf)){
    os <- sysinf['sysname']
    if (os == 'Darwin')
      os <- "osx"
  } else { ## mystery machine
    os <- .Platform$OS.type
    if (grepl("^darwin", R.version$os))
      os <- "osx"
    if (grepl("linux-gnu", R.version$os))
      os <- "linux"
  }
  tolower(os)
}
 
doNextChunk <- get_os() == "linux"
```
 
```{r conditional, eval = doNextChunk}
"hello world!"
```
</pre>
 
=== Conditional evaluation using params ===
See [[#render.28.2C_params.29|here]]
 
=== Conditional code for specific output formats ===
Breaking the page in LaTeX output for PDFs. Note that commands passed to LaTeX in this way need to be escaped with a double slash.
<pre>
`r if (knitr::is_latex_output()) '\\newpage'`
</pre>
 
=== results = "asis" ===
[https://bookdown.org/yihui/rmarkdown-cookbook/results-asis.html Output text as raw Markdown content]
 
== "source" (run) all of the code chunks in a .qmd file ==
[https://twitter.com/rlbarter/status/1646249384403959808?s=20 "source" (run) all of the code chunks in a .qmd file (like `source("analysis.qmd")`) inside another .qmd file?] See [https://bookdown.org/yihui/rmarkdown-cookbook/purl.html 3.4 Convert R Markdown to R script]
<pre>
```{r}
library(knitr)
temp_r_file <- tempfile(fileext = '.R')
purl("first-file.qmd", output = temp_r_file)
source(temp_r_file)
unlink(temp_r_file)
```
</pre>
 
== Multiple plots/tables in one chunk ==
[https://twitter.com/dgkeyes/status/1479208406799978496 twitter]
 
== Reuse code chunks, combine chunks ==
See [https://www.rstudio.com/blog/r-markdown-tips-and-tricks-3-time-savers/ R Markdown Tips and Tricks #3: Time-savers & Trouble-shooters]
 
== Comment chunks not working ==
[https://stackoverflow.com/questions/46148097/comment-out-some-chunks-part-of-rmd-file Comment out some chunks/part of Rmd file]. Use 'eval = FALSE' works.
 
== inline code ==
https://rmarkdown.rstudio.com/lesson-4.html ('''`r x<-5 `''')
 
== Language support (engine) ==
* https://bookdown.org/yihui/rmarkdown/language-engines.html
* [https://cran.r-project.org/web/packages/docknitr/vignettes/docknitr.html engine=docker]
 
== Mix markdown with Latex ==
* [https://stackoverflow.com/questions/47463861/how-to-use-latex-code-in-r-chunk-in-r-markdown How to use LaTeX Code in R Chunk in R-Markdown?]
* https://bookdown.org/yihui/rmarkdown-cookbook/raw-latex.html
* [https://stackoverflow.com/a/2552701 How can I mix LaTeX in with Markdown?]
 
== TinyTex for pdf output (works on Windows OS too) ==
https://github.com/yihui/tinytex and [https://yihui.name/tinytex/r/#debugging Debugging] page for different solutions.


On NIH/biowulf, there is no 'pdflatex' program. So a pdf file cannot be generated.  
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.
I install tinytex. At the end, many latex executable files (pdflatex, bibtex, ...) are installed under ~/bin directory.
<pre style="white-space: pre-wrap; /* CSS 3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* IE 5.5+ */ " >
 
Step 1:
{{Pre}}
> install.packages("tinytex")
> install.packages("tinytex")
trying URL 'https://yihui.name/gh/tinytex/tools/install-unx.sh'
trying URL 'https://yihui.name/gh/tinytex/tools/install-unx.sh'
Content type 'text/plain; charset=utf-8' length 616 bytes
Content type 'text/plain; charset=utf-8' length 616 bytes
==================================================
...
...
tlmgr: package log updated: /spin1/home/linux/USERNAME/.TinyTeX/texmf-var/web2c/tlmgr.log
tlmgr: package log updated: /spin1/home/linux/USERNAME/.TinyTeX/texmf-var/web2c/tlmgr.log
TinyTeX installed to /spin1/home/linux/USERNAME/.TinyTeX
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).
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).
</pre>
Step 2: I also need to run
{{Pre}}
> 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).
</pre>
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(). [https://github.com/yihui/tinytex/issues/39 Missing package. "Remote repository newer than local"], [https://yihui.org/tinytex/faq/ 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)“.]
<pre>
$ 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
</pre>
</pre>


== Built-in examples from rmarkdown ==
== Built-in examples from rmarkdown ==
<syntaxhighlight lang='rsplus'>
{{Pre}}
# This is done on my ODroid xu4 running Ubuntu Mate 15.10 (Wily)
# This is done on my ODroid xu4 running Ubuntu Mate 15.10 (Wily)
# I used sudo apt-get install pandoc in shell
# I used sudo apt-get install pandoc in shell
Line 114: Line 509:
rmarkdown::render("~/R/armv7l-unknown-linux-gnueabihf-library/3.2/rmarkdown/rmarkdown/templates/html_vignette/skeleton/skeleton.Rmd")
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>
# the output <skeleton.html> is located under the same dir as <skeleton.Rmd>
</syntaxhighlight>
</pre>


Note that the image files in the html are embedded '''Base64''' images in the html file. See
Note that the image files in the html are embedded '''Base64''' images in the html file. See
Line 128: Line 523:


== Knit button ==
== Knit button ==
* It calls rmarkdown::render()
* It calls '''rmarkdown::render()'''
* It does not count as an interactive session. But if we call '''rmarkdown::render()''' in an R session, it counts as an interactive session.
* R Markdown = knitr + Pandoc
* R Markdown = knitr + Pandoc
* rmarkdown::render () = knitr::knit() + a system() call to pandoc
* rmarkdown::render() = knitr::knit() + a system() call to pandoc
 
=== Exit knitting early ===
[https://bookdown.org/yihui/rmarkdown-cookbook/knit-exit.html Miscellaneous knitr Tricks]


== Pandoc's Markdown ==
== Pandoc's Markdown ==
'''pandoc''' will be installed automatically if we install '''tidyverse''' by using '''sudo apt install r-cran-tidyverse''' binary package.
Originally Pandoc is for html.
Originally Pandoc is for html.


Line 163: Line 564:
When you click the Knit button in RStudio, you will see the actual command that is executed.
When you click the Knit button in RStudio, you will see the actual command that is executed.


== Global options ==
=== pandoc included in RStudio ===
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.
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.
<pre>
 
```{r setup, include=FALSE}
[https://rmarkdown.rstudio.com/docs/reference/pandoc_available.html Check pandoc availability and version]
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
 
```
=== pander and rapport packages ===
</pre>
* https://rapporter.github.io/pander/
* https://cran.r-project.org/web/packages/rapport/index.html


== Examples/gallery ==
== Examples/gallery ==
Line 183: Line 585:


* [https://github.com/rstudio/rticles rticles] package
* [https://github.com/rstudio/rticles rticles] package
* [https://github.com/juba/rmdformats rmdformats] package
* [https://github.com/juba/rmdformats rmdformats] package. For each format, it also takes the option from [https://bookdown.org/yihui/rmarkdown/html-document.html html_document] such as '''code_folding''' (the default is 'none' so there is no way to control it on HTML).
** downcute: how to turn off black color
** '''robobook''': looks pretty. TOC is on LHS & can be closed like bookdown theme.
** material
** readthedown
** html_clean: the TOC is kind of small
** html_docco
<pre>
output:
  rmdformats::robobook:
    highlight: tango
    number_sections: true
    lightbox: true
    gallery: true
    code_folding: show
</pre>
 
== rmarkdown news, floating TOC ==
* [http://blog.rstudio.org/2016/03/21/rmarkdown-v0-9-5/ floating table of contents and tabbed sections]. An example from the  [http://bioconductor.org/packages/release/bioc/vignettes/tximport/inst/doc/tximport.html tximport vignette] with its [https://github.com/mikelove/tximport/blob/master/vignettes/tximport.Rmd source code].
 
=== How to create TOC from HTML/pdf? ===
Some examples that have no TOC in vignettes
* [https://cran.r-project.org/web/packages/stevedore/vignettes/stevedore.html stevedore]
 
Is there any web app that can generate the TOC?
 
== Useful tricks when including images ==
* [http://blog.revolutionanalytics.com/2017/06/rmarkdown-tricks.html Useful tricks when including images in Rmarkdown documents]
* [https://www.jumpingrivers.com/blog/knitr-rmarkdown-image-size/ Image sizes in an R markdown Document]
* [https://www.jumpingrivers.com/blog/knitr-include-graphics-external/ External Graphics with knitr]
* [https://blog.rstudio.com/2021/04/20/knitr-fig-alt/ Improved accessibility with image alt text]. knitr 1.32.
* [https://github.com/rstudio/rstudio/issues/4087 Markdown images won't show a preview in RMarkdown #4087] the image preview only shows up if you type in the code, not if you copy&paste the code in the .Rmd file.
 
== Space ==
=== Add horizontal space ===
Use '''&amp;nbsp;'''  See  https://stackoverflow.com/a/36831500
 
=== Add vertical space ===
See [https://stackoverflow.com/a/36831500 here].
 
== Figure ==
 
=== Avoid blank pages ===
Don't generate more than one plot in one chunk (learned by experience).
 
=== Reference a figure in latex ===
[https://alvinalexander.com/blog/post/latex/reference-figure-or-table-within-latex-document/ LaTeX examples: How to reference a figure or table]
 
An [https://github.com/sachsmc/signature-tutorial/blob/master/Documents/paper-revised-round2.Rmd rmarkdown example].
 
We can use this technique in rmarkdown. For example:
* In text: reference by \ref{fig1}
* In chunk:
<pre>
```{r myLabel, fig.cap="My caption\\label{fig1}"}
```
</pre>
 
=== Floating ===
See the solution [https://bookdown.org/yihui/rmarkdown-cookbook/figure-placement.html 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 [https://tex.stackexchange.com/a/2960 Why is \newpage ignored sometimes ?]
 
=== Figure caption ===
* 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 ===
[https://sebastiansauer.github.io/figure_sizing_knitr/ Different ways to set figure size in RMarkdown]
 
1. Define size in YAML header
<pre>
---
title: "My Document"
output: html_document:
fig_width: 6
fig_height: 4
---
</pre>
 
2. Global chunk
<pre>
knitr::opts_chunk$set(fig.width=12, fig.height=8)
</pre>
 
3. Chunk options
<pre>
{r fig1, fig.align = 'center', fig.height = 3, fig.width = 5}
 
{r fig3, fig.width = 5, fig.asp = .62}
 
{r fig4, out.width = '40%'}
</pre>
 
4. use knitr::include_graphics(); see [https://www.zevross.com/blog/2017/06/19/tips-and-tricks-for-working-with-images-and-figures-in-r-markdown-documents/ Tips and tricks for working with images and figures in R Markdown documents]
<pre>
```{r  out.width = "50%"}
include_graphics(img1_path)
```
</pre>
 
5. [https://bookdown.org/yihui/rmarkdown-cookbook/figure-size.html Control the size of plots/images]. It works.
<pre>
![A nice image.](foo/bar.png){width=50%}
</pre>
 
== 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.
 
{| class="wikitable"
! Package
! Example
! Comment
|-
| knitr
| 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.
 
[https://rfortherestofus.com/2019/11/how-to-make-beautiful-tables-in-r/ How to Make Beautiful Tables in R]: gt, kable + kableExtra, formattable, DT, reactable, flextable.
 
=== Table contest ===
* [https://www.rstudio.com/blog/winners-of-the-2021-table-contest/ Winners of the 2021 Table Contest],
** [https://rpubs.com/JackDavison/gt-openair Using {gt} and {openair} to Present Air Quality Data] can integrate clickable figures in tables. In order to reproduce the expandable figures as seen on the html, we need to download the gt.svg and Rmd file (table-contest-rmd.Rmd) and rebuild the html file.  The expandable figures/gallery feature relies on the [https://juba.github.io/rmdformats rmdformats] package (gallery: TRUE) and the [https://dimsemenov.com/plugins/magnific-popup/ lightbox] effect.
* [https://www.rstudio.com/blog/rstudio-community-table-gallery/ RStudio Community Table Gallery]
 
=== knitr::kable ===
* https://bookdown.org/yihui/rmarkdown-cookbook/kable.html
* [https://www.r-bloggers.com/2019/11/quicker-knitr-kables-in-rstudio-notebook/ Quicker knitr kables in RStudio notebook]
 
=== From scratch ===
[https://tomaztsql.wordpress.com/2023/03/26/little-useless-useful-r-functions-transforming-dataframe-to-markdown-table/ Little useless-useful R functions – Transforming dataframe to markdown table]
 
=== 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 (images in tables) ===
https://cran.r-project.org/web/packages/kableExtra/index.html, [https://cran.r-project.org/web/packages/kableExtra/vignettes/awesome_table_in_html.html#Insert_Images_into_Columns Insert Images into Columns], [https://stackoverflow.com/a/25107783 Add an image to a table-like output in R]
 
This example [https://rpubs.com/JackDavison/gt-openair Using gt, gtExtras and openair to present air quality monitoring data] can insert <s>clickable</s> '''''expandable''''' figures in tables. The example uses '''gt''' and [https://jthomasmock.github.io/gtExtras/ gtExtras] package.
 
Example:
<pre>
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"))
</pre>
[https://stackoverflow.com/a/44555902 Alternate row color with knitr:kable in R Markdown]
<pre>
knitr::kable(mtcars, "html") %>%
  kable_styling("striped")
</pre>
[https://stackoverflow.com/a/48512819 kable kableExtra, Cells with hyperlinks]
 
[https://stackoverflow.com/a/44888475 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 [https://leekgroup.github.io/postpi/doc/vignettes.html vignette] from postpi.
 
=== gt and gtExtras ===
* https://cran.r-project.org/web/packages/gt/index.html. Currently, gt supports the HTML, LaTeX, and RTF output formats.
* [https://blog.rstudio.com/2020/04/08/great-looking-tables-gt-0-2/ Great Looking Tables: gt (v0.2)]
* [https://rstats-tips.net/2021/11/27/custom-formats-in-gt-tables/ Custom Formats in gt Tables]
* [https://gt.rstudio.com/reference/cols_width.html cols_width()] Set the widths of columns
* [https://stackoverflow.com/a/64129080 R gt_table adjust row height]
* [https://datavizpyr.com/how-to-make-beautiful-tables-with-gtextras/ How to Make Beautiful Tables with gtExtras]
* [https://themockup.blog/posts/2022-06-13-gtextras-cran/ Beautiful tables in R with gtExtras] 2022-06-13
* [https://www.rstudio.com/blog/all-new-things-in-gt-0-7-0/ All the New Things in {gt} 0.7.0]
 
Include images in tables
* [https://gt.rstudio.com/reference/web_image.html ?web_image]. Simple use of '''fn''' in text_transform(locations, fn).
* [https://vincentarelbundock.github.io/modelsummary/articles/appearance.html Customizing the look of your table]. '''web_image()''' and '''text_transform()''' were used. Unfortunately it does not have different images.
 
Include plots in tables (without using '''gtExtras''' package)
* ?'''gt::ggplot_image''' for a one-row table example
* [https://github.com/rstudio/gt/issues/152 adding ggplot graphs in every row of a table #152]. Search '''text_transform''' and find the last match for a multi-row table example. Impressive!
 
=== gtsummary ===
* [https://cran.r-project.org/web/packages/gtsummary/index.html gtsummary]
* [https://education.rstudio.com/blog/2020/07/gtsummary/?s=09 Presentation-Ready Summary Tables with 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:
<pre>
print(xtable(x, digits = 4,
            caption = "deltaC \\label{tb3}"),
      comment = FALSE,
      size="\\fontsize{7pt}{8pt}\\selectfont",
      floating = TRUE,
      latex.environments="center")
</pre>
 
xtableList() can create a list of tables; see [https://cran.r-project.org/web/packages/xtable/vignettes/listOfTablesGallery.pdf#page=7 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.
<pre>
---
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)
```
</pre>
 
The following example is to give alternative colors on rows. Note the option '''header-includes''' and '''tables''' in YAML section
<pre>
---
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))
```
</pre>


== rmarkdown news ==
Tips
* [http://blog.rstudio.org/2016/03/21/rmarkdown-v0-9-5/ floating table of contents and tabbed sections]
* [https://stackoverflow.com/a/33994541 Changing the font size of table using print.xtable] (\fontsize{<size>}{<line space>})
:<pre>print(xt, size="\\fontsize{9pt}{10pt}\\selectfont") </pre>
* [https://stackoverflow.com/a/17499349 Avoiding floating xtable in knitr hides the caption of the table]. Only floating has captions.
:<pre>print(xtable(.., caption = " "), floating = TRUE) </pre>
 
=== Gmisc: create Table 1 used in medical articles ===
https://cran.r-project.org/web/packages/Gmisc/index.html
 
=== tableone ===
<ul>
<li>https://cran.r-project.org/web/packages/tableone/
<li>[https://www.r-bloggers.com/2016/02/table-1-and-the-characteristics-of-study-population/ Table 1 and the Characteristics of Study Population]
* The demo data can be downloaded
* [https://www.rdocumentation.org/packages/tableone/versions/0.13.2/topics/CreateTableOne ?CreateTableOne]
* It can be verified that if the interested variable is continuous, we can use '''t.test'''(, var.equal = T)$p.value to obtain p-values. Or use the '''aov''' function: <nowiki>summary(aov(BMI ~ Gender, dt))[[1]][["Pr(>F)"]][1] </nowiki> .
* If the interested variable is discrete, it seems it uses '''chi.square'''(var1, var2)$p.value to obtained p-values. It seems even the cells have small number of observations, it still use chisq-test. We can however obtain the Fisher's exact test p-values in the print() method.
<pre>
R> with(dt[1:15,], table(Smoking, Gender))
      Gender
Smoking Female Male
    No      5    6
    Yes      2    2
 
R> with(dt[1:15,], chisq.test(table(Education, Gender))$p.value)
[1] 0.6691241
Warning message:
In chisq.test(table(Education, Gender)) :
  Chi-squared approximation may be incorrect
R> with(dt[1:15,], fisher.test(table(Education, Gender))$p.value)
[1] 0.8135198
 
R> CreateTableOne(c("Smoking", "Education"), dt[1:15,], catVars, strata = c("Gender"))
                  Stratified by Gender
                    Female    Male      p      test
  n                7        8                   
  Smoking = Yes (%) 2 (28.6)  2 (25.0)  1.000   
  Education (%)                          0.669   
    High          2 (28.6)  4 (50.0)           
    Low            3 (42.9)  2 (25.0)           
    Medium        2 (28.6)  2 (25.0) 
 
R> print(CreateTableOne(c("Smoking", "Education"), dt[1:15,], catVars, strata = c("Gender")),
        exact = c("Smoking", "Education"))
                  Stratified by Gender
                    Female    Male      p      test
  n                7        8                   
  Smoking = Yes (%) 2 (28.6)  2 (25.0)  1.000 exact
  Education (%)                          0.814 exact
    High          2 (28.6)  4 (50.0)             
    Low            3 (42.9)  2 (25.0)             
    Medium        2 (28.6)  2 (25.0) 
 
# The print() result is a table so we can output it using write.table()
R> print(CreateTableOne(c("Smoking", "Education"), dt[1:15,], catVars, strata = c("Gender")),
        exact = c("Smoking", "Education")) %>% dim()
# [1] 6 4
</pre>
<li>[https://www.jianshu.com/p/40b7db26e7e6 简书]
</ul>
 
=== printr ===
https://cran.r-project.org/web/packages/printr/index.html
 
=== DT ===
https://cran.r-project.org/web/packages/DT/index.html
 
=== flextable ===
<ul>
<li>[https://cran.r-project.org/web/packages/flextable/index.html flextable]. Create pretty tables for 'HTML', 'Microsoft Word' and 'Microsoft PowerPoint' documents.
<syntaxhighlight lang='r'>
library(flextable)
df <- data.frame(a=letters[1:3], b=rnorm(3), c=letters[4:6])
df <- flextable(df)
df <- add_header_row(ft, c("Merge Header", "Header 3"))
df <- align(ft, align = 'center', part = 'header')
df <- align(ft, j=1:3, align='center', part='body')
df <- set_caption(ft, caption = 'my table caption')
df <- add_footer_lines(df, values = c("blah 1", "blah 2"))
# Output to Word
library(officer)
doc <- read_docx()
doc <- body_add_flextable(doc, ft)
print(doc, target = "output.docx")
</syntaxhighlight>
<li>[https://ardata-fr.github.io/flextable-book/index.html ebook] with [https://ardata-fr.github.io/flextable-book/static/pdf/cheat_sheet_flextable.pdf cheat sheet]. Search "library" to find examples, especially the chapter [https://ardata-fr.github.io/flextable-book/as-flextable.html Chapter 9 Transform objects into flextable]  
<pre>
---
title: "Untitled2"
output:
  word_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
```
 
```{r echo=FALSE}
ft <- flextable(head(mtcars))
ft <- autofit(ft)
ft
```
 
A new chunk
```{r results='asis', echo = FALSE}
ft <- set_caption(ft, caption = "Table 1: New York Air Quality Measurements")
theme_zebra(ft) # ft <- theme_zebra(ft)
```
</pre>
</ul>


== Useful tricks when including images in Rmarkdown documents ==
=== sparkTable ===
http://blog.revolutionanalytics.com/2017/06/rmarkdown-tricks.html
https://cran.r-project.org/web/packages/sparkTable/


== [https://cran.r-project.org/web/packages/sparkTable/ sparkTable] ==
[https://journal.r-project.org/archive/2015-1/templ-kowarik-meindl.pdf sparkTable: Generating Graphical Tables for Websites and Documents with R]. It depends on the Rglpk package which requires the [https://stackoverflow.com/questions/25114771/glpk-no-such-file-or-directory-error-when-trying-to-install-r-package glpk library]. However, sparkTable is not maintained anymore.
[https://journal.r-project.org/archive/2015-1/templ-kowarik-meindl.pdf sparkTable: Generating Graphical Tables for Websites and Documents with R]. It depends on the Rglpk package which requires the [https://stackoverflow.com/questions/25114771/glpk-no-such-file-or-directory-error-when-trying-to-install-r-package glpk library]. However, sparkTable is not maintained anymore.


[https://ropensci.org/blog/2017/07/11/skimr/ skimr for useful and tidy summary statistics] is new and provide a histogram next to each variable.
[https://ropensci.org/blog/2017/07/11/skimr/ skimr for useful and tidy summary statistics] is new and provide a histogram next to each variable.
=== mmtable2 ===
[https://github.com/ianmoran11/mmtable2?s=09 mmtable2] allows you to create and combine tables with a ggplot2/patchwork syntax.
=== tinytable ===
https://vincentarelbundock.github.io/tinytable/


== How to align table and plot in rmarkdown html_document ==
== How to align table and plot in rmarkdown html_document ==
https://stackoverflow.com/a/54359010
https://stackoverflow.com/a/54359010
== Math equations ==
* [https://rpruim.github.io/s341/S19/from-class/MathinRmd.html Mathematics in R Markdown] R Pruim
* [https://rmd4sci.njtierney.com/math RMarkdown for Scientists]
* [https://github.com/datalorax/equatiomatic equationmatic] package


== RMarkdown Template that Manages Academic Affiliations ==
== RMarkdown Template that Manages Academic Affiliations ==
Line 204: Line 986:
== Converting Rmarkdown to F1000Research LaTeX Format ==
== Converting Rmarkdown to F1000Research LaTeX Format ==
[https://www.bioconductor.org/packages/release/bioc/html/BiocWorkflowTools.html BiocWorkflowTools] package and [https://f1000research.com/articles/7-431/ paper]
[https://www.bioconductor.org/packages/release/bioc/html/BiocWorkflowTools.html BiocWorkflowTools] package and [https://f1000research.com/articles/7-431/ paper]
== Convert markdown to HTML ==
* [https://www.lifehack.org/articles/productivity/use-markdown-for-easy-web-writing.html Use Markdown For Easy Web Writing] and [https://daringfireball.net/projects/markdown/basics markdown 1.0.1] (Dec 2004) perl script.
* https://www.markdownguide.org/tools/ and [https://www.markdownguide.org/getting-started An overview of Markdown, how it works, and what you can do with it].
* [https://github.com/mundimark/awesome-markdown Awesome markdown]
== Convert to a markdown document ==
[https://rmarkdown.rstudio.com/docs/reference/md_document.html md_document()]


== Internal links ==
== Internal links ==
Line 214: Line 1,004:


Another easier way to use the [https://pandoc.org/MANUAL.html#heading-identifiers Heading identifiers] as described in pandoc. In the header use "# section 1{#s1}" and in the toc or paragraph use '[my section name](#s1)".
Another easier way to use the [https://pandoc.org/MANUAL.html#heading-identifiers 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 [https://bookdown.org/yihui/rmarkdown/pdf-document.html R Markdown: The Definitive Guide].
<pre>
urlcolor: blue
</pre>


== Colored text ==
== Colored text ==
Line 226: Line 1,022:
* [https://amstat.tandfonline.com/doi/full/10.1080/00031305.2017.1397549#.XPZobstKhNk Infrastructure and Tools for Teaching Computing Throughout the Statistical Curriculum] Mine Çetinkaya-Rundel & Colin Rundel 2017
* [https://amstat.tandfonline.com/doi/full/10.1080/00031305.2017.1397549#.XPZobstKhNk Infrastructure and Tools for Teaching Computing Throughout the Statistical Curriculum] Mine Çetinkaya-Rundel & Colin Rundel 2017
* [https://jozefhajnala.gitlab.io/r/r910-rmarkdown-reproducibility/ R Markdown: 3 sources of reproducibility issues and options how to tackle them]
* [https://jozefhajnala.gitlab.io/r/r910-rmarkdown-reproducibility/ R Markdown: 3 sources of reproducibility issues and options how to tackle them]
* [https://docs.google.com/presentation/d/1qFUgYpHDmy68-oaaWS196zrsACtiyj2C65GZAjDDyNs/ Rmd Workflows Make New Statistical Methods Accessible to Biomedical Researchers] Michael Love 2020
* [https://bioconductor.org/packages/release/bioc/html/easyreporting.html easyreporting] package. [https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0244122 Easyreporting simplifies the implementation of Reproducible Research layers in R software] (paper)
* [https://cran.r-project.org/web/packages/reprex/index.html reprex]: Prepare Reproducible Example Code via the Clipboard
== 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).


== Interactive document: Shiny ==
[https://stackoverflow.com/a/59691029 R built in Web server]
<pre>
writeLines("<h1>Hi</H1>", "index.html")
 
library(servr)
servr::httd()
</pre>
 
=== crosstalk - generating a plain HTML document with no need for a Shiny server ===
[https://github.com/jthomasmock/penguin-project/blob/master/02-data-products/crosstalk.rmd Rmarkdown taxonomy] and the video by Thomas Mock
 
=== Interactive document: Shiny ===
See [https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf R Markdown Cheat Sheet].
See [https://www.rstudio.com/wp-content/uploads/2016/03/rmarkdown-cheatsheet-2.0.pdf R Markdown Cheat Sheet].


Line 233: Line 1,049:
* I can't run "Build" anymore. An error will come out: Error in numericInput("n", "How many cars?", 5) :  could not find function "numericInput".
* 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).
* 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).
[https://youtu.be/fgOw78Sp8TA How to boost R Markdown interactivity with runtime Shiny] and the [https://www.infoworld.com/article/3391381/turn-an-r-markdown-document-into-an-interactive-experience.html article]
=== Tooltip ===
[https://cran.r-project.org/web/packages/tippy/index.html tippy]: Add Tooltips to 'R markdown' Documents or 'Shiny' Apps
=== Theme, CSS ===
* [https://community.rstudio.com/t/in-rstudio-rmarkdown-how-to-add-new-html-themes-from-bootswatch/15791 how to add new html themes from Bootswatch?]
* [https://m-clark.github.io/Introduction-to-Rmarkdown/customization.html#html-css HTML & CSS]
* [https://sourcethemes.com/academic/ Academic] theme
* [https://www.garrickadenbuie.com/blog/r-colors-css/?s=09 R Colors in CSS for R Markdown HTML Documents]
* [https://emilyriederer.netlify.app/post/snow/ How to Make R Markdown Snow]
=== Scrollable code/output ===
* [https://bookdown.org/yihui/rmarkdown-cookbook/html-scroll.html 7.4 Scrollable code blocks],
** [https://github.com/rstudio/rmarkdown-cookbook/issues/75 Scrollable code output #75] 2018
** [https://stackoverflow.com/a/67942965 Vertically scrollable code with RStudio and xaringan]
** [https://stackoverflow.com/questions/50919104/horizontally-scrollable-output-on-xaringan-slides Horizontally scrollable output on xaringan slides]
=== Download button/embed files ===
* [https://cran.r-project.org/web/packages/downloadthis/index.html downloadthis]: Implement Download Buttons in 'rmarkdown'
* [https://bookdown.org/yihui/rmarkdown-cookbook/html-css.html 7.1 Apply custom CSS]
<pre>
```{css echo = F}
.button_green {
  background-color: #4CAF50;
  font-size: 14px;
}
```
```{r}
library(downloadthis)
# Use the class parameter in download_this to apply your custom class
mtcars %>% download_this(
  output_name = "mtcars",
  output_extension = ".xlsx",
  button_label = "Download datasets as xlsx",
  button_type = "warning", 
  has_icon = TRUE,
  icon = "fa fa-save",
  class = "button_green" # Add your custom class here
)
```
</pre>
<syntaxhighlight lang='r'>
library(download_this)
# Custom CSS for the button
button_css <- "
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 4px;
"
# Create the download button
download_this(mtcars, style = button_css,
              str_anvlopt = "Download Data",
              output_name = "my_data.csv")
</syntaxhighlight>
== Dropdown menu ==
<ul>
<li>plotly package. [https://plotly.com/r/dropdowns/  Dropdown Events in R]
* [https://statisticsglobe.com/dropdown-menu-plotly-graph-r Create Dropdown Menu in plotly Graph in R (Example)]
* [https://sylwiamielnicka.com/blog/advanced-plotly-sliders-and-dropdown-menus/ Advanced Plotly – Sliders and dropdown menus]
<li>[http://walkerke.github.io/2016/12/rmd-dropdowns/ Dropdown menus in R Markdown with bsselectR]
<pre>
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")
</pre>
</ul>


== Automatic document production with R ==
== Automatic document production with R ==
Line 244: Line 1,145:
* http://dirk.eddelbuettel.com/code/pinp.html
* http://dirk.eddelbuettel.com/code/pinp.html


== Gmisc: create Table 1 used in medical articles ==
== Presentation ==
https://cran.r-project.org/web/packages/Gmisc/index.html
* [https://rmarkdown.rstudio.com/lesson-11.html Slide Presentations]
* https://bookdown.org/yihui/rmarkdown/presentations.html
* https://codingclubuc3m.rbind.io/post/2019-09-24/
 
=== 4 output format ===
* HTML (ioslides) you can print ioslides to PDF with Chrome. [https://bookdown.org/yihui/rmarkdown/ioslides-presentation.html 4.1 ioslides presentation], [https://garrettgman.github.io/rmarkdown/ioslides_presentation_format.html Presentations with ioslides Overview]
* HTML (Slidy) you can print ioslides to PDF with Chrome
* PDF (beamer) requires TeX
* PowerPoint
 
=== font size ===
* [https://bookdown.org/yihui/rmarkdown/slidy-presentation.html Slidy presentation]
* [https://github.com/rstudio/rmarkdown-book/issues/30 Adjusting font-size in ioslides using R Markdown]
 
=== Presenter mode ===
* [https://bookdown.org/yihui/rmarkdown/ioslides-presentation.html#presenter-mode ioslides presentation]
*# Follow [https://bookdown.org/yihui/rmarkdown/ioslides-presentation.html#presenter-mode this] to add notes to your Rmd file.
*# Add "?presentme=true" to the end of the URL of HTML file in your browser. This will popup another window/tab and this is the presenter will control and see all the notes. The original window/tab will be used by viewers. On my Chrome browser, I need to allow a popup from this location. Firefox is easy to allow popup. [https://support.apple.com/guide/safari/block-pop-ups-sfri40696/mac Safari browser] is difficult.
*# Use the "p" key to change the mode to [https://bookdown.org/yihui/rmarkdown/ioslides-presentation.html#display-modes Presenter mode]. You will see the notes at the bottom of the screen. If the notes are very long, a slider will be shown on the RHS.
* [https://bookdown.org/yihui/rmarkdown/xaringan-format.html#xaringan-notes xaringan Presentations]
** [https://bookdown.org/yihui/rmarkdown/xaringan-format.html#xaringan-notes Presenter notes]
 
=== PowerPoint slides ===
* https://rstudio.com/resources/webinars/make-powerpoint-presentations-with-r-markdown/
* https://bookdown.org/yihui/rmarkdown/powerpoint-presentation.html
* [https://support.rstudio.com/hc/en-us/articles/360004672913-Rendering-PowerPoint-presentations-with-RStudio#templates Rendering PowerPoint Presentations with RStudio]
* [https://appsilon.com/r-markdown-powerpoint-presentation/ How To Make A PowerPoint Presentation Using R Markdown] including How to Style Your R Markdown PowerPoint Presentation.
* [https://www.infoworld.com/article/3648458/how-to-create-powerpoint-slides-from-r.html How to create PowerPoint slides from R]


== Tips ==
=== xaringan: presentation ===
* https://github.com/yihui/xaringan.
** [https://slides.yihui.org/xaringan/#1 HTML Vignette/Slides].
** [https://bookdown.org/yihui/rmarkdown/xaringan.html Chapter 7 xaringan Presentations]
* [https://github.com/jtleek/jhsph-irb-research-plan-template/ R markdown template for writing these research plans]
* [https://jozefhajnala.gitlab.io/r/r909-rmarkdown-tips/ Create R Markdown reports and presentations even better with these 3 practical tips]
* [https://jozefhajnala.gitlab.io/r/r909-rmarkdown-tips/ 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(). [https://yihui.name/en/2019/02/ultimate-inf-mr/ The Ultimate Infinite Moon Reader for xaringan Slides].
** Live preview of R Markdown files with xaringan’s infinite_moon_reader(). [https://yihui.name/en/2019/02/ultimate-inf-mr/ The Ultimate Infinite Moon Reader for xaringan Slides].
** The RStudio addins is available in the Addins dropdown menu under the XARINGAN (need to scroll down to the bottom since the addins are sorted alphabetically by package names).
** Creating beautiful, multi format reports directly from R scripts
** Creating beautiful, multi format reports directly from R scripts
** Advanced chunk options with useful effects
** Advanced chunk options with useful effects
 
* [https://codingclubuc3m.rbind.io/post/2019-09-24/ Mastering R presentations]
== Cache ==
* Examples:
* If my markdown file is called '''abc.Rmd''', then two cache directories ('''abc_cache''' & '''abc_files''') will be created by default. See [https://bookdown.org/yihui/rmarkdown/rmarkdown-site.html rmarkdown’s site generator].
** [https://github.com/wlandau/learndrake/tree/master/inst/slides Reproducible workflows at scale with drake]
** *_files: figures.
* [https://pkg.garrickadenbuie.com/xaringanExtra/#/ xaringanExtra], [https://youtu.be/vZMuu77ocMY useR! 2020] youtube video.
** *_cache: *.RData, *.rdb, __packages.
* [https://silvia.rbind.io/2021-03-16-deploying-xaringan-slides/?s=09 Deploying xaringan Slides: A Ten-Step GitHub Pages Workflow]
** When knitting failed due to my error, I will rm these two directories, fix my code and knit again.
* [https://youtu.be/3n9nASHg9gc Slides with Rmarkdown: xaringan (R case study, 2021)], [https://www.youtube.com/watch?v=3n9nASHg9gc&t=1888s Speaker/presenter notes]
* [https://community.rstudio.com/t/caching-chunks-in-rstudio-does-not-work/9040/2 Cache not work]
** [https://bookdown.org/yihui/rmarkdown/xaringan-key.html Keyboard shortcuts]. '''c''' & '''p'''.
* [https://yihui.name/knitr/demo/cache/ Examples] <syntaxhighlight lang='rsplus'>
** In the presenter mode, there is a timer on the top right corer. It'll start with 0:00:00.
> system.time(rmarkdown::render("~/Downloads/tmp.Rmd")) # first time
** You may press '''t''' to restart the timer at any time.
...
** Flash cards - [https://quizlet.com Quizlet]
Output created: tmp.html
* (Video) [https://youtu.be/5ZeA-E-XIE0 rmarkdown: Make interactive PowerPoint slide presentations in R]
  user  system elapsed
* Live preview/View changes in real-time: infinite moon reader
  3.123  0.108  5.426
** [http://jenrichmond.rbind.io/post/infinite-moon-reader/ How to use infinite moon reader]. Open a rmd document that you are working on and in the console call xaringan::inf_mr() i.e. infinite moon reader.
# It will create two directories: tmp_files, tmp_cache
** [https://bookdown.org/yihui/rmarkdown/xaringan-preview.html 7.4 Build and preview slides]
 
* [https://www.keanarichards.com/2021/03/19/making-a-presentation-in-r/ Making a Presentation in R: Getting started]
> system.time(rmarkdown::render("~/Downloads/tmp.Rmd")) # Second time
* Creating PDF file: [https://www.garrickadenbuie.com/blog/print-xaringan-chromote/ Printing xaringan slides]: Use "Print" function in the browser
...
* [https://stackoverflow.com/a/52663290 Change the background color of a code chunk]
Output created: tmp.html
:<syntaxhighlight lang='css'>
  user  system elapsed
.code-bg-gray .remark-code, .code-bg-gray .remark-code * {
  0.239  0.019  0.317
background-color:#e1e7e9!important;
}
</syntaxhighlight>In the YAML section, change to '''css: [default, metropolis, metropolis-fonts, custom.css]'''. In the code chunk, follow the instruction there by wrapping your code chunk with '''.code-bg-gray[ ] '''
* You can use HTML elements directly in an R Markdown file. R Markdown allows you to mix Markdown, R code, and HTML code in the same document1. You can use HTML tags to format your text, create tables, insert images, and more. For example, if I want to highlight a certain line in a code block, we can use the '''span''' element with an inline CSS to set the background color. See [https://bookdown.org/yihui/rmarkdown-cookbook/html-hardcore.html 7.15 For hardcore HTML users (*)] from [https://bookdown.org/yihui/rmarkdown-cookbook/ R Markdown Cookbook].
:<syntaxhighlight lang='html'>
<pre><code>
This is a code block
<span style="background-color:yellow">This line is highlighted</span>
</code></pre>
</syntaxhighlight>
</syntaxhighlight>
 
* Change the font size of a table
=== read knitr/Rmd cache ===
[https://stackoverflow.com/a/47772063 read knitr/Rmd cache in interactive session?]. Use [https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/lazyLoad lazyLoad()] function without specifying any extension.
<pre>
<pre>
lazyLoad("unnamed-chunk-1_3c3ad57469a118cd9c584c8d941d2c09")
<style>
</pre>
# Slide with Table


== RStudio ==
table {
RStudio is the best editor.
  font-size: 16px; /* Set the desired font size */
}
</style>


Markdown has two drawbacks: 1. it does not support TOC natively. 2. RStudio cannot show headers in the editor.
| Task                            | Docker Command              |
 
|---------------------------------|-----------------------------|
Therefore, use rmarkdown format instead of markdown.
| Build an image                  | `docker build`              |
 
| Run a container                | `docker run`                |
== Writing a R book and self-publishing it in Amazon ==
| Pull an image from a registry  | `docker pull`              |
* https://msperlin.github.io/2017-02-16-Writing-a-book/
</pre>
* [https://community.rstudio.com/t/announcing-winners-of-the-1st-bookdown-contest/16394?utm_content=buffer909fb&utm_medium=social&utm_source=twitter&utm_campaign=buffer Winner of the 1st Bookdown contest]
 
== Create professional reports from R scripts, with custom styles ==
[https://jozefhajnala.gitlab.io/r/r913-spin-with-style/ How to create professional reports from R scripts, with custom styles]
 
== Publish R results ==
[https://jozefhajnala.gitlab.io/r/r907-christmas-praise/ 5 amazing free tools that can help with publishing R results and blogging]


== Scheduling R Markdown Reports via Email ==
=== Create presentation file (beamer) ===
http://www.analyticsforfun.com/2016/01/scheduling-r-markdown-reports-via-email.html
 
== Create presentation file (beamer) ==
* http://rmarkdown.rstudio.com/beamer_presentation_format.html
* http://rmarkdown.rstudio.com/beamer_presentation_format.html
* http://www.theresearchkitchen.com/archives/1017 (markdown and presentation files)
* http://www.theresearchkitchen.com/archives/1017 (markdown and presentation files)
Line 351: Line 1,282:
```
```
</pre>
</pre>
== 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 [https://bookdown.org/yihui/rmarkdown/rmarkdown-site.html 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.
* [https://community.rstudio.com/t/caching-chunks-in-rstudio-does-not-work/9040/2 Cache not work]
* [https://yihui.name/knitr/demo/cache/ Examples] <syntaxhighlight lang='rsplus'>
> 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
</syntaxhighlight>
=== 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.
<pre>
```{r chunk1}
x <- 1
```
```{r chunk2, echo=FALSE}
print(x)
```
</pre>
=== cache.extra option: dependence ===
* [https://bookdown.org/yihui/rmarkdown-cookbook/cache.html rmarkdown cookbook > Chunk > Cache]. Some examples
** cache.extra = tools::md5sum('my-precious.csv')
** cache.extra = getRversion()
** cache.extra = Sys.Date()
** <nowiki>cache.extra = Sys.info()[['sysname']] </nowiki>
* We can use our own way to replace the cache design in knitr; see a simple example in the [https://bookdown.org/yihui/rmarkdown-cookbook/cache.html Cache] subsection.
* [https://stackoverflow.com/a/33272773 How to make code chunks depend on all previous chunks in knitr/rmarkdown?]
* '''Object used in cache.extra will be printed on screen.''' So if a complex object is used in '''cache.extra''', it will take a long time to show the object (thus build the output). So it is smart to only use a simple part of a complex object in "cache.extra".
* Even I use "eval=FALSE", the object in cache.extra is still printed out on screen (or depends on the order of the parameters?).
<pre>
---
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)
```
</pre>
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 [https://stackoverflow.com/a/33272773 How to make code chunks depend on all previous chunks in knitr/rmarkdown?] and [https://yihui.org/knitr/options/#code-chunk 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 ===
[https://stackoverflow.com/a/47772063 read knitr/Rmd cache in interactive session?]. Use [https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/lazyLoad lazyLoad()] function without specifying any extension.
<pre>
lazyLoad("unnamed-chunk-1_3c3ad57469a118cd9c584c8d941d2c09")
</pre>
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 [https://stackoverflow.com/a/27054476 Pipe in magrittr package is not working for function load()].
::<syntaxhighlight lang='rsplus'>
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())
}
</syntaxhighlight>
=== _cache and _files folders ===
Files in these two folders do not work when I copy these two folders generated from macOS to Linux.
If I manually delete files under _files and re-render, it will give me an error that xxx.pdf cannot be found.
If I want to re-run a chunk without changing the Rmd file, we can just delete files (*.rdx, *.rdb, *.RData) related to a chunk in '''_cache/latex''' folder. There is no need to delete files in _files folder since files there will be re-generated too.
== 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 ==
* https://msperlin.github.io/2017-02-16-Writing-a-book/
* [https://community.rstudio.com/t/announcing-winners-of-the-1st-bookdown-contest/16394?utm_content=buffer909fb&utm_medium=social&utm_source=twitter&utm_campaign=buffer Winner of the 1st Bookdown contest]
== Create professional reports from R scripts, with custom styles ==
[https://jozefhajnala.gitlab.io/r/r913-spin-with-style/ How to create professional reports from R scripts, with custom styles]
=== reporter ===
[https://reporter.r-sassy.org/ reporter] package
== Publish R results ==
* [https://jozefhajnala.gitlab.io/r/r907-christmas-praise/ 5 amazing free tools that can help with publishing R results and blogging]
* [https://rpubs.com/about/getting-started Getting Started with RPubs]
== Scheduling R Markdown Reports via Email ==
http://www.analyticsforfun.com/2016/01/scheduling-r-markdown-reports-via-email.html
== Scheduling Rmarkdown files on Windows ==
[https://www.johnmackintosh.net/blog/2021-11-01-scheduling/ Scheduling Rmarkdown files on Windows - your foolproof guide]


== R notebook vs R markdown in RStudio ==
== R notebook vs R markdown in RStudio ==
[https://stackoverflow.com/questions/43820483/difference-between-r-markdown-and-r-notebook Difference between R MarkDown and R NoteBook]
* [https://rmarkdown.rstudio.com/lesson-10.html Notebooks]
* [https://stackoverflow.com/questions/43820483/difference-between-r-markdown-and-r-notebook Difference between R MarkDown and R NoteBook]
* [http://pklab.med.harvard.edu/peterk/review2020/euclid/ An Example]


There is no coding difference. The difference is in the rendering. The file extension is the same.
There is no coding difference. The difference is in the rendering. The file extension is the same.
Line 359: Line 1,427:
R notebook
R notebook
* It adds '''html_notebook''' in the output option in the header.  
* 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.
* 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.  
* It also refreshes the preview every time you save.  
* However in that preview you don't have the code output (no figures, no tables..)  
* However in that preview you don't have the code output (no figures, no tables..)  
Line 366: Line 1,434:


== Table creating packages ==
== Table creating packages ==
[https://cran.r-project.org/web/packages/stargazer/index.html 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
* [https://cran.r-project.org/web/packages/stargazer/index.html 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
* [http://skranz.github.io//r/2021/01/18/EmpEconB.html Empirical economics with r (part b): confounders, proxies and sources of exogenous variations]


== Graphics Output in LaTeX Format ==
== Graphics Output in LaTeX Format ==
Line 375: Line 1,444:
https://stackoverflow.com/questions/25849814/rstudio-rmarkdown-both-portrait-and-landscape-layout-in-a-single-pdf
https://stackoverflow.com/questions/25849814/rstudio-rmarkdown-both-portrait-and-landscape-layout-in-a-single-pdf


== Break a long document ==
== Break a long document and child option ==
[https://martinctc.github.io/blog/first-world-problems-very-long-rmarkdown-documents/ First World Problems: Very long RMarkdown documents]
* [https://bookdown.org/yihui/rmarkdown-cookbook/child-document.html Child documents]
* [https://martinctc.github.io/blog/first-world-problems-very-long-rmarkdown-documents/ First World Problems: Very long RMarkdown documents]
* [https://github.com/juba/rmdformats/blob/master/vignettes/examples/robobook.Rmd An example] of using knitr::knit_child()
 
== Programmatically create new headings and outputs ==
[https://scienceloft.com/technical/programmatically-create-new-headings-and-outputs-in-rmarkdown/ Programmatically create new headings and outputs in Rmarkdown]
 
== Request an early exit ==
https://stackoverflow.com/a/33711413


== Bibliographies ==
== Bibliographies ==
[https://www.datasurg.net/2019/05/09/bibliographies-in-rstudio-markdown-are-difficult-heres-how-to-make-it-easy/ Bibliographies in RStudio Markdown are difficult – here’s how to make it easy]
* [https://www.datasurg.net/2019/05/09/bibliographies-in-rstudio-markdown-are-difficult-heres-how-to-make-it-easy/ Bibliographies in RStudio Markdown are difficult – here’s how to make it easy]
* [https://ropensci.org/technotes/2020/05/07/rmd-citations/ A Roundup of R Tools for Handling BibTeX] 2020
 
== Docker ==
[https://jlintusaari.github.io/2018/07/how-to-compile-rmarkdown-documents-using-docker/ How to compile R Markdown documents using Docker]
 
== lazy load plots ==
[https://github.com/hafen/lazyrmd lazyrmd] package
== Emoji ==
* https://en.wikipedia.org/wiki/Emoji
* [https://github.com/hadley/emo emo] package.
* An example from [https://raw.githubusercontent.com/lcolladotor/biocthis/master/vignettes/biocthis.Rmd biocthis] vignette
* [https://meta.stackexchange.com/questions/260622/support-emoji-in-markdown Support Emoji in Markdown]
* [https://www.howtogeek.com/449858/how-new-emoji-are-born-and-how-to-propose-your-own/ How New Emoji Are Born (and How to Propose Your Own)]
* [https://www.makeuseof.com/how-to-make-emoji/ How to Make Your Own Emoji]
 
== Citation ==
[https://github.com/cboettig/knitcitations knitcitations]. knitcitations is an R package designed to add dynamic citations to dynamic documents created with Yihui's knitr package.
 
== Video ==
[https://journal.r-project.org/archive/2020/RJ-2020-023/RJ-2020-023.pdf ari: The Automated R Instructor]
 
== Render an R script ==
[https://happygitwithr.com/r-test-drive.html 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").


=  [https://bookdown.org/ bookdown.org] =
=  [https://bookdown.org/ bookdown.org] =
Line 386: Line 1,492:
* [https://blog.rstudio.org/2016/12/02/announcing-bookdown/ Announce bookdown]
* [https://blog.rstudio.org/2016/12/02/announcing-bookdown/ Announce bookdown]
* [https://bookdown.org/yihui/bookdown/ bookdown package]: Authoring Books and Technical Documents with R Markdown. We can download [https://github.com/rstudio/bookdown-demo bookdown-demo] to write our book.
* [https://bookdown.org/yihui/bookdown/ bookdown package]: Authoring Books and Technical Documents with R Markdown. We can download [https://github.com/rstudio/bookdown-demo bookdown-demo] to write our book.
* R Graphics Cookbook. Check Makefile and [https://github.com/wch/rgcookbook/blob/master/.github/workflows/build-book.yaml workflow]. 8GB is still too tight. Error: libpng warning: iCCP: profile 'icc': 'RGB ': RGB color space not permitted on grayscale PNG.
* [http://brettklamer.com/diversions/statistical/compile-r-for-data-science-to-a-pdf/ Compile R for Data Science to a PDF]
* [http://brettklamer.com/diversions/statistical/compile-r-for-data-science-to-a-pdf/ Compile R for Data Science to a PDF]


Line 395: Line 1,502:
bookdown::render_book("index.Rmd", "bookdown::pdf_book")
bookdown::render_book("index.Rmd", "bookdown::pdf_book")
</pre>
</pre>
For example, to build the r4ds book (website), using
<pre>
# 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")
</pre>
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.
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 [https://bookdown.org/yihui/bookdown/figures.html 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)'''.
Also the way of including figures is different in R markdown and bookdown. In bookdown, we should follow [https://bookdown.org/yihui/bookdown/figures.html 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 [[Curl#wget_to_download_a_website|webhttrack]] to download the whole website/book without re-building the book in R.


== TexLive ==
== TexLive ==
Line 420: Line 1,543:
** 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 [https://github.com/yihui/bookdown-crc/issues/2 Rendering of unicode characters fails].
** 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 [https://github.com/yihui/bookdown-crc/issues/2 Rendering of unicode characters fails].
** The pdf output ''_main.pdf'' is under ''_book/''.
** 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 ==
[http://www.markvanderloo.eu/yaRb/2020/12/09/author-with-affiliation-in-bookdown-html-and-pdf/ Author with affiliation in bookdown: HTML and pdf]
== Publish on github ==
https://github.com/ThinkR-open/testdown
= Create a website using R Markdown =
[https://rmarkdown.rstudio.com/lesson-13.html R Markdown Websites], [https://github.com/rstudio/rmarkdown-website-examples Files], [https://bookdown.org/yihui/rmarkdown/rmarkdown-site.html 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 [https://taichimd.us/mediawiki/index.php/Apache#Overview:_Four_steps 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
== [https://github.com/hadley/pkgdown pkgdown]: create a website for your package ==


= [https://github.com/hadley/pkgdown pkgdown]: create a website for your package =
* [https://www.emilyzabor.com/tutorials/rmarkdown_websites_tutorial.html#package_websites Creating websites in R] Emily C. Zabor
* [https://www.emilyzabor.com/tutorials/rmarkdown_websites_tutorial.html#package_websites 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 [https://bookdown.org/yihui/blogdown/rstudio-ide.html RStudio IDE] section from [https://bookdown.org/yihui/blogdown/ blogdown: Creating Websites with R Markdown].
** 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 [https://bookdown.org/yihui/blogdown/rstudio-ide.html RStudio IDE] section from [https://bookdown.org/yihui/blogdown/ blogdown: Creating Websites with R Markdown].
** [https://rmarkdown.rstudio.com/lesson-13.html R Markdown Websites]
* [http://lbusettspatialr.blogspot.com/2017/08/building-website-with-pkgdown-short.html Building a website with pkgdown: a short guide]
* [http://lbusettspatialr.blogspot.com/2017/08/building-website-with-pkgdown-short.html Building a website with pkgdown: a short guide]
* [https://enpiar.com/2017/12/18/integrating-pkgdown-with-your-personal-website/ Integrating 'pkgdown' with Your Personal Website]
* [https://enpiar.com/2017/12/18/integrating-pkgdown-with-your-personal-website/ Integrating 'pkgdown' with Your Personal Website]
* [https://pkgdown.r-lib.org/articles/pkgdown.html Introduction to pkgdown]. The [https://github.com/r-lib/pkgdown/tree/master/vignettes source code] is an Rmd file.
* [https://pkgdown.r-lib.org/articles/pkgdown.html Introduction to pkgdown]. The [https://github.com/r-lib/pkgdown/tree/master/vignettes source code] is an Rmd file.


= Blogdown =
== Blogdown ==
 
* https://github.com/rstudio/blogdown
* https://github.com/rstudio/blogdown
* https://bookdown.org/yihui/blogdown/
* https://bookdown.org/yihui/blogdown/
Line 435: Line 1,589:
* [https://liao961120.github.io/2018/12/13/hugo_rss.html Customizing Hugo / Blogdown RSS Templates]
* [https://liao961120.github.io/2018/12/13/hugo_rss.html Customizing Hugo / Blogdown RSS Templates]
* [http://estebanmoro.org/post/2019-02-02-setting-up-your-blog-with-rstudio-and-blogdown-i-creating-the-blog/ Setting up our blog with RStudio and blogdown I: Creating the blog]
* [http://estebanmoro.org/post/2019-02-02-setting-up-your-blog-with-rstudio-and-blogdown-i-creating-the-blog/ Setting up our blog with RStudio and blogdown I: Creating the blog]
* [https://masalmon.eu/2020/02/29/hugo-maintenance/ What to know before you adopt Hugo/blogdown]
* [https://ropensci.org/blog/2020/04/07/bookdown-learnings/ 10 Things We Learned in Creating the Blog Guide with bookdown] 2020
* [https://yihui.org/en/2022/06/user-blogdown/ My Talk at useR! 2022 on blogdown]
== bs4cards: an R package for bootstrap 4 cards ==
[https://bs4cards.djnavarro.net/ bs4cards]
== Jekyll and Github ==
[https://jessecambon.github.io/2020/03/22/deploying-rmarkdown-online.html Deploying RMarkdown Online]
== portfoliodown ==
[https://www.business-science.io/code-tools/2021/12/20/portfoliodown.html Introducing portfoliodown: The Data Science Portfolio Website Builder]
== distill ==
[https://book.rwithoutstatistics.com/websites-chapter.html 10 Make Websites to Share Results Online] from the ebook '''R Without Statistics'''
== flexdashboard ==
* https://pkgs.rstudio.com/flexdashboard/
* [https://ivelasq.rbind.io/blog/automated-youtube-dashboard/ Using flexdashboard to create a GitHub Actions-powered YouTube feed]
= Pagedown =
* [https://github.com/rstudio/pagedown pagedown], [https://rstudio.com/resources/rstudioconf-2019/pagedown-creating-beautiful-pdfs-with-r-markdown-and-css/ pagedown: Creating beautiful PDFs with R Markdown and CSS] (video)
* [https://datawookie.dev/blog/2021/05/using-pagedown-in-docker/ Using {pagedown} in Docker]
==  Convert an HTML to a PDF, PNG ==
[https://pagedown.rbind.io/  Convert an HTML to a PDF, PNG, etc]. The [https://rdrr.io/cran/pagedown/man/chrome_print.html chrome_print] function from {pagedown} can do this either for a local file (including an Rmd file that outputs HTML) or for a URL .
<pre>
library(pagedown)
chrome_print("example.html")
chrome_print("example.html", format="png")
</pre>
= postcards =
[https://github.com/seankross/postcards postcards] - Create simple, beautiful personal websites and landing pages using only R Markdown.


= Posterdown =
= Posterdown =
[https://github.com/brentthorne/posterdown posterdown]: Use RMarkdown to generate PDF Conference Posters via HTML or LaTeX
[https://github.com/brentthorne/posterdown posterdown]: Use RMarkdown to generate PDF Conference Posters via HTML or LaTeX
= rticles package =
LaTeX Journal Article Templates for R Markdown
* https://github.com/rstudio/rticles
* https://github.com/eddelbuettel/rticles-gallery


= Latex tools =
= Latex tools =
[https://mathpix.com/ Mathpix Snip] Take a screenshot of math and paste the LaTeX into your editor
[https://mathpix.com/ Mathpix Snip] Take a screenshot of math and paste the LaTeX into your editor
== Tutorials ==
https://latex-tutorial.com/tutorials/
== Math ==
* Use \textrm{} for normal text in math mode. For example, $n_\textrm{training} = n_\textrm{test} = 500$.
= Quarto =
* https://quarto.org/
** File extension name is qmd. [https://quarto.org/docs/get-started/hello/rstudio.html Tutorial: Hello, Quarto]
** [https://quarto.org/docs/tools/rstudio.html Quarto - RStudio IDE], [https://quarto.org/docs/get-started/hello/rstudio.html Quarto - Tutorial: Hello, Quarto], [https://quarto.org/docs/get-started/computations/rstudio.html Tutorial: Computations]
** [https://quarto.org/docs/guide/ Guide], [https://quarto.org/docs/computations/r.html Using R]
** [https://quarto.org/docs/faq/rmarkdown.html FAQ for R Markdown Users]
* [https://www.jumpingrivers.com/blog/quarto-rmarkdown-comparison/ I'm an R user: Quarto or R Markdown?]
* [https://dirk.eddelbuettel.com/blog/2022/01/31/#035_apt_install_rstudio_quarto #35: apt install rstudio quarto] Eddelbuettel
* [https://youtu.be/y5VcxMOnj3M Create beautiful documents with Quarto and R] (video)
* [https://youtu.be/shVSmYna3GM R-Ladies Freiburg (English) - Getting to know Quarto] (video) 2022/6/3
* [https://youtu.be/yvi5uXQMvu4 Welcome to Quarto Workshop!] (video) Tom Mock 2022/8/9
* [https://github.com/analythium/quarto-docker-examples Quarto Examples with Docker]
* [https://twitter.com/mattdray/status/1555610784406855683?s=20&t=Xe3aXGwh0-CHLdXfKaEb5w quartostamp] RStudio Add-in package
* [https://twitter.com/MeghanMHall/status/1573365069093937155?s=20&t=f71qFzNbqErKI0omM2dWCQ Parameterized reporting with Quarto in R]
* [https://nrennie.rbind.io/blog/parameterized-plots-reports-r-quarto/ Parameterized plots and reports with R and Quarto]
* [https://medium.com/number-around-us/arming-your-data-spaceship-essential-quarto-tips-and-tricks-for-the-modern-explorer-ead0fa787adb Arming Your Data Spaceship: Essential Quarto Tips and Tricks for the Modern Explorer]. [https://quarto.org/docs/interactive/layout.html Tabs], [https://davidgohel.github.io/ggiraph/ ggiraph] for interactive ggplot2., [https://quarto.org/docs/authoring/footnotes-and-citations.html#footnotes footnote], [https://quarto.org/docs/authoring/diagrams.html mermaid/graphviz diagrams].
== Visual editor ==
[https://stackoverflow.com/a/72360483 Disable visual markdown editor on RStudio]
== Quarto dashboard ==
* https://quarto.org/docs/dashboards/
* [https://www.r-bloggers.com/2024/03/building-a-malaysian-population-dashboard-with-quarto-in-r/ Building a Malaysian Population Dashboard with Quarto in R]
= parsermd =
https://rundel.github.io/parsermd/. The goal of parsermd is to extract the content of an R Markdown file to allow for programmatic interactions with the document’s contents (i.e. code chunks and markdown text).

Latest revision as of 07:30, 30 October 2024

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.

Editors

Why You Should Be Writing Everything in Markdown 2023/10/11.

Github markdown Readme.md

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

Simple example

| Column 1       | Column 2     | Column 3     |
| :------------- | :----------: | -----------: |
|  Cell Contents | More Stuff   | And Again    |
| You Can Also   | Put Pipes In | Like this \| |

Include code

Markdown Code Block: Including Code In .md Files

  • Inline code blocks:
    Use `print("Hello, world!")` to print a message to the screen.
    
  • Fenced code blocks:
    ```python
    print("Hello, world!")
    for i in range(10):
        print(i)
    ```
    
  • Indented code blocks
    Here's some regular text. And now a code block:
    
        print("Hello, world!")
        if True:
            print('true!')
    

Literate programming

Rmarkdown

HTML5 slides examples

Software requirement

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.

Tips

Debug

Debugging RMarkdown

YAML

  • ymlthis package - write YAML for R Markdown, bookdown, blogdown, and more.
  • R Markdown Crash Course YAML Headers

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

HTML Turn off title

Rmarkdown: Turn off title

HTML with dynamic table of contents

---
title: "My title"
output: 
  rmdformats::robobook: 
    highlight: tango
    number_sections: true
    lightbox: true
    gallery: true
    code_folding: hide
---

Inlcude latex packages

render(, params)

---
title: "Homework"
output:
  html_document:
    code_folding: hide
    toc: true
    toc_float: true
    theme: "flatly"
params:
  run: TRUE
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = params$run)
```

```{r chunk1}
dim(iris)
```

We can use the following command to create an html file that does not evaluate the code for the whole document.

rmarkdown::render("Homework.Rmd",
          params=list(run = FALSE), 
          output_file = "Homework.html")

Code folding

Center title in html output

How do I center my YAML heading in an R markdown html document? Use CSS styling.

Office (Word, Powerpoint) output

Examples

ComBatCorr, Preview HTML

---
title: "..."
author: "..."
output:
  html_document:
    code_folding: hide
    toc: true
    toc_float: true
    theme: "flatly"
editor_options: 
  chunk_output_type: console
---

PDF output

---
title: "..."
date: "`r format(Sys.time(), '%d %B, %Y')`"
output:
  pdf_document:
    toc: true
    toc_depth: 3
    number_sections: true
    fig_caption: true
urlcolor: blue
---

Chunk options

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). This seems to be useful. Too many warnings will make the computation time much longer (eg. 3 hours vs 30 minutes) no to say the output file will be very large.
  • error. whether to preserve errors (from stop()); If you want to show the errors without stopping R, you may use the chunk option error = TRUE. See Do not stop on error
  • 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)
```

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)
```

Include sections inside a chunk

```{r}
# Chunk section 1 ----
...
# Chunk section 2 ----
```

Ignore duplicate chunk labels

I got an error Calls: <Anonymous> ... process_file -> split_file -> lapply -> FUN -> parse_block Execution halted because of duplicate chunk labels.

See Chapter 14 Miscellaneous knitr Tricks

Include all the code from the Rmd

Use knitr::all_labels().

Conditional `echo` (or eval or include) in rmarkdown chunks

Control eval or output. Following is an example that the chunk will be run if a "linux" machine is detected (e.g. the linux machine is some quality such as a larger RAM).

```{r label}
get_os <- function(){
  sysinf <- Sys.info()
  if (!is.null(sysinf)){
    os <- sysinf['sysname']
    if (os == 'Darwin')
      os <- "osx"
  } else { ## mystery machine
    os <- .Platform$OS.type
    if (grepl("^darwin", R.version$os))
      os <- "osx"
    if (grepl("linux-gnu", R.version$os))
      os <- "linux"
  }
  tolower(os)
}

doNextChunk <- get_os() == "linux"
```

```{r conditional, eval = doNextChunk}
"hello world!"
```

Conditional evaluation using params

See here

Conditional code for specific output formats

Breaking the page in LaTeX output for PDFs. Note that commands passed to LaTeX in this way need to be escaped with a double slash.

`r if (knitr::is_latex_output()) '\\newpage'`

results = "asis"

Output text as raw Markdown content

"source" (run) all of the code chunks in a .qmd file

"source" (run) all of the code chunks in a .qmd file (like `source("analysis.qmd")`) inside another .qmd file? See 3.4 Convert R Markdown to R script

```{r}
library(knitr)
temp_r_file <- tempfile(fileext = '.R')
purl("first-file.qmd", output = temp_r_file)
source(temp_r_file)
unlink(temp_r_file)
```

Multiple plots/tables in one chunk

twitter

Reuse code chunks, combine chunks

See R Markdown Tips and Tricks #3: Time-savers & Trouble-shooters

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 (`r x<-5 `)

Language support (engine)

Mix markdown with Latex

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

Templates

Knit button

  • It calls rmarkdown::render()
  • It does not count as an interactive session. But if we call rmarkdown::render() in an R session, it counts as an interactive session.
  • R Markdown = knitr + Pandoc
  • rmarkdown::render() = knitr::knit() + a system() call to pandoc

Exit knitting early

Miscellaneous knitr Tricks

Pandoc's Markdown

pandoc will be installed automatically if we install tidyverse by using sudo apt install r-cran-tidyverse binary package.

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 and rapport packages

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.

Read the docs Sphinx theme and journal article formats

http://blog.rstudio.org/2016/03/21/r-markdown-custom-formats/

  • rticles package
  • rmdformats package. For each format, it also takes the option from html_document such as code_folding (the default is 'none' so there is no way to control it on HTML).
    • downcute: how to turn off black color
    • robobook: looks pretty. TOC is on LHS & can be closed like bookdown theme.
    • material
    • readthedown
    • html_clean: the TOC is kind of small
    • html_docco
output: 
  rmdformats::robobook: 
    highlight: tango
    number_sections: true
    lightbox: true
    gallery: true
    code_folding: show

rmarkdown news, floating TOC

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

Space

Add horizontal space

Use &nbsp; 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

An rmarkdown example.

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 ?

Figure caption

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) 
```

5. Control the size of plots/images. It works.

![A nice image.](foo/bar.png){width=50%}

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
knitr 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.

Table contest

knitr::kable

From scratch

Little useless-useful R functions – Transforming dataframe to markdown table

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 (images in tables)

https://cran.r-project.org/web/packages/kableExtra/index.html, Insert Images into Columns, Add an image to a table-like output in R

This example Using gt, gtExtras and openair to present air quality monitoring data can insert clickable expandable figures in tables. The example uses gt and gtExtras package.

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"))

Alternate row color with knitr:kable in R Markdown

knitr::kable(mtcars, "html") %>%
  kable_styling("striped")

kable kableExtra, Cells with hyperlinks

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.

gt and gtExtras

Include images in tables

Include plots in tables (without using gtExtras package)

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

print(xt, size="\\fontsize{9pt}{10pt}\\selectfont") 
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/
  • Table 1 and the Characteristics of Study Population
    • The demo data can be downloaded
    • ?CreateTableOne
    • It can be verified that if the interested variable is continuous, we can use t.test(, var.equal = T)$p.value to obtain p-values. Or use the aov function: summary(aov(BMI ~ Gender, dt))[[1]][["Pr(>F)"]][1] .
    • If the interested variable is discrete, it seems it uses chi.square(var1, var2)$p.value to obtained p-values. It seems even the cells have small number of observations, it still use chisq-test. We can however obtain the Fisher's exact test p-values in the print() method.
    R> with(dt[1:15,], table(Smoking, Gender))
           Gender
    Smoking Female Male
        No       5    6
        Yes      2    2
    
    R> with(dt[1:15,], chisq.test(table(Education, Gender))$p.value)
    [1] 0.6691241
    Warning message:
    In chisq.test(table(Education, Gender)) :
      Chi-squared approximation may be incorrect
    R> with(dt[1:15,], fisher.test(table(Education, Gender))$p.value)
    [1] 0.8135198
    
    R> CreateTableOne(c("Smoking", "Education"), dt[1:15,], catVars, strata = c("Gender")) 
                       Stratified by Gender
                        Female    Male      p      test
      n                 7         8                    
      Smoking = Yes (%) 2 (28.6)  2 (25.0)   1.000     
      Education (%)                          0.669     
         High           2 (28.6)  4 (50.0)             
         Low            3 (42.9)  2 (25.0)             
         Medium         2 (28.6)  2 (25.0)  
    
    R> print(CreateTableOne(c("Smoking", "Education"), dt[1:15,], catVars, strata = c("Gender")), 
             exact = c("Smoking", "Education")) 
                       Stratified by Gender
                        Female    Male      p      test 
      n                 7         8                     
      Smoking = Yes (%) 2 (28.6)  2 (25.0)   1.000 exact
      Education (%)                          0.814 exact
         High           2 (28.6)  4 (50.0)              
         Low            3 (42.9)  2 (25.0)              
         Medium         2 (28.6)  2 (25.0)  
    
    # The print() result is a table so we can output it using write.table()
    R> print(CreateTableOne(c("Smoking", "Education"), dt[1:15,], catVars, strata = c("Gender")), 
             exact = c("Smoking", "Education")) %>% dim()
    # [1] 6 4
    
  • 简书

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.
    library(flextable)
    df <- data.frame(a=letters[1:3], b=rnorm(3), c=letters[4:6])
    df <- flextable(df)
    df <- add_header_row(ft, c("Merge Header", "Header 3"))
    df <- align(ft, align = 'center', part = 'header')
    df <- align(ft, j=1:3, align='center', part='body')
    df <- set_caption(ft, caption = 'my table caption')
    df <- add_footer_lines(df, values = c("blah 1", "blah 2"))
    # Output to Word
    library(officer)
    doc <- read_docx()
    doc <- body_add_flextable(doc, ft)
    print(doc, target = "output.docx")
  • ebook with cheat sheet. Search "library" to find examples, especially the chapter Chapter 9 Transform objects into flextable
    ---
    title: "Untitled2"
    output:
      word_document: default
    ---
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    library(flextable)
    ```
    
    ```{r echo=FALSE}
    ft <- flextable(head(mtcars))
    ft <- autofit(ft)
    ft
    ```
    
    A new chunk
    ```{r results='asis', echo = FALSE}
    ft <- set_caption(ft, caption = "Table 1: New York Air Quality Measurements")
    theme_zebra(ft) # ft <- theme_zebra(ft)
    ```
    

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.

tinytable

https://vincentarelbundock.github.io/tinytable/

How to align table and plot in rmarkdown html_document

https://stackoverflow.com/a/54359010

Math equations

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

Convert to a markdown document

md_document()

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

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).

R built in Web server

writeLines("<h1>Hi</H1>", "index.html")

library(servr)
servr::httd()

crosstalk - generating a plain HTML document with no need for a Shiny server

Rmarkdown taxonomy and the video by Thomas Mock

Interactive document: Shiny

See R Markdown Cheat Sheet.

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

Scrollable code/output

Download button/embed files

```{css echo = F}
.button_green {
  background-color: #4CAF50;
  font-size: 14px;
}
```

```{r}
library(downloadthis)

# Use the class parameter in download_this to apply your custom class
mtcars %>% download_this(
  output_name = "mtcars",
  output_extension = ".xlsx",
  button_label = "Download datasets as xlsx",
  button_type = "warning",  
  has_icon = TRUE,
  icon = "fa fa-save",
  class = "button_green" # Add your custom class here
)
```
library(download_this)

# Custom CSS for the button
button_css <- "
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
  font-size: 16px;
  margin: 4px 2px;
  cursor: pointer;
  border-radius: 4px;
"

# Create the download button
download_this(mtcars, style = button_css, 
              str_anvlopt = "Download Data", 
              output_name = "my_data.csv")

Dropdown menu

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

Presentation

4 output format

font size

Presenter mode

  • ioslides presentation
    1. Follow this to add notes to your Rmd file.
    2. Add "?presentme=true" to the end of the URL of HTML file in your browser. This will popup another window/tab and this is the presenter will control and see all the notes. The original window/tab will be used by viewers. On my Chrome browser, I need to allow a popup from this location. Firefox is easy to allow popup. Safari browser is difficult.
    3. Use the "p" key to change the mode to Presenter mode. You will see the notes at the bottom of the screen. If the notes are very long, a slider will be shown on the RHS.
  • xaringan Presentations

PowerPoint slides

xaringan: presentation

.code-bg-gray .remark-code, .code-bg-gray .remark-code * {
 background-color:#e1e7e9!important;
}
In the YAML section, change to css: [default, metropolis, metropolis-fonts, custom.css]. In the code chunk, follow the instruction there by wrapping your code chunk with .code-bg-gray[ ]
  • You can use HTML elements directly in an R Markdown file. R Markdown allows you to mix Markdown, R code, and HTML code in the same document1. You can use HTML tags to format your text, create tables, insert images, and more. For example, if I want to highlight a certain line in a code block, we can use the span element with an inline CSS to set the background color. See 7.15 For hardcore HTML users (*) from R Markdown Cookbook.
<pre><code>
This is a code block
<span style="background-color:yellow">This line is highlighted</span>
</code></pre>
  • Change the font size of a table
<style>
# Slide with Table

table {
  font-size: 16px; /* Set the desired font size */
}
</style>

| Task                            | Docker Command              | 
|---------------------------------|-----------------------------|
| Build an image                  | `docker build`              |
| Run a container                 | `docker run`                |
| Pull an image from a registry   | `docker pull`               |

Create presentation file (beamer)

  1. Create Rmd file first in Rstudio by File -> R markdown. Select Presentation > choose pdf (beamer) as output format.
  2. Edit the template created by RStudio.
  3. 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?
  • Object used in cache.extra will be printed on screen. So if a complex object is used in cache.extra, it will take a long time to show the object (thus build the output). So it is smart to only use a simple part of a complex object in "cache.extra".
  • Even I use "eval=FALSE", the object in cache.extra is still printed out on screen (or depends on the order of the parameters?).
---
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:

  1. 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 "~/".
  2. 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.

If I manually delete files under _files and re-render, it will give me an error that xxx.pdf cannot be found.

If I want to re-run a chunk without changing the Rmd file, we can just delete files (*.rdx, *.rdb, *.RData) related to a chunk in _cache/latex folder. There is no need to delete files in _files folder since files there will be re-generated too.

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

reporter

reporter package

Publish R results

Scheduling R Markdown Reports via Email

http://www.analyticsforfun.com/2016/01/scheduling-r-markdown-reports-via-email.html

Scheduling Rmarkdown files on Windows

Scheduling Rmarkdown files on Windows - your foolproof guide

R notebook vs R markdown in RStudio

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

Graphics Output in LaTeX Format

Landscape output

https://stackoverflow.com/questions/25849814/rstudio-rmarkdown-both-portrait-and-landscape-layout-in-a-single-pdf

Break a long document and child option

Programmatically create new headings and outputs

Programmatically create new headings and outputs in Rmarkdown

Request an early exit

https://stackoverflow.com/a/33711413

Bibliographies

Docker

How to compile R Markdown documents using Docker

lazy load plots

lazyrmd package

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.

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
  • 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

Publish on github

https://github.com/ThinkR-open/testdown

Create a website using R Markdown

R Markdown Websites, Files, R Markdown: The Definitive Guide -> rmarkdown’s site generator

  1. RStudio -> File -> New Project -> Simple R Markdown Website. This will create 3 files: _site.yml, index.Rmd and about.Rmd
  2. 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

pkgdown: create a website for your package

Blogdown

bs4cards: an R package for bootstrap 4 cards

bs4cards

Jekyll and Github

Deploying RMarkdown Online

portfoliodown

Introducing portfoliodown: The Data Science Portfolio Website Builder

distill

10 Make Websites to Share Results Online from the ebook R Without Statistics

flexdashboard

Pagedown

Convert an HTML to a PDF, PNG

Convert an HTML to a PDF, PNG, etc. The chrome_print function from {pagedown} can do this either for a local file (including an Rmd file that outputs HTML) or for a URL .

library(pagedown)
chrome_print("example.html")
chrome_print("example.html", format="png")

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

Tutorials

https://latex-tutorial.com/tutorials/

Math

  • Use \textrm{} for normal text in math mode. For example, $n_\textrm{training} = n_\textrm{test} = 500$.

Quarto

Visual editor

Disable visual markdown editor on RStudio

Quarto dashboard

parsermd

https://rundel.github.io/parsermd/. The goal of parsermd is to extract the content of an R Markdown file to allow for programmatic interactions with the document’s contents (i.e. code chunks and markdown text).