【问题标题】:Initializing values which will be generated by renderUI初始化将由 renderUI 生成的值
【发布时间】:2018-07-08 04:03:54
【问题描述】:

我正在为我的闪亮应用程序开发一个数据选择组件。 输入是一个data.frame。 然后有一个动态数据选择 UI(由 renderUI() 实现)供用户按不同列选择数据。 默认情况下,我希望数据被完全选中,因此我将 checkboxGroupInput() 的选定参数设置为所有值。

但是,由于响应式表达式是惰性求值的,因此只有在每个 UI 组件都被 renderUI() 渲染后,数据表才会真正完成。这意味着即使我知道默认情况下所有行都被选中,我仍然需要单击 selectInput() 选项来初始化将由 renderUI 提供的值。

我想知道在shiny中实现这种数据选择组件UI的方法是什么?

运行示例代码在这里:

library(dplyr)
library(shiny)
set.seed(319)
df <- data.frame(A = sample(c("aa", "ab", "ac"), 100, T), 
             B = sample(c("ba", "bb", "bc"), 100, T), 
             C = sample(c("ca", "cb", "cc"), 100, T))

ui <-  fluidPage(
  titlePanel("Dynamically generated user interface components"),
  fluidRow(column(12,
          selectInput("cellsVars",
                      label = "Cell Attributes",
                      choices = c("A", "B", "C")),
          uiOutput("cellsCheckBox")
  ),
 fluidRow(column(12,
          dataTableOutput("table"))
       )
 )
)

server <- function(input, output) {
 output$cellsCheckBox <- renderUI({
 if(is.null(input$cellsVars) ) return()

switch(input$cellsVars,
       "A"      = wellPanel(
         checkboxGroupInput("A", label = "Donors",
                            choices = c("aa", "ab", "ac"),
                            selected = c("aa", "ab", "ac") )
       ),
       "B"   = wellPanel(
         checkboxGroupInput("B", label = "Tissue",
                            choices = c("ba", "bb", "bc"),
                            selected = c("ba", "bb", "bc"))
       ),
       "C"  = wellPanel(
         checkboxGroupInput("C", label = "Annotated Cell Type",
                            choices = c("ca", "cb", "cc"),
                            selected = c("ca", "cb", "cc"))
       )
  )
  })

  output$table <- renderDataTable({
    filtered <- df %>%  filter( (A %in% input$donors) & (B %in%    input$tissueType) & (C %in% input$cellType))
filtered
  })
}

shinyApp(用户界面,服务器)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    问题在于,checkboxGroupInput 在初始化时和未进行任何选择时都返回NULL。这使得仅基于该值很难区分。下面是解决这个问题的方法,reactiveValues 在未初始化时为 NULL,在取消选择所有选项时为 ''。

    请注意,您的脚本还有一个问题(或者是故意的?那就是每次从输入 A 转到 B 时,您都在重新创建整个 cellsCheckBox,因此每个checkboxGroupInput 也会再次初始化!因此,当您从A 去选择那里的选项,到B 并返回A 时,checkboxGroupInput A 再次选择了所有值。也许tabSetPanel 是更适合您的情况?

    library(dplyr)
    library(shiny)
    set.seed(319)
    df <- data.frame(A = sample(c("aa", "ab", "ac"), 100, T), 
                     B = sample(c("ba", "bb", "bc"), 100, T), 
                     C = sample(c("ca", "cb", "cc"), 100, T))
    
    ui <-  fluidPage(
      titlePanel("Dynamically generated user interface components"),
      fluidRow(column(12,
                      selectInput("cellsVars",
                                  label = "Cell Attributes",
                                  choices = c("A", "B", "C")),
                      uiOutput("cellsCheckBox")
      ),
      fluidRow(column(12,
                      dataTableOutput("table"))
      )
      )
    )
    
    server <- function(input, output) {
      output$cellsCheckBox <- renderUI({
        if(is.null(input$cellsVars) ) return()
    
        switch(input$cellsVars,
               "A"      = wellPanel(
                 checkboxGroupInput("A", label = "Donors",
                                    choices = c("aa", "ab", "ac"),
                                    selected = c("aa", "ab", "ac") )
               ),
               "B"   = wellPanel(
                 checkboxGroupInput("B", label = "Tissue",
                                    choices = c("ba", "bb", "bc"),
                                    selected = c("ba", "bb", "bc"))
               ),
               "C"  = wellPanel(
                 checkboxGroupInput("C", label = "Annotated Cell Type",
                                    choices = c("ca", "cb", "cc"),
                                    selected = c("ca", "cb", "cc"))
               )
        )
      })
    
      selections = reactiveValues(A=NULL,B=NULL,C=NULL)
      lapply(c('A','B','C'), function(x) {
        observeEvent(input[[x]],ignoreInit = T,{
          selections[[x]] <- ifelse(is.null(input[[x]]),'',input[[x]])}
        )
      })
    
      output$table <- renderDataTable({
        if(!is.null(selections$A))
          df <- df %>% filter(A %in% input$A)
        if(!is.null(selections$B))
          df <- df %>% filter(B %in% input$B)
        if(!is.null(selections$C))
          df <- df %>% filter(C %in% input$C)
        df
      })
    }
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2012-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 2021-01-23
      相关资源
      最近更新 更多