Inputs are not updated after using eventReactive.

huangapple go评论65阅读模式
英文:

Inputs are not updated after using eventReactive

问题

在Shiny中,我正在创建一个应用程序,根据用户选择的输入生成报告。报告是在用户单击"生成"按钮时创建的。有三种类型的输入:

(1) "input_type":一个SelectInput,用于让用户选择要生成的报告类型。选项有"Global"和"Por instituciones"。

(2) "param":一个SelectInput,只有当"input_type"等于"Por instituciones"时才会显示。

(3) "date":一个SelectInput,允许用户定义要生成的报告的日期。

问题是:

当首先选择"Por instituciones"选项时,代码按预期运行:报告会随着"date"和"param"中的不同选择而更改。

然而,如果出现以下过程:

  1. 在"input_type"中选择"Global"选项。
  2. 单击"生成"按钮生成报告。
  3. 然后在"input_type"中选择"Por instituciones"。
  4. 再次单击"生成"按钮生成报告。

那么"param"中选择的选项就不再重要。input$param等于在使用"Global"选项生成报告之前选择的最后一个值。

以下是你的代码的翻译:

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

希望这有所帮助。如果需要进一步解释或有其他问题,请随时提出。

英文:

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 &lt;- fluidPage(
sidebarLayout(
sidebarPanel(
titlePanel(&quot;Repoter&#237;a seguimiento PEIPRDCI&quot;),
selectInput(&quot;input_type&quot;, &quot;Tipo reporte&quot;,
c(&quot;Global&quot;, &quot;Por instituciones&quot;)), 
uiOutput(&quot;ui&quot;), 
selectInput(&quot;date&quot;, &quot;Seleccione la fecha:&quot;, 
choices = c(&quot;2023-01-01&quot;, &quot;2023-02-01&quot;)), 
actionButton(&quot;generate&quot;, &quot;Generar reporte&quot;), 
downloadButton(&#39;downloadReport&#39;, &quot;Descargar&quot;)
),
mainPanel(
htmlOutput(&quot;report&quot;)
)
)
)
server &lt;- function(input, output) {
output$ui &lt;-  renderUI({
if (input$input_type == &quot;Por instituciones&quot;) {
selectInput(&quot;param&quot;, &quot;Seleccione la instituci&#243;n:&quot;, 
choices = c(&quot;BDE&quot;, &quot;DIGERCIC&quot;))
} 
})
a &lt;-  eventReactive(input$generate, {
if(input$input_type == &quot;Por instituciones&quot;)  {
print(input$param)
rmarkdown::render(&quot;C:/Users/santy/Documents/test5.Rmd&quot;,
params = list(institucion = input$param,
fecha = input$date))
includeHTML(&quot;C:/Users/santy/Documents/test5.html&quot;)
} else {
print(input$param)
rmarkdown::render(&quot;C:/Users/santy/Documents/Reporte_seguimiento110523.Rmd&quot;,
params = list(fecha = input$date))
includeHTML(&quot;C:/Users/santy/Documents/Reporte_seguimiento110523.html&quot;)
}
})
output$report &lt;-  renderUI({
a()
})
output$downloadReport &lt;- downloadHandler(
filename = function() {
paste(&#39;my-report&#39;, sep = &#39;.&#39;, &#39;html&#39;)
},
content = function(file) {
src &lt;- normalizePath(&#39;C:/Users/santy/Documents/test5.Rmd&#39;)
# temporarily switch to the temp dir, in case you do not have write
# permission to the current working directory
owd &lt;- setwd(tempdir())
on.exit(setwd(owd))
file.copy(src, &#39;test5.Rmd&#39;, overwrite = TRUE)
library(rmarkdown)
out &lt;- a()
file.rename(out, file)
}
)
}
shinyApp(ui = ui, server = server)

Any ideas?

答案1

得分: 1

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

ui &lt;- 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")
    )  
  )
)

(Note: I have removed the HTML entities for special characters like "í" and "ó" for better readability.)

英文:

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

ui &lt;- fluidPage(
sidebarLayout(
sidebarPanel(
titlePanel(&quot;Repoter&#237;a seguimiento PEIPRDCI&quot;),
selectInput(&quot;input_type&quot;, &quot;Tipo reporte&quot;,
c(&quot;Global&quot;, &quot;Por instituciones&quot;)),
conditionalPanel(
condition = &quot;input.input_type != &#39;Global&#39;&quot;, 
selectInput(&quot;param&quot;, &quot;Seleccione la instituci&#243;n:&quot;, 
choices = c(&quot;BDE&quot;, &quot;DIGERCIC&quot;))
),
selectInput(&quot;date&quot;, &quot;Seleccione la fecha:&quot;, 
choices = c(&quot;2023-01-01&quot;, &quot;2023-02-01&quot;)), 
actionButton(&quot;generate&quot;, &quot;Generar reporte&quot;), 
downloadButton(&#39;downloadReport&#39;, &quot;Descargar&quot;),
), 
mainPanel(
htmlOutput(&quot;report&quot;)
)  
)
)

huangapple
  • 本文由 发表于 2023年5月22日 13:54:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303347.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定