2021年4月2日星期五

Use Rmarkdown within shiny to create pdf

I want to create a shiny app that creates pdf reports using markdown.

so far:

I can create a pdf with shiny in two different ways:

  • A : using renderUI( and pdf())
  • B : using downloadHandler( und rmarkdown::render() )

This gives me:

  • A : an embedded pdf but no markdown features (i.e Yaml,params)
  • B : Any Rmarkdown style pdf i can think of - but no embedding

A mimimal example:

library(shiny)    ui <- shinyUI(fluidPage(              sidebarLayout(          sidebarPanel(              ## for the embedded rendering:              actionButton("generate", "show PDF"),                ## for the markdown- report:              downloadButton("report", "download pdf"),               textInput("title" , "Title", "Some Great Title"),              textInput("author" , "Name", "authors You Me"),              textInput("content" , "Content", "Make an rmarkdown report using this shinyapp")          ),          # embedded rendering          mainPanel(              uiOutput("pdfview")          )      )  ))    server <- shinyServer(function(input, output) {      ## embedded pdf      observeEvent(input$generate, {          output$pdfview <- renderUI({              pdf("www/myreport.pdf")              hist(rnorm(100))              dev.off()              tags$iframe(style="height:600px; width:100%", src="myreport.pdf")          })      })      ## pdf download using rmarkdown      output$report <- downloadHandler(          filename = "report.pdf",          content = function(file) {          tempReport <- file.path(tempdir(), "file.rmd")          file.copy("file.rmd", tempReport, overwrite = TRUE)          cat(paste0('---  title: ', input$title,  '\nauthor: ', input$author,  '\noutput: pdf_document  ---  # Nice Report  ',input$content,  '\n```{r}  hist(rnorm(100))  ``` '),file="file.rmd")  rmarkdown::render("file.rmd","pdf_document",output_file = file, envir = new.env(parent = globalenv()))          }          )            })    shinyApp(ui = ui, server = server)    

How can i get an rmardown rendered pdf embedded into the shiny app?

https://stackoverflow.com/questions/66926884/use-rmarkdown-within-shiny-to-create-pdf April 03, 2021 at 09:08AM

没有评论:

发表评论