Skip to content

Help needed: generating downloadable reports in golem #969

Description

@YonghuiDong

Hi developers,

I want to generate a downloadable HTML reports with golem. What I want to do is to included some generated tables and figures in the report.

These tables and figures are produced in different shiny modules. How can I include them in params, should I convert them into global variables?

Below is my minimal example. n is an output from the same module, and It works. When I add the second param barplot() from another module (the module is not shown here), it pops up an error: Error in barplot: could not find function "barplot"

I know transform them into global variable should work. I am wondering if there is a suggested way under the golem framework?

Thanks a lot.

Dong

library(shiny)

# UI
choice_ui <- function(id) {
  ns <- NS(id)
  tagList(
    sliderInput(inputId = ns("slider"), 
                label = "Slider",  
                min = 1, 
                max = 100, 
                value = 50),
    downloadButton(outputId = ns("report_button"), 
                   label = "Generate report"
                   )
    )
}

# Server
choice_server <- function(id) {
  moduleServer(id, function(input, output, session){
    output$report_button<- downloadHandler(
      filename = "report.html",
      content = function(file) {
        tempReport <- file.path(tempdir(), "myRport.Rmd")
        file.copy("www/myReport.Rmd", tempReport, overwrite = TRUE)
        params <- list(n = input$slider,
                       mybar = barplot() # from another module
                        )
        rmarkdown::render(tempReport, output_file = file,
                          params = params,
                          envir = new.env(parent = globalenv())
                          )
        }
      )
    }
  )
}

# Application
library(shiny)
app_ui <- function() {
  fluidPage(
    choice_ui("choice_ui_1")
  )
}

app_server <- function(input, output, session) {
  r <- reactiveValues()
  choice_server("choice_ui_1")
}

shinyApp(app_ui, app_server)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions