【问题标题】:radioButtons() vs. uiOutput() and observeEvent(input[[""]] - shinyradioButtons() 与 uiOutput() 和 observeEvent(input[[""]] - 闪亮
【发布时间】:2020-10-17 09:10:31
【问题描述】:

我尝试用闪亮的方式观察事件。如果我手动定义收音机,它将被正确观察 - 输出:print(paste0("SELECT * FROM daten;"))。我想避免在 ui.r 中编写十分之几的单选按钮。因此我在服务器部分写了一个循环。 但是相同的 observeEvent() 不会对我的“循环列表”单选按钮做出反应,这些单选按钮在闪亮的应用程序中正确构建。我不知道为什么。

我写了一个最小的例子:

library(shiny)

shinyApp(
  ui = fluidPage(
    
    ####### manually set radio #######
    print("This radio 'pd1' will be observed:"),
    radioButtons(inputId = "pd1", label = "value:", choices = c("?", "0", "1")),
    br(), br(),
    
    ####### versus looped set set radio #######
    
    uiOutput("scrlst"),
  ),
  
  server = function(input, output) {
    
    tablscr <- data.frame("1","question")
    
    ###################### observeEvent
    ##### "counter" for several items (in this case just 1 item)
    rv <- reactiveValues(counter = 0)
    lapply(1:dim(tablscr)[1], function(i) {
      isolate({qnum <- paste0('pd', rv$counter <- rv$counter + 1)})
      observeEvent(input[[qnum]], {print(paste0("SELECT * FROM daten;"))})
    })
    
    ### output for tenths of items in one loop  (in this case just 1 item)
    output$scrlst <- renderUI({
      tagList(
        scr <- list(),
        for (sq in 1:dim(tablscr)[1]){
          scr[[sq]] = list(sq,
                           print("This radio 'pd1' will not be observed:"),
                           radioButtons(inputId = "pd1", label = "value:", choices = c("?", "0", "1")),
                           br(),
                           br()
          )
        },
        return(scr),
      )
    })
  }
)

【问题讨论】:

  • 在循环中,您所有的f7Radios 都具有相同的 ID。那是行不通的。他们都需要有唯一的ID。与其尝试在主服务器中自己管理这些唯一 ID,不如将 f7Radio 转换为 module 并让 Shiny 为您完成繁重的工作。此外,在您的renderUIcr &lt;- list()return(scr) 中似乎在tagList() 内。这不太可能是正确的...如果您提供minimal working example,您更有可能获得帮助。哦,是的:欢迎来到 SO!
  • 谢谢!我稍微编辑了我的帖子并添加了一个最小的示例。不在乎身份证。这只是示例(并监视发生的情况)。实际上有一个柜台。
  • 哦,我在循环之外设置了scr &lt;- list()return(scr)。但没关系。

标签: shiny observers uioutput


【解决方案1】:

你的tagList 包含一个循环和一个返回语句听起来很奇怪。此外,您有一个重复的 id pd1。这是一个工作代码:

library(shiny)

shinyApp(
  ui = fluidPage(
    
    uiOutput("scrlst")
    
  ),
  
  server = function(input, output) {
    
    tablscr <- data.frame(c("1","2"), c("question", "hello"))
    
    lapply(1:nrow(tablscr), function(i) {
      qnum <- paste0('pd', i)
      observeEvent(input[[qnum]], {print(paste0("SELECT * FROM daten;"))})
    })
    
    output$scrlst <- renderUI({
      do.call(tagList, lapply(1:nrow(tablscr), function(i){
        tagList(
          radioButtons(paste0("pd", i), label = "value:", choices = c("?", "0", "1")),
          br(), br()
        )
      }))
    })
    
  }
)

【讨论】:

  • 这太棒了!它有效。谢谢!!是否有机会观察到两个具有相同输入 ID 的收音机?
猜你喜欢
  • 2015-08-03
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-02
  • 2020-05-24
  • 2015-11-13
相关资源
最近更新 更多