Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save strengejacke/bd14a299a806d5e2d03347d5c274a7f9 to your computer and use it in GitHub Desktop.
Save strengejacke/bd14a299a806d5e2d03347d5c274a7f9 to your computer and use it in GitHub Desktop.
Compile R Report (HTML/PDF/Word file from R scripts)
#' ---
#' title: "Compile Report"
#' author: ""
#' date: ""
#' ---
#' Comments that are to be displayed as normal text are marked with `#'`
#' at the beginning of the line. Comments that are to be displayed as
#' code are marked with `#` at the beginning of the line.
# This is a comment for R code
x <- 1
#' # Headings
#'
#' Headings are introduced with `#` after the comment. 1x `#` for
#' heading 1, 2x `#` for heading 2, etc., e.g: `#' ## heading 2`.
#'
#' # Run R-code
#'
#' R-Code is run automatically
library(datawizard)
data(mtcars)
data_tabulate(mtcars, "cyl")
#' If only the output is to be displayed without the code, then there
#' must be a comment with `+ echo=FALSE` before the code block,
#' e.g. `#+ echo=FALSE`.
#+ echo=FALSE
library(datawizard)
data(mtcars)
data_tabulate(mtcars, "cyl")
#' # HTML-tables instead text output
#'
#' HTML can also be used, hence you can create nice tables with `print_html()`
library(datawizard)
data(mtcars)
data_tabulate(mtcars, "cyl") |> print_html()
#' # Text formatting
#'
#' Text can be formatted using markdown syntax, e.g. `**word**` for
#' **bold** or `*word*` für *italic*.
#'
#' It is important that the text, including blank lines, is continued as comment, e.g.
#'
#'
#' ```
#' #' First row
#' #'
#' #' Second row
#' ```
@strengejacke
Copy link
Author

RStudio:

Here's the icon in to compile reports in RStudio:
image

VSCode

This is a possible keybinding for VS Code:

// Compile report (knit R Script)
{
  "description": "R Compile Report",
  "key": "ctrl+shift+t",
  "command": "r.runCommand",
  "args": "rmarkdown::render(input=rstudioapi::getSourceEditorContext()$path)",
  "when": "resourceLangId == 'r'"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment