RMarkdown in Shiny Application
I think knitting it and rendering a UI should work.
library(shiny)
library(knitr)
ui <- shinyUI(
fluidPage(
uiOutput('markdown')
)
)
server <- function(input, output) {
output$markdown <- renderUI({
HTML(markdown::markdownToHTML(knit('RMarkdownFile.rmd', quiet = TRUE)))
})
}
shinyApp(ui, server)
Use R Markdown .docx template in shiny app
Keep your rmarkdown and word template in app folder.
In your downloadhandler:
output$your_output <- downloadHandler(
filename = function() {
'output_title.docx'
},
content = function(file) {
tempReport <- file.path(temp_folder,
"rmarkdown_template.Rmd")
tempTemplate <- file.path(temp_folder, "template.docx")
file.copy("rmarkdown_template.Rmd", tempReport, overwrite = TRUE)
file.copy("template.docx", tempTemplate, overwrite = TRUE)
# Params
)
rmarkdown::render(tempReport, output_file = file, output_format = 'word_document',
params = params,
envir = new.env(parent = globalenv())
)
}
)
Then in your yaml:
output:
word_document:
reference_docx: template.docx
Passing a dataframe as a parameter from Shiny app to RMarkdown
There are several issues with your code. I'll go over them one by one
Invalid parameter in downloadHandler()
You are passing an object of class reactive
to the contentType
parameter of downloadHandler()
.
downloadHandler(
reactive(file <- input$file1), ## <--- here
filename = "report.doc",
content = function(file) {
# ...
}
)
It seems that this messes up the whole logic of downloadHandler()
and leads to "server error" messages on the client side with no errors or warnings from shiny.
This line needs to be removed in order to download files successfully
Reference the Rmd-parameter correctly
When you want to access the parameter from the Rmd report, you will need to use params$report.data
. Just using report.data
will lead to the following error: object 'report.data' not found
.
---
output: word_document
params:
report.data: NULL
---
```{r}
report.data <- params$report.data
# ...
```
Fix the path to the generated file
You are knitting the Rmd inside the temporary directory, which is generally a good idea. However, getting the paths right is not always that easy. In your case, I used the following
rendered_report <- rmarkdown::render(
tempReport, output_file = "wordreport.doc",
params = params,
envir = new.env(parent = globalenv())
)
file.copy(rendered_report, file)
The reason your version didn't work is that the generated report is created inside the temporary directory alogside tmpReport
. See the reference documentation of ?rmarkdown::render
for more details.
I used the return value of rmarkdown::render()
instead which holds an absolute path to the generated file. This is less error prone and especially useful if you do not know the file extension of the generated file in advance
Use read.csv to convert the uploaded file into a data.frame
Shiny doesn't automatically convert uploaded csv files into dataframes. You need to define a parsing logic to do that.
params <- list(report.data = read.csv(input$file1$datapath))
One final word
Try to get more organized with your coding projects and limit the scope of future SO questions to one issue at a time. Creating "minimal reproducible examples" might seem tedious at first, but there are several advantages in doing that
- Other people can read the questions and answers and reuse them in their own projects easily without dissecting a wall of code
- It is much easier to answer those questions. With questions like this, the SO community usually only provides comments because answering them properly requires a lot of effort
- Minimizing and isolating problems is a skill that will help you to figure out issues in your future coding projects much more easily
My shiny app does not render R code in markdown
I think the problem will be, that you are copying your prot.Rmd
to a tempfile with fileext = ".html"
- it should be ".Rmd"
.
Please try the following:
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Knit"),
downloadButton("prot", "Render file")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$prot <- downloadHandler(
# For PDF output, change this to "report.pdf"
filename = "prot.html",
content = function(file) {
report_path <- tempfile(fileext = ".Rmd")
file.copy("prot.Rmd", report_path, overwrite = TRUE)
rmarkdown::render(report_path,
output_file = file)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
How to pass a reactive plot generated in Shiny to Rmarkdown to generate dynamic reports
Basically your question already included all the building blocks. I only updated the report template to include the code to plot the radar chart. As a parameter I decided to pass the filtered dataset. In the server I only adjusted the specs for the params
:
server <- function(input, output) {
output$plot1 <- renderChartJSRadar({
chartJSRadar(skills[, c("Label", input$selectedPeople)],
maxScale = 10, showToolTipLabel=TRUE)
})
output$report <- downloadHandler(
filename = "report.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)
```
Related Topics
1-Dimensional Matrix Is Changed to a Vector in R
Extract Last Non-Missing Value in Row with Data.Table
Repeat the Re-Sampling Function for 1000 Times? Using Lapply
R Ggplot Ordering Bars in "Barplot-Like " Plot
Why Does Mapply Not Return Date-Objects
How to Change the Default Directory in Rstudio (Or R)
Connect Ggplot Boxplots Using Lines and Multiple Factor
Is There a Limit for the Possible Number of Nested Ifelse Statements
How to Print a Variable Inside a for Loop to the Console in Real Time as the Loop Is Running
How to 'Unlist' a Column in a Data.Table
Rscript Could Not Find Function
Use Dplyr to Concatenate a Column
Implementation of Skyline Query or Efficient Frontier
Mutate Multiple/Consecutive Columns (With Dplyr or Base R)
Finding Maximum Value of One Column (By Group) and Inserting Value into Another Data Frame in R