### Install rmarkdown Development Version
Source: https://github.com/rstudio/rmarkdown/blob/main/README.md
Install the latest development version of the rmarkdown package from GitHub using the 'pak' package. Ensure 'pak' is installed first if you don't have it.
```r
# install.packages("pak")
pak::pak('rstudio/rmarkdown')
```
--------------------------------
### Pandoc Detection and Versioning
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Checks if pandoc is installed and meets minimum version requirements. Automatically finds pandoc from the system PATH and RStudio's bundled version.
```R
library(rmarkdown)
pandoc_available()
pandoc_available("2.11")
pandoc_version()
pandoc_available("2.0", error = TRUE)
pandoc_exec()
if (pandoc_available("2.19")) {
message("Using --embed-resources instead of --self-contained")
}
```
--------------------------------
### Install rmarkdown from CRAN
Source: https://github.com/rstudio/rmarkdown/blob/main/README.md
Use this command to install the stable version of the rmarkdown package from CRAN.
```r
install.packages("rmarkdown")
```
--------------------------------
### Pandoc Detection
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Functions to check for pandoc installation and version.
```APIDOC
## pandoc_available()
### Description
Checks if pandoc is installed and meets a minimum version requirement. Automatically finds the highest available pandoc from the system PATH and RStudio's bundled version.
### Usage
```R
library(rmarkdown)
# Check if any pandoc is available
pandoc_available()
# Check for a specific minimum version
pandoc_available("2.11")
```
## pandoc_version()
### Description
Retrieves the exact installed pandoc version.
### Usage
```R
pandoc_version()
```
## pandoc_exec()
### Description
Returns the file path to the pandoc binary.
### Usage
```R
pandoc_exec()
```
```
--------------------------------
### Direct Pandoc Conversion
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Low-level function to invoke pandoc for arbitrary document conversion. Useful for plain Markdown, bibliography processing, or custom pipelines. Ensure pandoc is installed.
```R
library(rmarkdown)
pandoc_convert("input.md", to = "html", output = "output.html")
pandoc_convert("input.md", to = "latex", output = "output.tex")
pandoc_convert("input.md", to = "html",
citeproc = TRUE,
options = c("--bibliography=refs.bib",
"--csl=apa.csl"))
pandoc_convert("input.md", to = "docx",
output = "report.docx",
options = c("--reference-doc=template.docx"),
verbose = TRUE)
bib_list <- pandoc_citeproc_convert("refs.bib", type = "list")
bib_list[[1]]$title
```
--------------------------------
### Create Document from Template
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Creates a new .Rmd file from a named template supplied by an installed package or a filesystem path. Templates are located within the package's inst/rmarkdown/templates/ directory.
```R
library(rmarkdown)
available_templates("rmarkdown")
draft("MyReport.Rmd",
template = "quarterly_report",
package = "pubtools",
edit = FALSE)
draft("NewDoc.Rmd",
template = "/path/to/my-template",
create_dir = TRUE)
```
--------------------------------
### Using Absolute URLs for External Resources
Source: https://github.com/rstudio/rmarkdown/wiki/rmarkdown::run-and-external-resources
To make external resources accessible in interactive R Markdown documents, reference them using absolute URLs (starting with HTTP or HTTPS).
```markdown
## Enjoy this embedded graph
```
--------------------------------
### Embedding External HTML in R Markdown
Source: https://github.com/rstudio/rmarkdown/wiki/rmarkdown::run-and-external-resources
This example shows how to embed an external HTML file using an iframe. It works in static documents but fails in interactive documents with `runtime: shiny`.
```markdown
# myfile.Rmd
## Enjoy this embedded graph
```
--------------------------------
### Configure PDF Output with `pdf_document()`
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Use `pdf_document()` to convert R Markdown files to PDF via LaTeX. It supports custom LaTeX templates, figure cropping, and citation management with natbib/biblatex. A LaTeX installation, such as tinytex, is required.
```R
library(rmarkdown)
```
--------------------------------
### Configure math rendering engine for github_document
Source: https://github.com/rstudio/rmarkdown/blob/main/NEWS.md
Enable LaTeX equation rendering in GitHub Markdown documents by setting the math_method argument. Defaults to 'webtex' for image rendering. Set to NULL to disable.
```yaml
output:
github_document:
math_method: webtex
```
--------------------------------
### Configure math rendering engine in html_document
Source: https://github.com/rstudio/rmarkdown/blob/main/NEWS.md
Specify the math rendering engine for HTML documents. Supports 'mathjax', 'katex', 'mathml', 'webtex', and 'gladtex'. The 'mathjax' argument is kept for backward compatibility but 'math_method' is recommended.
```yaml
output:
html_document:
math_method: katex
```
```yaml
output:
html_document:
math_method:
engine: mathjax
url: https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js
```
--------------------------------
### Configure HTML Output with `html_document()`
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Use `html_document()` to create HTML documents with Bootstrap theming. Options include table of contents (floating or nested), section numbering, code folding/download, themes, syntax highlighting, and math rendering. It can also integrate with bslib for Bootstrap 5 theming.
```R
library(rmarkdown)
# Full-featured HTML document
fmt <- html_document(
toc = TRUE,
toc_depth = 3,
toc_float = list(collapsed = TRUE, smooth_scroll = TRUE, print = FALSE),
number_sections = TRUE,
anchor_sections = list(style = "icon", depth = 2),
code_folding = "hide", # "none" | "show" | "hide"
code_download = TRUE,
self_contained = TRUE,
theme = "flatly", # Bootswatch theme
highlight = "arrow", # accessible color scheme
math_method = "katex",
df_print = "paged",
css = "custom.css",
fig_width = 8,
fig_height = 5
)
render("analysis.Rmd", fmt)
# Minimal document using bslib Bootstrap 5 theme
render("report.Rmd",
html_document(
theme = bslib::bs_theme(version = 5, bootswatch = "cosmo"),
highlight = "pygments"
))
# In YAML front matter (equivalent):
# output:
# html_document:
# toc: true
# toc_float: true
# code_folding: hide
# theme: flatly
# highlight: arrow
# df_print: paged
```
--------------------------------
### Run Shiny R Markdown Documents with rmarkdown::run()
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Serve an interactive R Markdown file with `runtime: shiny` as a live Shiny web application. This allows code to execute reactively based on user input.
```R
library(rmarkdown)
# --- shiny-app.Rmd (YAML header) ---
# ---
# title: "Interactive Explorer"
# runtime: shiny
# output: html_document
# ---
# ```{r, echo=FALSE}
# sliderInput("n", "Number of points", 10, 500, 100)
# renderPlot({ plot(rnorm(input$n)) })
# ```
# Launch in the default browser
run("shiny-app.Rmd")
# Launch on a specific port and host
run("shiny-app.Rmd",
shiny_args = list(port = 4321, launch.browser = FALSE))
# Run all .Rmd files in a directory (serves directory as a Shiny app)
run("my-shiny-site/")
```
--------------------------------
### Render GitHub Markdown with WebTeX Math
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Configures GitHub Markdown output to use WebTeX for math rendering, ensuring compatibility with older Pandoc versions. Specify the image URL for the rendering service.
```r
render("notes.Rmd",
github_document(
math_method = list(engine = "webtex",
url = "https://latex.codecogs.com/png.image?")
))
```
--------------------------------
### Render Basic Word Document
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Creates a basic Microsoft Word (.docx) document from an R Markdown file. Suitable for standard reports and documents.
```r
render("report.Rmd", word_document())
```
--------------------------------
### jQuery UI Slider Widget
Source: https://github.com/rstudio/rmarkdown/blob/main/inst/rmd/h/jqueryui/index.html
Initializes a range slider widget with predefined values. The `range` option set to `true` enables the range functionality, and `values` sets the initial start and end points.
```javascript
$( "#slider" ).slider({
range: true,
values: [ 17, 67 ]
});
```
--------------------------------
### Read R Markdown Site Configuration
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Reads the _site.yml configuration file into an R list for programmatic access.
```R
config <- site_config(".")
config$name
config$output_dir
config$navbar
```
--------------------------------
### Render GitHub Flavored Markdown Document
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Creates a Markdown file suitable for GitHub, with options for table of contents and math rendering. Use for README files or project documentation on GitHub.
```r
render("README.Rmd",
github_document(
toc = TRUE,
toc_depth = 2,
math_method = "default", # native GitHub math ($...$ and $$...$$)
hard_line_breaks = TRUE,
html_preview = FALSE # skip generating preview .html
))
```
--------------------------------
### Custom Output Format
Source: https://context7.com/rstudio/rmarkdown/llms.txt
Constructor for defining custom output formats by combining knitr and pandoc options.
```APIDOC
## output_format()
### Description
Defines a custom output format by combining knitr options, pandoc options, and lifecycle hooks. Used internally by built-in formats and by third-party packages.
### Usage
```R
library(rmarkdown)
# Define a minimal custom HTML format
my_format <- output_format(
knitr = knitr_options(opts_chunk = list(dev = "svg", dpi = 144, fig.width = 8, fig.height = 5)),
pandoc = pandoc_options(to = "html", from = from_rmarkdown(fig_caption = TRUE), args = c("--standalone", "--toc")),
keep_md = FALSE,
clean_supporting = TRUE,
df_print = "kable",
pre_processor = function(metadata, input_file, runtime, knit_meta, files_dir, output_dir) {
if (!is.null(metadata$watermark)) {
c("--variable", paste0("watermark=", metadata$watermark))
}
},
post_processor = function(metadata, input_file, output_file, clean, verbose) {
lines <- readLines(output_file)
lines <- sub("