Skip to content

Instantly share code, notes, and snippets.

@mcjmigdal
Created July 10, 2025 14:43
Show Gist options
  • Select an option

  • Save mcjmigdal/5491234407582ab4addd31a93ecf341b to your computer and use it in GitHub Desktop.

Select an option

Save mcjmigdal/5491234407582ab4addd31a93ecf341b to your computer and use it in GitHub Desktop.
Rmarkdown that can be run interactively or render to static output
---
title: "Report that can be run interactively or rendered to static output"
output:
html_document:
toc: yes
toc_float:
collapsed: no
smooth_scroll: no
toc_depth: 5
css: "./style/css/markdown.css"
df_print: paged
pdf_document:
toc: yes
toc_depth: '5'
runtime: shiny
params:
row: 1
---
This RMarkdown report can be run interactively or rendered to static output using the following commands:
### Running interactively:
Use `rmarkdown::run()` to launch the report with a Shiny runtime:
```{r, eval=FALSE}
rmarkdown::run("report.Rmd")
```
### Rendering to static output:
Use `rmarkdown::render()` for a static document rendering:
```{r, eval=FALSE}
rmarkdown::render("report.Rmd", params = list(row = 5))
```
```{r, echo = FALSE, collapse=FALSE, message = FALSE, warning = FALSE}
library(shiny)
# setup to allow both interactive shiny and static rendering
if (! shiny::isRunning()) {
input <- params
# for a static render monkey shiny functions
assign(
"reactive",
function(expr, ...) {
function() eval(expr, envir = parent.frame())
},
envir = globalenv()
)
# loop over useful shiny functions and monkey them
shiny_fns <- ls(envir = asNamespace("shiny"), all.names = TRUE)
for (nm in shiny_fns) {
# patch all render* functions to evaluate & print
if (grepl("^render", nm)) {
assign(nm,
function(expr, ...) {
val <- eval(expr, envir = parent.frame())
print(val)
},
envir = globalenv()
)
}
# stub out all *Input() and *Output() calls so they don't error
if (grepl("(Input|Output)$", nm)) {
assign(nm,
function(...) NULL,
envir = globalenv()
)
}
}
}
```
```{r, echo = FALSE}
selectInput("row", "Select row:",
choices = 1:10,
selected = params$row)
foo <- reactive({paste("This is row", input$row)})
renderPrint({foo()})
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment