首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >radioButtons()与uiOutput()和observeEvent(input[[""]] --闪亮

radioButtons()与uiOutput()和observeEvent(input[[""]] --闪亮
EN

Stack Overflow用户
提问于 2020-06-27 01:50:31
回答 1查看 105关注 0票数 0

我试着观察闪亮中的一个事件。如果我手动定义一个无线电,它将被正确地观察到- output:print(paste0("SELECT * FROM daten;"))。我希望避免在ui.r中编写十分之几的单选按钮。因此,我在服务器部分编写了一个循环。但是同样的observeEvent()对我的“循环列表”单选按钮没有反应,这些单选按钮正确地内置在闪亮的应用程序中。我不知道为什么。

我写了一个最小的例子:

代码语言:javascript
复制
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),
      )
    })
  }
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-27 20:12:46

包含循环和返回语句的tagList听起来很奇怪。而且,您有一个重复的id pd1。下面是一个有效的代码:

代码语言:javascript
复制
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()
        )
      }))
    })
    
  }
)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62600279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档