【问题标题】:Create one or more shiny widgets based on selection of another shiny widget根据对另一个闪亮小部件的选择创建一个或多个闪亮小部件
【发布时间】:2020-09-04 16:16:26
【问题描述】:

我有下面的闪亮应用程序,用户可以在其中从 dt data.frame 中选择一个或多个列名

name<-c("John","Jack","Bill")
value1<-c(2,4,6)
add<-c("SDF","GHK","FGH")
value2<-c(3,4,5)
dt<-data.frame(name,value1,add,value2)

那么对于他所做的每一个选择,相对的selectInput() 可能会显示在下面。也许我可以在每种情况下使用if,但问题是我的真实数据集可能要大得多,并且每次都有不同的列名,所以我正在寻找一种方法来立即将pickerInput() 选择的名称与相关的创建联系起来selectInput() 将包括列值。例如,如果有人选择了所有 4 个列名,则应使用相对列名作为标签创建 4 个 selectInputs。

library(shiny)
library(shinyWidgets)

# ui object
ui <- fluidPage(
    titlePanel(p("Spatial app", style = "color:#3474A7")),
    sidebarLayout(
        sidebarPanel(
            pickerInput(
                inputId = "p1",
                label = "Select Column headers",
                choices = colnames( dt),
                multiple = TRUE,
                options = list(`actions-box` = TRUE)
            ),
            uiOutput("widg")
            
            
        ),
        
        mainPanel(
            
        )
    )
)

# server()
server <- function(input, output) {
   
    output$widg<-renderUI({
        
    })
    
   
}

# shinyApp()
shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    高级别的,我会在 UI 中静态创建 selectInput,并在您的选择器更改时考虑使用 updateSelectInput。大致是这样的(注意在服务器函数中添加了session 变量):

    ui <- fluidPage(
      titlePanel(p("Spatial app", style = "color:#3474A7")),
      sidebarLayout(
        sidebarPanel(
          pickerInput(
            inputId = "p1",
            label = "Select Column headers",
            choices = colnames( dt),
            multiple = TRUE,
            options = list(`actions-box` = TRUE)
          ),
          selectInput("columnValues", "", choices = "")
          
          
        ),
        
        mainPanel(
          
        )
      )
    )
    
    # server()
    server <- function(input, output, session) {
      
      observeEvent(input$p1, {
        
        updateSelectInput(
          session, 
          inputId = "columnValues", 
          label = paste0("Choose from: ", input$p1),
          choices = dt[[input$p1]]
        )
        
      })
      
      
    }
    
    # shinyApp()
    shinyApp(ui = ui, server = server)
    

    我确实注意到您正在使用多选选择器,因此您必须修改在选择多个标题时如何连接列值(以及如何生成标签)。

    如果您不想在选择器中未选择任何内容的情况下使 selectInput 可见,则有多种方法可以切换其可见性,例如使用 conditionalPanel

    【讨论】:

    • 当我尝试选择第二个值时它会崩溃
    • 是的,这就是为什么需要修改labelchoices。我不确定所需的行为是什么。可能类似于purrr::reduce(input$p1, ~c(.x, dt[[.y]]), .init = NULL)。或者您是说您希望 selectInputs 的数量根据选择的标头而变化?
    • 如果你正在寻找后者,看看这篇文章:shiny.rstudio.com/articles/dynamic-ui.html
    • 是的,我想要第二个解决方案。如果我选择 4,我想要一个有 4 个选择输入的解决方案
    • 标签要与列名一致
    猜你喜欢
    • 2018-07-01
    • 2020-07-19
    • 2018-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-14
    • 2021-04-20
    • 2019-11-29
    相关资源
    最近更新 更多