【问题标题】:Is there a way to group variables with the pickerInput function from the shinyWidgets package?有没有办法使用 shinyWidgets 包中的 pickerInput 函数对变量进行分组?
【发布时间】:2019-06-08 17:22:58
【问题描述】:

我的数据如下所示:

A B C

x 1 NA
x 2 NA
x 2 NA
y 1 NA
y 2 NA
z 2 NA
z 3 NA
z 3 NA
z 2 NA

我需要在我的 pickerInput 函数中对 A 的每个值进行分组

library(shiny)
library(shinyWidgets)

x <- read.csv("Example.csv", sep=";")

ui <- fluidPage(
    pickerInput("x", "Pick :", choices = x$A, multiple = T)
)
server <- function(input, output) {}
shinyApp(ui, server)

我希望选择框中的内容是:

x

是的

z

而不是我得到 x y z 的每次出现

那么我该如何对这些值进行分组呢?

我设法使用来自 shiny 包的传统 selectInput 函数来实现这一点,但是来自 shinyWidget 函数的“全选/取消全选”对我的目的来说真的很有趣

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:
    library(shiny)
    library(shinyWidgets)
    
    ui <- fluidPage(
        fileInput("file1", "Choose CSV File",
                  accept = c(
                      "text/csv",
                      "text/comma-separated-values,text/plain",
                      ".csv"),
        ),
        uiOutput("selection")
    )
    server <- function(input, output) {
    
        output$selection <- renderUI({
            inFile <- input$file1
    
            if (is.null(inFile))
                return(NULL)
    
            df <- read.csv(inFile$datapath)
    
            tagList(
            selectizeInput("dynamicselctor", "Pick: ", choices = unique(df[[1]]), multiple = TRUE),
            pickerInput("dynamicselctor2", "Pick: ", choices = unique(df[[1]]), multiple = TRUE)
            )
    
        })
    
    }
    shinyApp(ui, server)
    

    从服务器端渲染您的PickerInput 或selectizeInput 也是一种选择(但通常它应该可以工作)

    【讨论】:

    • 在这个例子中就像一个魅力,但它不适用于 updateSelectInput。我在示例中没有解释的是我的数据是使用 fileInput 上传的。你知道如何解决这个问题吗?感谢您的回复!
    • @Henry 我不明白为什么这不适用于 updateSelectInput 或数据是否已上传。应用unique 返回唯一值,那又怎样?
    • 我也不知道,choices = unique(df$A) 对我不起作用,我仍然每次出现 A。但我用 @DSGym 的代码和一切正常。将 pickerInput 函数放入 renderUI 是一个好主意。非常感谢你们!
    猜你喜欢
    • 2021-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 2021-01-14
    • 2021-11-16
    • 2011-02-26
    • 2021-12-02
    相关资源
    最近更新 更多