在 Rshiny 过滤器中显示替代名称导致没有默认选择

Displaying alternate names in Rshiny filter resulting in no default selection

我正在构建一个 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"),
....

根据我的实际数据,我的过滤器看起来像这样。我怎样才能让默认选择出现?

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

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)

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