how to see second tabitem after clicking action button??

Hello !
I am learning Shiny to develop an app.

I want to see the second tabitem contents (which is table from uploaded file) after I click on Submit button.

I ran the below code but it is not working, even after click on actionbutton, it is still showing me the first tabitem contents or it is not changing !

Thanks

My Code:

# Upload library ----
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Simple tabs"),
  dashboardSidebar(
    sidebarMenu(
      id = "tabs",
      menuItem("About", tabName = "first",icon = icon("home")),
      menuItem("Upload Data", tabName = "second", icon = icon("home"),
               fileInput("file1", "Upload a .txt file ",
                         accept = c("text/csv", "text/comma-separated-values,text/plain",".csv",
                                    options(shiny.maxRequestSize=900*1024^2))),
               actionButton('subm', 'Submit'))
    )
  ),
  dashboardBody(
    
    tabItems(
    tabItem(tabName = "first",
            h3("App Ver 0.1.0")
    ),
    tabItem(tabName = "second",
            h2("Results Table"),
            tableOutput("contents")
    )
  )
  )
)

server <- function(input, output, session) {
  observeEvent(input$subm, {
    output$contents <- renderTable(rownames = TRUE,{
      if (is.null(input$file1)) return(NULL)
      head(read.csv(input$file1$datapath))
    })
    updateTabItems(session, "tabs", selected = "second" )
  })
}

shinyApp(ui, server)

at first it struck me that you hadn't formatted your code as code; so I have edited your post please read

with that out of the way, the colourising code formatting, makes it clear an obvious problem in your code
notice how the colour at the bottom is not the colour at the start...

there is no ' ending to 'subm

Thanks for formatting my code! even after putting ' after subm and correcting to actionButton('subm', 'Submit'), the code is not working,

The problem here is that childfull menuItems by default don't link to a tabItem - they are used only to present their children after being expanded:

“Childfull” menuItem() s cannot have a tabName or a selected argument (or rather, they can, but this will be completely ignored by Shiny).

Source: Shiny Dashboard Behavior

Childfull means having child tags like your fileInput, the actionButton or another menuItem / menuSubItem.

Please see my answer here:

for a workaround.

Here is a working version of your code:

# Upload library ----
library(shiny)
library(shinyjs)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Simple tabs"),
  dashboardSidebar(
    useShinyjs(),
    sidebarMenu(
      id = "tabs",
      menuItem("About", tabName = "first",icon = icon("home")),
      menuItem("Upload Data", expandedName = "second", icon = icon("home"),
               fileInput("file1", "Upload a .txt file ",
                         accept = c("text/csv", "text/comma-separated-values,text/plain",".csv",
                                    options(shiny.maxRequestSize=900*1024^2))),
               actionButton('subm', 'Submit')),
      hidden(menuItem("childlessSecond", tabName = "childlessSecond"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "first",
              h3("App Ver 0.1.0")
      ),
      tabItem(tabName = "childlessSecond",
              h2("Results Table"),
              tableOutput("contents")
      )
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$subm, {
    output$contents <- renderTable(rownames = TRUE,{
      if (is.null(input$file1)) return(NULL)
      head(read.csv(input$file1$datapath))
    })
    updateTabItems(session, "tabs", selected = "childlessSecond" )
  })
}

shinyApp(ui, server)
1 Like

Thank you so much @ismirsehregal :slight_smile: for your help.

The code is working now and will add some more features to complete my app.

Appreciated !!

  • Sagar Patel

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.