【发布时间】:2021-10-30 18:31:12
【问题描述】:
我正在构建我的第一个闪亮的应用程序,我需要一个反应式选择器输入。感谢@YBS,我成功构建了这个[反应式选择器输入][1]。
但是,在选择器输入中,我不想显示值,而是显示标签。例如在下面的代码中,我没有显示year_1,而是显示了标签2001。我想对picker_cny 做同样的事情,而不是显示codebook$Polygon我想显示codebook$Label(即而不是显示polygon_a我想在选择器输入中显示Baron Dubois)。
我想过写choices = paste(c(unique(codebook$Polygon), " = " unique(codebook$Label)),
有什么想法吗?谢谢!
这是数据:
structure(list(X = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Polygon = c("polygon_a",
"polygon_a", "polygon_a", "polygon_a", "polygon_a", "polygon_a",
"polygon_a", "polygon_a", "polygon_b", "polygon_b", "polygon_b",
"polygon_b", "polygon_b", "polygon_b", "polygon_b", "polygon_b",
"polygon_c", "polygon_c", "polygon_c", "polygon_c"), Label = c("Baron Dubois",
"Baron Dubois", "Baron Dubois", "Baron Dubois", "Baron Dubois",
"Baron Dubois", "Baron Dubois", "Baron Dubois", "Baron Delasalle",
"Baron Delasalle", "Baron Delasalle", "Baron Delasalle", "Baron Delasalle",
"Baron Delasalle", "Baron Delasalle", "Baron Delasalle", "Baron Istog",
"Baron Istog", "Baron Istog", "Baron Istog"), Year = c("year_1",
"year_1", "year_1", "year_1", "year_2", "year_2", "year_2", "year_2",
"year_1", "year_1", "year_1", "year_1", "year_2", "year_2", "year_2",
"year_2", "year_1", "year_1", "year_1", "year_1"), Variable = c("Variable1",
"Variable2", "Variable3", "Variable4", "Variable1", "Variable2",
"Variable3", "Variable4", "Variable1", "Variable2", "Variable3",
"Variable4", "Variable1", "Variable2", "Variable3", "Variable4",
"Variable1", "Variable2", "Variable3", "Variable4"), Value = c(1L,
245L, 23L, 2L, 0L, 34L, 1L, 245L, 1L, 23L, 2L, 0L, 0L, 34L, 0L,
34L, 0L, 34L, 90L, 9L)), class = "data.frame", row.names = c(NA,
-20L))
代码如下:
# Creation of APP ------------------------------------------
remove(ui, server)
if (interactive()) {
library(shiny)
library(shinyWidgets)
library(shinythemes)
library(shinycssloaders)
library(shinydashboard)
##########
# Define UI -----------------------------------------------
ui <- fluidPage(
# Application title
titlePanel("Colonial Concessions Within DRC"),
# Parameters
sidebarLayout(
sidebarPanel(
selectInput(inputId = "input_period", label = "Period",
choices = c("2001" = "year_1", "2002" = "year_2", "2003" = "year_3")),
pickerInput(
inputId = "picker_cny",
label = "Select Polygon",
choices = unique(codebook$Polygon),
options = list(`actions-box` = TRUE),
multiple = TRUE),
width = 2),
# Displat the reactive map
mainPanel(
DTOutput("t1"),
width = 10)
))
# Define Server ------------------------------------------
server <- function(input, output, session) {
output$t1 <- renderDT({
# Display data only when one polygon is selected
code1 <- codebook[codebook$Year == input$input_period & (codebook$Polygon %in% input$picker_cny),]
code1
})
# Reactive pickerInput ---------------------------------
observeEvent(input$input_period, {
# Generate reactive picker input
code1 <- codebook[codebook$Year %in% input$input_period,]
codeu <- unique(codebook$Polygon)
code1u <- unique(code1$Polygon)
disabled_choices <- ifelse(codeu %in% code1u, 0,1)
#print(disabled_choices)
updatePickerInput(session = session,
inputId = "picker_cny",
choices = unique(codebook$Polygon),
choicesOpt = list(
disabled = disabled_choices,
style = ifelse(disabled_choices,
yes = "color: rgba(119, 119, 119, 0.5);",
no = "")
)
)
}, ignoreInit = TRUE)
}
# Run the application
shinyApp(ui = ui, server = server)
}
``
[1]: https://stackoverflow.com/questions/68994999/update-picker-input-reuse-selected-data/68999463#68999463
【问题讨论】: