I have a Shiny app with multiple tabs. On one of the tabs, I have an option between base 1
and base 2
. Choosing base 1
leads to a checkboxgroup with many different options while base 2
leads to a simple selectInput where can you choose one of only two options.
When choosing base 1
I have added select all
and deselect all
buttons (in the form of action links). I would like to show these buttons only when base 1
is selected. My first instinct was to put the two action links into the same renderUI
as the checkboxgroup, but then the observe
function to update the checkboxgroup input did not recognize the action link IDs and crashed.
So I thought maybe I can simply hide them when they are not needed using shinyjs
, but this does not seem to work either. Below is my sample code (I have chosen to include two tabs, since all answers I could find online did not work for me and consisted of problems with only one tab present).
library(shiny)
library(shinydashboard)
library(shinyjs)
UI:
ui <- dashboardPage(
dashboardHeader(title = "First Past Yield"),
dashboardSidebar(
sidebarMenu(
menuItem("Tab 1", tabName = "tab1", icon = icon("stats",lib = "glyphicon")),
menuItem("Tab 2", tabName = "tab2", icon = icon("dashboard",lib = "glyphicon"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "tab1",
fluidRow(
column(width = 4,
box(title = "Inputs",width = NULL,status = 'primary',solidHeader = TRUE,
checkboxGroupInput("some_input",h3("Some Input"),choices = list("Input 1" = 1,"Input 2" = 2,"Input 3" = 3),
selected = c(1,2),inline = TRUE))),
column(width = 8,
box(title = "Outputs",width = NULL,status = "primary", solidHeader = TRUE, "Some Output")))),
tabItem(tabName = "tab2",
fluidRow(
column(width = 4,
box(title = "Inputs",width = NULL,status = 'primary',solidHeader = TRUE,
selectInput("base",h3("Basis"),choices = list("Base 1" = 1,"Base 2" = 2),selected = 1),
uiOutput("basestuff"),
actionLink("selectall","Select All"),
actionLink("selectnone",HTML(paste(' ',"Deselect All")))
)),
column(width = 8,
box(title = "Outputs",width = NULL,status = "primary", solidHeader = TRUE, "Some Output")))))))
Server:
server <- function(input,output,session){
output$basestuff <- renderUI({
if(input$base == 1){
checkboxGroupInput("many_choices",h3("Many Choices"),choices = list("a" = 1,"b" = 2,"c" = 3,"d" = 4,"e" = 5, "f" = 6, "g" = 7),
selected = 1:7,inline = TRUE)
}
else if(input$base == 2){
selectInput("few_choices",h3("Few Choices"),choices = list("a" = 1,"b" = 2),selected = 1)
}
})
observe({
shinyjs::toggle(id = "selectall",condition = input$base == 1)
})
observe({
shinyjs::toggle(id = "selectnone",condition = input$base == 1)
})
observe({
if(input$selectall == 0) return(NULL)
else if (input$selectall > 0){
updateCheckboxGroupInput(session,"many_choices",choices = list("a" = 1,"b" = 2,"c" = 3,"d" = 4,"e" = 5, "f" = 6, "g" = 7),
selected = 1:7,inline = TRUE)}
})
observe({
if(input$selectnone == 0) return(NULL)
else if (input$selectnone > 0){
updateCheckboxGroupInput(session,"many_choices",choices = list("a" = 1,"b" = 2,"c" = 3,"d" = 4,"e" = 5, "f" = 6, "g" = 7),inline = TRUE)
}
})
}
shinyApp(ui = ui, server = server)
CodePudding user response:
Add this instead of the two toggles: and dont forget to put useShinyjs(),
into the dashboardBody
library(shiny)
library(shinydashboard)
library(shinyjs)
ui <- dashboardPage(
dashboardHeader(title = "First Past Yield"),
dashboardSidebar(
sidebarMenu(
menuItem("Tab 1", tabName = "tab1", icon = icon("stats",lib = "glyphicon")),
menuItem("Tab 2", tabName = "tab2", icon = icon("dashboard",lib = "glyphicon"))
)
),
dashboardBody(
useShinyjs(),
tabItems(
tabItem(tabName = "tab1",
fluidRow(
column(width = 4,
box(title = "Inputs",width = NULL,status = 'primary',solidHeader = TRUE,
checkboxGroupInput("some_input",h3("Some Input"),choices = list("Input 1" = 1,"Input 2" = 2,"Input 3" = 3),
selected = c(1,2),inline = TRUE))),
column(width = 8,
box(title = "Outputs",width = NULL,status = "primary", solidHeader = TRUE, "Some Output")))),
tabItem(tabName = "tab2",
fluidRow(
column(width = 4,
box(title = "Inputs",width = NULL,status = 'primary',solidHeader = TRUE,
selectInput("base",h3("Basis"),choices = list("Base 1" = 1,"Base 2" = 2),selected = 1),
uiOutput("basestuff"),
actionLink("selectall","Select All"),
actionLink("selectnone",HTML(paste(' ',"Deselect All")))
)),
column(width = 8,
box(title = "Outputs",width = NULL,status = "primary", solidHeader = TRUE, "Some Output")))))))
server <- function(input, output, session) {
output$basestuff <- renderUI({
if(input$base == 1){
checkboxGroupInput("many_choices",h3("Many Choices"),choices = list("a" = 1,"b" = 2,"c" = 3,"d" = 4,"e" = 5, "f" = 6, "g" = 7),
selected = 1:7,inline = TRUE)
}
else if(input$base == 2){
selectInput("few_choices",h3("Few Choices"),choices = list("a" = 1,"b" = 2),selected = 1)
}
})
observeEvent(input$base,{
if(input$base == 1){
shinyjs::show("selectall")
shinyjs::show("selectnone")
}else{
shinyjs::hide("selectall")
shinyjs::hide("selectnone")
}
})
observe({
if(input$selectall == 0) return(NULL)
else if (input$selectall > 0){
updateCheckboxGroupInput(session,"many_choices",choices = list("a" = 1,"b" = 2,"c" = 3,"d" = 4,"e" = 5, "f" = 6, "g" = 7),
selected = 1:7,inline = TRUE)}
})
observe({
if(input$selectnone == 0) return(NULL)
else if (input$selectnone > 0){
updateCheckboxGroupInput(session,"many_choices",choices = list("a" = 1,"b" = 2,"c" = 3,"d" = 4,"e" = 5, "f" = 6, "g" = 7),inline = TRUE)
}
})
}
shinyApp(ui, server)