我正在尝试创建一个 Shiny 的应用程序。在此特定应用中,我有一组单选按钮,其中选择一个将在下面显示一组选项,如“复选框”,而选择另一个单选按钮将选择其他选项。请在下面找到用户界面和服务器代码。
library(shiny)
library(shinydashboard)
library(shinyWidgets)
d <-
data.frame(
year = c(1995, 1995, 1995, 1996, 1996, 1996, 1997, 1997, 1997),
Product_Name = c(
"Table",
"Chair",
"Bed",
"Table",
"Chair",
"Bed",
"Table",
"Chair",
"Bed"
),
Product_desc = c("X", "X", "X", "Y", "Y", "Y", "Z", "Z", "Z"),
Cost = c(1, 2, 3, 4, 2, 3, 4, 5, 6)
)
ui <- shinyUI(fluidPage(
useShinydashboard(),
tabPanel(
"Plot",
sidebarLayout(
sidebarPanel(
radioButtons(
"Choose",
"Choose One",
c("Year" = "p", "Numbers" = "l")
),
uiOutput('checkbox'),
#width = 2,
position = "bottom"),
mainPanel(uiOutput("graph"))
)
)
))
server <- function(input, output, session) {
output$graph <- renderUI({
# create tabPanel with datatable in it
req(input$year)
tabPanel("Plots",
fluidRow(lapply(as.list(paste0("plot", seq_along(input$year))), plotOutput)))
})
output$checkbox <- renderUI({
if (input$Choose == "p") {
checkboxGroupInput("Cross",
"Year",
choices = (unique(d$year)))
} else{
checkboxGroupInput("Cross_1",
"Numbers",
choices = c("1","2","3"))
}
})
}
shinyApp(ui, server)
现在我面临的挑战是,当我选择“年份”单选按钮时,年份值将显示为复选框,而当我选择复选框时,相应的图形将显示在主面板中(我在此处未提供代码) )。现在,当我选择“数字”单选按钮时,将显示正确的复选框选项。但是当我回到“年”单选按钮时。已经选择的复选框值不会保留,因此用户需要再次选中复选框以获取相应的图形。
在我的情况下,CheckboxGroupInput()中的选择是动态的。因此,我将无法在UI()中给出此选择。我也尝试使用updateCheckboxGroupInput()失败。通过再次对代码进行一些调查,我了解到发生问题的地方就是代码的这一部分。
output$checkbox <- renderUI({
if (input$Choose == "p") {
checkboxGroupInput("Cross",
"Year",
choices = (unique(d$year)))
} else{
checkboxGroupInput("Cross_1",
"Numbers",
choices = c("1","2","3"))
}
})
即使选择了另一个单选按钮,也请向我建议如何保留复选框中的值。还请注意,每个单选按钮下的复选框的选项将根据用户在上一个选项卡中选择的选项而更改。因此,我使用观察功能来获取相应的复选框。
最佳答案
您可以存储试图保留为无功值的年份的值。
global <- reactiveValues(years = NULL)
observeEvent(input$Cross, {
global$years <- input$Cross
})
然后,使您的动态ui取决于 react 性值:
checkboxGroupInput(
"Cross",
"Year",
choices = (unique(d$year)),
selected = global$years
)
可重现的示例:
library(shiny)
library(shinydashboard)
library(shinyWidgets)
d <-
data.frame(
year = c(1995, 1995, 1995, 1996, 1996, 1996, 1997, 1997, 1997),
Product_Name = c(
"Table",
"Chair",
"Bed",
"Table",
"Chair",
"Bed",
"Table",
"Chair",
"Bed"
),
Product_desc = c("X", "X", "X", "Y", "Y", "Y", "Z", "Z", "Z"),
Cost = c(1, 2, 3, 4, 2, 3, 4, 5, 6)
)
ui <- shinyUI(fluidPage(
useShinydashboard(),
tabPanel(
"Plot",
sidebarLayout(
sidebarPanel(
radioButtons(
"Choose",
"Choose One",
c("Year" = "p", "Numbers" = "l")
),
uiOutput('checkbox'),
#width = 2,
position = "bottom"),
mainPanel(uiOutput("graph"))
)
)
))
server <- function(input, output, session) {
global <- reactiveValues(years = NULL)
observeEvent(input$Cross, {
global$years <- input$Cross
})
output$graph <- renderUI({
# create tabPanel with datatable in it
req(input$year)
tabPanel("Plots",
fluidRow(lapply(as.list(paste0("plot", seq_along(input$year))), plotOutput)))
})
output$checkbox <- renderUI({
if (input$Choose == "p") {
checkboxGroupInput("Cross",
"Year",
choices = (unique(d$year)),selected = global$years)
} else{
checkboxGroupInput("Cross_1",
"Numbers",
choices = c("1","2","3"))
}
})
}
shinyApp(ui, server)
我认为Ash的Shinyjs隐藏/显示变体也很有趣。我还没有测试过,但是我想您可以将动态ui替换为动态ui(?)。
关于r - 如何在R Shiny App的复选框中保留值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61382981/