使用eventReactive后,输入不会更新

tf7tbtn2  于 2023-05-26  发布在  React
关注(0)|答案(1)|浏览(135)

在shiny中,我创建了一个应用程序,它可以根据用户选择的输入生成报告。当用户点击按钮“生成”时,报告被创建。有三种类型的输入:
(1)“input_type”:一个SelectInput,用于让用户选择要生成的报告类型。选项为“Global”和“Por instituciones”。
(2)“param”:仅当“input_type”等于“Por instituciones”时必须显示的SelectInput。
(3)“date”:一个SelectInput,允许用户定义他们想要生成的报告的日期。
问题如下:
当首先选择“Por instituciones”选项时,代码按预期运行:报告随着“日期”和“参数”中的不同选择而改变。
但是,如果发生以下过程:
1.在“input_type”中,选择“Global”选项。
1.单击“生成”按钮生成报告。
1.在“input_type”中,选择“Por instituciones”。
1.单击“生成”按钮生成报告。
那么在“param”中选择的选项就不再重要了。input$param等于在使用“全局”选项生成报告之前选择的最后一个值。
代码如下:

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)

有什么想法吗

jexiocij

jexiocij1#

我最终使用conditionalPanel()而不是在服务器中定义输入来解决这个问题。像这样:

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")
    )  
  )
)

相关问题