【问题标题】:Displaying alternate names in Rshiny filter resulting in no default selection在 Rshiny 过滤器中显示替代名称导致没有默认选择
【发布时间】:2019-05-02 15:28:30
【问题描述】:

我正在构建一个 Rshiny 应用程序,其中输入数据将始终具有相同的结构但不同的类别。无论这些数据是什么样子,我总是希望我的过滤器显示相同的值。我遵循了一些示例并认为这会起作用,但我无法设置默认选择。

此外,我很好奇当我在过滤器中为我的绘图和表格使用这些备用显示名称时,是否需要对 服务器 做任何事情,或者这只是对 Ui 的纯粹编辑?

The data would have a structure like this, 
but the values of "col_1" and "col_2" will always differ.
No matter what those values are, I always want the first value of "col_1" 
to appear in the Ui "Filter 1" as "Segment 1" and the first 
value of "col_2" to appear as "Group A" in "Filter 2" and so on...


col_1 <- c("Seg 1", "Seg 2")
col_2 <- c("A", "B")

x<-data.frame(Seg, Group)

#My ui looks something like this
....
pickerInput("Segment", "Filter 1",
    choices = c("Segement 1" =(x$Seg[1])[1],
              "Segment 2" = (x$Seg[2])[2]),
               selected = 'Segment 1', multiple = TRUE),
                    cellWidths = c("10%", "89%")
               ),
pickerInput("product_line", "Filter 2", multiple = TRUE,
      choices = c("Product A" = levels(x$Group[1]),
                  "Product B" = levels(x$Goup[2])),
                   selected = "Product A"),
....

使用我的实际数据,我的过滤器看起来像这样。如何让默认选择出现?

【问题讨论】:

    标签: r filter shiny selection shiny-server


    【解决方案1】:

    我猜你想要类似下面的东西:

    library(shiny)
    library(shinyWidgets)
    
    random_numbers <- as.integer(runif(5, 1, 26))
    
    col_1 <- paste("Seg", random_numbers)
    col_2 <- LETTERS[random_numbers]
    
    seg_choices <- seq_along(col_1)
    names(seg_choices) <- paste("Segment", seg_choices)
    
    grp_choices <- seq_along(col_2)
    names(grp_choices) <- paste("Product", LETTERS[grp_choices])
    
    ui <- fluidPage(
        pickerInput("segment", "Filter 1",
                    choices = seg_choices,
                    selected = seg_choices[1], multiple = TRUE),
        pickerInput("product", "Filter 2",
                    choices = grp_choices,
                    selected = grp_choices[1], multiple = TRUE),
        textOutput("selections")
    )
    
    server <- function(input, output, session) {
        selectedSegments <- reactive({
            col_1[as.integer(input$segment)]
        })
    
        selectedProducts <- reactive({
            col_2[as.integer(input$product)]
        })
    
        output$selections <- renderPrint({cat("selectedSegments: ", paste(selectedSegments(), collapse = ", "), " selectedProducts: ", paste(selectedProducts(), collapse = ", "))})
    }
    
    shinyApp(ui = ui, server = server)
    

    选择是通过索引提供的,而不是直接使用要过滤的基础数据的元素。

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多