Search code examples
rshinyreportdata-analysis

Inputs are not updated after using eventReactive


In shiny, I am creating an app that generates reports based on the inputs that the user selects. The reports are created when the user clicks the button "generate". There are three types of inputs:

(1) "input_type": a SelectInput that is used to let the user select which type of report they want to generate. The options are "Global" and "Por instituciones".

(2) "param": a SelectInput that must be shown only if "input_type" is equal to "Por instituciones".

(3) "date": a SelectInput that lets the user define the date of the report they want to generate.

The problem is the following:

When the option "Por instituciones" is selected first, the code runs as expected: the report changes as different choices in "date" and "param".

However, if the following process occurs:

  1. In "input_type", "Global" option is selected.
  2. A report is generated clicking the "generate" button.
  3. In "input_type", "Por instituciones" is selected.
  4. A report is generated clicking the "generate" button.

Then the option selected in the "param" no longer matters. input$param is equal to the last value selected before generating the report with the "Global" option.

My code is the following:

library(shiny)
library(DT)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      titlePanel("Repotería seguimiento PEIPRDCI"),
      selectInput("input_type", "Tipo reporte",
                  c("Global", "Por instituciones")), 
      uiOutput("ui"), 
      selectInput("date", "Seleccione la fecha:", 
                  choices = c("2023-01-01", "2023-02-01")), 
      actionButton("generate", "Generar reporte"), 
      downloadButton('downloadReport', "Descargar")
      
    ),
    
    mainPanel(
 
      htmlOutput("report")
      
    )
  )
)


server <- function(input, output) {

  output$ui <-  renderUI({
      if (input$input_type == "Por instituciones") {
                selectInput("param", "Seleccione la institución:", 
                            choices = c("BDE", "DIGERCIC"))
    } 
  })


  
  a <-  eventReactive(input$generate, {
    
    if(input$input_type == "Por instituciones")  {
      
      print(input$param)
      rmarkdown::render("C:/Users/santy/Documents/test5.Rmd",
                        params = list(institucion = input$param,
                                      fecha = input$date))
      
      includeHTML("C:/Users/santy/Documents/test5.html")

    } else {
      
      print(input$param)
      rmarkdown::render("C:/Users/santy/Documents/Reporte_seguimiento110523.Rmd",
                        params = list(fecha = input$date))
      includeHTML("C:/Users/santy/Documents/Reporte_seguimiento110523.html")
    }
  })

  output$report <-  renderUI({
    
a()
    
  })
  

  output$downloadReport <- downloadHandler(
    filename = function() {
      paste('my-report', sep = '.', 'html')
    },
    
    content = function(file) {
      src <- normalizePath('C:/Users/santy/Documents/test5.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, 'test5.Rmd', overwrite = TRUE)
      
      library(rmarkdown)
      out <- a()
      file.rename(out, file)
    }
  )
  
  

}

shinyApp(ui = ui, server = server)

Any ideas?


Solution

  • I finally solved this problem using conditionalPanel() instead of defining the input in the server. Like this:

    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          titlePanel("Repotería seguimiento PEIPRDCI"),
          selectInput("input_type", "Tipo reporte",
                      c("Global", "Por instituciones")),
          conditionalPanel(
            condition = "input.input_type != 'Global'", 
            selectInput("param", "Seleccione la institución:", 
                        choices = c("BDE", "DIGERCIC"))
            
          ),
          selectInput("date", "Seleccione la fecha:", 
                      choices = c("2023-01-01", "2023-02-01")), 
          actionButton("generate", "Generar reporte"), 
          downloadButton('downloadReport', "Descargar"),
        ), 
        mainPanel(
          
          htmlOutput("report")
        )  
      )
    )