Generic button for go to next and previous tabItem Shiny

后端 未结 2 1141
一向
一向 2020-12-21 06:29

I know this is pretty close to previously aked questions, but after thorough study of those examples I haven\'t found a solution for my particular problemm yet.

I h

2条回答
  •  生来不讨喜
    2020-12-21 06:47

    I will admit that this is not fully generalized. It requires that you place a vector in your server that has the names of the tabs from the UI. But, you really only need two buttons to make it work (not two buttons per tab). You only need to make sure that the tab_id vector has the correct names in the same order as the UI. You can probably get away with something like this if it is a small scale project where the tabs and tab names are not changing a lot.

    library(shiny)
    library(shinydashboard)
    library(shinyjs)
    
    ### create general button here like: 
    ### write a function that looks at what (nth) tabItem we are, and creates a ###  uiOutput for a next_n button (I can do this myself I think) 
    
    shinyApp(
      ui = 
        dashboardPage(
          dashboardHeader(title = "FLOW C.A.R.S."),
          dashboardSidebar(
            useShinyjs(),
            sidebarMenu(id = "tabs",
                        menuItem("Home", tabName = "Home", icon = icon("home")),
                        menuItem("My Page", tabName = "MyPage", icon =icon("download")),
                        menuItem("Do math", tabName = "Math", icon=icon("folder-open")),
                        menuItem("Results of something", tabName="Results", icon= 
                                   icon("file-text-o")),
                        menuItem("Short Manual", tabName = "Manual", icon = icon("book"))
            )
          ),
    
          dashboardBody(
            hidden(actionButton(inputId ="Previous", label = icon("arrow-left"))),
            hidden(actionButton(inputId ="Next", label = icon("arrow-right")))
          )
        ),
    
      server = 
        shinyServer(function(input, output, session){
    
          tab_id <- c("MyPage", "Math", "Results", "Manual")
    
          observe({
            lapply(c("Next", "Previous"),
                   toggle,
                   condition = input[["tabs"]] != "Home")
          })
    
          Current <- reactiveValues(
            Tab = "Home"
          )
    
          observeEvent(
            input[["tabs"]],
            {
              Current$Tab <- input[["tabs"]]
            }
          )
    
          observeEvent(
            input[["Previous"]],
            {
              tab_id_position <- match(Current$Tab, tab_id) - 1
              if (tab_id_position == 0) tab_id_position <- length(tab_id)
              Current$Tab <- tab_id[tab_id_position]
              updateTabItems(session, "tabs", tab_id[tab_id_position]) 
            }
          )
    
          observeEvent(
            input[["Next"]],
            {
              tab_id_position <- match(Current$Tab, tab_id) + 1
              if (tab_id_position > length(tab_id)) tab_id_position <- 1
              Current$Tab <- tab_id[tab_id_position]
              updateTabItems(session, "tabs", tab_id[tab_id_position]) 
            }
          )
        })
    )
    

提交回复
热议问题