I am trying to modify this example to add a pre-visualisation of the markdown report in the main Ui panel but I am not able to pass the parameters correctly using rmarkdown::render.
shinyApp(
ui = fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("slider", "Slider", 1, 100, 50),
downloadButton("report", "Generate report")),
mainPanel(
uiOutput('reportUi')
)
)
),
server = function(input, output) {
output$report <- downloadHandler(
filename = "report.html",
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(n = input$slider)
# Knit the document, passing in the `params` list
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
output$reportUi <- renderUI({
tempReportUi <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReportUi, overwrite = TRUE)
# Set up parameters to pass to Rmd document
paramsUi <- list(n = input$slider)
rmarkdown::render(tempReportUi, output_file = file,
params = paramsUi,
envir = new.env(parent = globalenv()))
})
}
)
report.Rmd
Here is my regression model:
```{r model, collapse=TRUE}
options(digits = 4)
fit <- lm(params$n, data = mtcars)
b <- coef(fit)
summary(fit)
```
The fitting result is $mpg = `r b[1]` + `r b[2]``r input$x`$.
Below is a scatter plot with the regression line.
```{r plot, fig.height=5}
par(mar = c(4, 4, 1, 1))
plot(regFormula(), data = mtcars, pch = 19, col = 'gray')
abline(fit, col = 'red', lwd = 2)
```
Related
This is a follow up question to this How to pass a reactive plot generated in Shiny to Rmarkdown to generate dynamic reports
I am trying to pass the user input in textInput to the filename argument of downloadhandler.
This concept usually works, but in this case this code does not work:
In essence I want to change
filename = "report.html",
TO
filename = paste0("my_new_name", input$text,"-" ,Sys.Date(), ".html"),
Here is the code:
library(shiny)
library(radarchart)
shinyApp(
ui = fluidPage(
sliderInput("slider", "Slider", 1, 100, 50),
textInput("text", "text"),
downloadButton("report", "Generate report")
),
server <- function(input, output) {
output$plot1 <- renderChartJSRadar({
chartJSRadar(skills[, c("Label", input$selectedPeople)],
maxScale = 10, showToolTipLabel=TRUE)
})
output$report <- downloadHandler(
#filename = "report.html",
filename = paste0("my_new_name", input$text,"-" ,Sys.Date(), ".html"),
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
params <- list(scores = skills[, c("Label", input$selectedPeople)])
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
)
Report.Rmd
---
title: "Dynamic report"
output: html_document
params:
scores: NA
---
```{r}
chartJSRadar(params$scores, maxScale = 10, showToolTipLabel=TRUE)
```
When the desired file name relies on a reactive value, you have to set it with a function without argument:
filename = function() {
_what_you_want_
}
I have an example RShiny app with reactive filters that I added download button buttons for downloading .csv and Rmd report (as html). The download report handler links to a parameterized Rmd file.
I have no problem downloading the filtered data in .csv. I can also download the report, but it is not filtered with the slider/selection inputs.
I think it is something with the params arg, but I am getting stuck on that. Below is the RShiny code and the .Rmd code below:
library(dplyr)
library(ggplot2)
library(shiny)
library(shinydashboard)
library(ggthemes)
setwd(wd)
cat1 <- as.character(c(1:10))
cat2 <- c("a", "b", "a", "a", "a", "b", "b", "a", "a", "b")
cat3 <- c(1,3,6,9,12,15,18,21,24,27)
cat4 <- c("one", "one", "one", "two", "two", "four", "three", "five", "three", "four")
df <- data.frame(cat1, cat2, cat3, cat4)
#--------------------------------------------
ui <-
fluidPage(
theme = bs_theme(version = 4, bootswatch = "lumen"),
fluidRow(
column(9,
offset = 0,
span("Example")
)
),
sidebarLayout(
position = "left",
sidebarPanel(
width = 3, offset = 0,
selectInput("set",
label = "Set:",
choices = c("All", unique(df$cat2))
),
sliderInput(inputId = "age",
label = "Choose Age Range:",
min = min(df$cat3),
max = 30,
value=c(1, 30),
step = 3)
),
mainPanel(
width = 9, offset=0,
tabsetPanel(
tabPanel('Dashboard',
br(),
dashboardPage(
dashboardHeader(disable = TRUE),
dashboardSidebar(disable = TRUE),
dashboardBody(
box(
title = "Group distribution",
width = 6,
background = "light-blue",
solidHeader = TRUE,
plotOutput("group_bar", height = 300)
)
)
),
downloadButton("data", "Download Data"),
downloadButton("report", "Download Report")
)
)
)
)
)
#------------------------------------------------
server <- function(input, output, session) {
rval_filters <- reactive({
req(input$set)
req(input$age)
data <- df
#filter data set
if (input$set != "All"){
data <- data %>%
filter(cat2 %in% input$set)
} else {
data
}
#filter based on age range
data <- data %>%
filter(cat3 >= input$age[1] & cat3 <= input$age[2])
data
})
# plot by group
output$group_bar <- renderPlot({
group <- rval_filters() %>%
#summarize
group_by(cat4) %>%
summarise(n = n())
plot_bar <- ggplot(group, aes(x= n, y = reorder(cat4, n))) +
geom_bar(stat= "identity",fill = "#4C7A99") +
theme_minimal()+
labs(x = "Count")
plot_bar
})
output$data <- downloadHandler(
filename = function(){
paste0("report", ".csv")
},
content = function(file){
write.csv(rval_filters(), file)
}
)
output$report <- downloadHandler(
filename = "report.html",
content = function(file) {
# Copy the report file to a temporary directory before processing it, in
# case we don't have write permissions to the current working dir (which
# can happen when deployed).
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(
n = df,
plot = plot_bar
)
# Knit the document, passing in the `params` list, and eval it in a
# child of the global environment (this isolates the code in the document
# from the code in this app).
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
# Run app ----
shinyApp(ui, server)
#-----------RMD----------------------
---
title: "Dynamic report"
output: html_document
params:
n: NA
plot: "NULL"
---
```{r}
# The `params` object is available in the document.
params$n
# A plot
params$plot
The issue is that you pass plot_bar to the plot parameter of the Rmd. However, plot_bar is a local variable defined and only accessible within your renderPlot. To export your plot use a reactive to create the plot. This reactive could then be called inside renderPlot to plot your chart and also be passed to the plot parameter of the Rmd:
plot_bar <- reactive({
group <- rval_filters() %>%
group_by(cat4) %>%
summarise(n = n())
plot_bar <- ggplot(group, aes(x = n, y = reorder(cat4, n))) +
geom_bar(stat = "identity", fill = "#4C7A99") +
theme_minimal() +
labs(x = "Count")
plot_bar
})
# plot by group
output$group_bar <- renderPlot({
plot_bar()
})
output$report <- downloadHandler(
filename = "report.html",
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
# Set up parameters to pass to Rmd document
params <- list(
n = df,
plot = plot_bar()
)
rmarkdown::render(tempReport,
output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
I'm trying to make a reactive data table in R Shiny that has a button you can press to compile an RMarkdown document. Ultimately, I'm trying to combine the solutions from these two links:
R Shiny: Handle Action Buttons in Data Table and https://shiny.rstudio.com/articles/generating-reports.html. Here is what I have so far:
library(shiny)
library(shinyjs)
library(DT)
shinyApp(
ui <- fluidPage(
DT::dataTableOutput("data")
),
server <- function(input, output) {
useShinyjs()
shinyInput <- function(FUN, len, id, ...) {
inputs <- character(len)
for (i in seq_len(len)) {
inputs[i] <- as.character(FUN(paste0(id, i), ...))
}
inputs
}
df <- reactiveValues(data = data.frame(
Portfolio = c('Column1', 'Column2'),
Option_1 = shinyInput(downloadButton, 2, 'compile_', label = "Compile Document", onclick = 'Shiny.onInputChange(\"compile_document\", this.id)' ),
stringsAsFactors = FALSE,
row.names = 1:2
))
output$data <- DT::renderDataTable(
df$data, server = FALSE, escape = FALSE, selection = 'none', filter='top'
)
output$compile_document <- downloadHandler(
filename = "report.html",
content = function(file) {
tempReport <- file.path(tempdir(), "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
params <- list(n = input$slider)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
)
Here is the RMarkdown document I'd like to compile:
---
title: "Dynamic report"
output: html_document
params:
n: NA
---
```{r}
# The `params` object is available in the document.
params$n
```
A plot of `params$n` random points.
```{r}
plot(rnorm(params$n), rnorm(params$n))
```
The pieces all seem to be there, but I can't connect the "Compile Document" button to the download handler.
Here is a way that does not use downloadHandler.
library(shiny)
library(DT)
library(base64enc)
library(rmarkdown)
js <- '
Shiny.addCustomMessageHandler("download", function(b64){
const a = document.createElement("a");
document.body.append(a);
a.download = "report.docx";
a.href = b64;
a.click();
a.remove();
})
'
buttonHTML <- function(i){
as.character(
actionButton(
paste0("button_", i), label = "Report",
onclick = sprintf("Shiny.setInputValue('button', %d);", i)
)
)
}
dat <- data.frame(
PortFolio = c("Column 1", "Column 2")
)
dat$Action <- sapply(1:nrow(dat), buttonHTML)
ui <- fluidPage(
tags$head(tags$script(HTML(js))),
br(),
sliderInput("slider", "Sample size", min = 10, max = 50, value = 20),
br(),
DTOutput("dtable")
)
server <- function(input, output, session){
output[["dtable"]] <- renderDT({
datatable(dat, escape = -ncol(dat)-1)
})
observeEvent(input[["button"]], {
showNotification("Creating report...", type = "message")
tmpReport <- tempfile(fileext = ".Rmd")
file.copy("report.Rmd", tmpReport)
outfile <- file.path(tempdir(), "report.html")
render(tmpReport, output_file = outfile,
params = list(
data = dat[input[["button"]], -ncol(dat)],
n = input[["slider"]]
)
)
b64 <- dataURI(
file = outfile,
mime = "text/html"
)
session$sendCustomMessage("download", b64)
})
}
shinyApp(ui, server)
The rmd file:
---
title: "Dynamic report"
output: html_document
params:
data: "x"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
Row contents:
```{r}
params$data
```
A plot of `params$n` random points:
```{r}
plot(rnorm(params$n), rnorm(params$n))
```
I have a big shiny app and I'm making a downloadable pdf with rmarkdownfrom the content in it. The problem I'm having is that all the plots are in plotlyand I haven't found how to plot 2 plots in the same row of the pdf file, in R it would be a simple subplot but it doesn't work.
This is a toy example of what I have:
shinyApp(
ui = fluidPage(
downloadButton("reporte", "Generate report"),
plotlyOutput("plotTest"),
plotlyOutput("plotHist")
),
server = function(input, output) {
library(webshot)
data = as.data.frame(rnorm(1000))
plotTest = plot_ly(y = ~rnorm(1000),type = "scatter",mode = "lines")
plotHist = plot_ly(x = ~rnorm(1000),type = "histogram")
output$plotTest = renderPlotly({plotTest})
output$plotHist = renderPlotly({plotHist})
output$reporte <- downloadHandler(
filename = "reporte.pdf",
content = function(file) {
tempReport <- file.path("C:/Users/Alejandro/Documents/test", "report.Rmd")
file.copy("report.Rmd", tempReport, overwrite = TRUE)
params <- list(n=plotTest,k=plotHist)
rmarkdown::render(tempReport, output_file = file,
params = params,
envir = new.env(parent = globalenv())
)
}
)
}
)
report.Rmd:
---
title: "Ensayo Reporte"
output: pdf_document
always_allow_html: yes
params:
n: NA
k: NA
---
```{r,echo=FALSE}
library(plotly)
tmpFile <- tempfile(fileext = ".png")
export(params$n, file = tmpFile)
export(params$k, file = tmpFile)
```
Adding the ususal fig.align='center',fig.show='hold' doesn't work i'll just get: Warning: Error in : pandoc document conversion failed with error 43
The shiny app below is taken out of gallery. It allow user to choose a variable, build a linear regression and download report.
What if I do not know in advance how many plots and models user wants to build and include into report. Is it possible to create a report with dynamically added plots?
Server.R
function(input, output) {
regFormula <- reactive({
as.formula(paste('mpg ~', input$x))
})
output$regPlot <- renderPlot({
par(mar = c(4, 4, .1, .1))
plot(regFormula(), data = mtcars, pch = 19)
})
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},
content = function(file) {
src <- normalizePath('report.Rmd')
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()
))
file.rename(out, file)
}
)
}
ui.R
fluidPage(
title = 'Download a PDF report',
sidebarLayout(
sidebarPanel(
helpText(),
selectInput('x', 'Build a regression model of mpg against:',
choices = names(mtcars)[-1]),
radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
inline = TRUE),
downloadButton('downloadReport')
),
mainPanel(
plotOutput('regPlot')
)
)
)
report.Rmd
Here is my regression model:
```{r model, collapse=TRUE}
options(digits = 4)
fit <- lm(regFormula(), data = mtcars)
b <- coef(fit)
summary(fit)
```
The fitting result is $mpg = `r b[1]` + `r b[2]``r input$x`$.
Below is a scatter plot with the regression line.
```{r plot, fig.height=5}
par(mar = c(4, 4, 1, 1))
plot(regFormula(), data = mtcars, pch = 19, col = 'gray')
abline(fit, col = 'red', lwd = 2)
```
Well, it looks like I have found the answer. The problem was in local/global variables. I had to put list initialisation outside server function. Also I had to use <<- instead of <- to assign new element to the plot rather than create new plot every time.
Many thanks to Peter Ellis to support!
So, the solution is (I have slightly changed initial code to focus on the important part):
server.R
library(ggplot2); library(shiny); library(grid); library(gridExtra)
plist <- list() # IMPORTANT - outside server function
shinyServer(function(input, output) {
output$regPlot <- renderPlot({
p <- do.call("grid.arrange", c(plotList(),
ncol=floor(sqrt(length(plotList())+1)),
top = "test"))
})
plotList <- eventReactive(input$plt2rprt, {
p <- ggplot(data = mtcars, aes_string(x = input$x, y = "mpg")) +
geom_point()
# isolate(
plist[[length(plist)+1]] <<- p #IMPORTATNT <<- instead of <-
# )
return(plist)
})
output$lengthOfList <- renderText({length(plotList())})
output$lll <- renderText({length(plist)})
output$downloadReport <- downloadHandler(
filename = function() {
paste('my-report', sep = '.', switch(
input$format, PDF = 'pdf', HTML = 'html', Word = 'docx'
))
},
content = function(file) {
src <- normalizePath('report.Rmd')
owd <- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, 'report.Rmd', overwrite = TRUE)
library(rmarkdown)
out <- render('report.Rmd', switch(
input$format,
PDF = pdf_document(), HTML = html_document(), Word = word_document()
))
file.rename(out, file)
}
)
}) #ShinyServer
ui.R
fluidPage(
title = 'Download a PDF report',
sidebarLayout(
sidebarPanel(
helpText(),
selectInput('x', 'Build a regression model of mpg against:',
choices = names(mtcars)[-1]),
actionButton("plt2rprt", label = "Include into report"),
hr(),
radioButtons('format', 'Document format', c('PDF', 'HTML', 'Word'),
inline = TRUE),
downloadButton('downloadReport')
),
mainPanel(
plotOutput('regPlot'),
#verbatimTextOutput("count"),
hr(),
textOutput("lengthOfList"),
textOutput("lll"),
helpText("test-test-test")
)
)
)
report.Rmd
Length of list of plots `r length(plotList())`
```{r plot, fig.height=5}
do.call("grid.arrange", c(plotList(),
ncol=floor(sqrt(length(plotList())+1)),
top = "test"))
```