简体   繁体   中英

Switching icons in action buttons for R shiny app

I have a sidebarPanel in a shiny app which has a few menu items which I dont need to show always. So I want to have a button which will expand and collapse the sidebarPanel. As of now I am able to do it with two different buttons. Is it possible to show just one button which switches its icons to right arrow when collapsed and left arrow when its expanded.

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  navbarPage("",
             tabPanel("tab",
                      div( id ="Sidebar",sidebarPanel(div(
                        id = "tab1-scrollspy",
                        class = "potential-scrollspy",
                        tags$ul(
                          class = "nav nav-pills nav-stacked",

                          tags$li(tags$a(href = "#section1-1", "Apple")),
                          tags$li(tags$a(href = "#section1-2", "Bananas")),
                          tags$li(tags$a(href = "#section1-3", "Oranges")),
                          tags$li(tags$a(href = "#section1-4", "Cherries"))
                        )
                      )
                      )),
                      mainPanel(actionButton("showSidebar", "", icon = icon("arrow-alt-circle-right")),
                                actionButton("hideSidebar", "", icon = icon("arrow-alt-circle-left"))
                      )
             )
  )
)

server <-function(input, output, session) {
  observeEvent(input$showSidebar, {
    shinyjs::show(id = "Sidebar")
  })
  observeEvent(input$hideSidebar, {
    shinyjs::hide(id = "Sidebar")
  })
}

shinyApp(ui, server)  
library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  navbarPage("",
             tabPanel("tab",
                      div( id ="Sidebar",sidebarPanel(div(
                        id = "tab1-scrollspy",
                        class = "potential-scrollspy",
                        tags$ul(
                          class = "nav nav-pills nav-stacked",

                          tags$li(tags$a(href = "#section1-1", "Apple")),
                          tags$li(tags$a(href = "#section1-2", "Bananas")),
                          tags$li(tags$a(href = "#section1-3", "Oranges")),
                          tags$li(tags$a(href = "#section1-4", "Cherries"))
                        )
                      )
                      )),
                      mainPanel(
                        actionButton("toggleSidebar", "", icon = icon("arrow-alt-circle-left"))
                      )
             )
  )
)

server <-function(input, output, session) {

  observeEvent(input$toggleSidebar, {
    shinyjs::toggle(id = "Sidebar")
    if(input$toggleSidebar %% 2 == 1){
      updateActionButton(session, "toggleSidebar", icon = icon("arrow-alt-circle-right"))
    }else{
      updateActionButton(session, "toggleSidebar", icon = icon("arrow-alt-circle-left"))
    }
  }, ignoreInit = TRUE)

}

shinyApp(ui, server)  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM