Shiny 仪表板主体未返回正确的选项卡项

Shiny 仪表板主体未返回正确的选项卡项,shiny,shinydashboard,shiny-server,shinyapps,shiny-reactivity,Shiny,Shinydashboard,Shiny Server,Shinyapps,Shiny Reactivity,当我在菜单项(“文件加载”)中选择一个CSV文件时,该文件将被选中,但不会显示在仪表板主体的选项卡项(选项卡1)中。它试图只显示第二个tabItem“tab2”。我想我错过了一些可以联系的东西。有人能帮帮我吗 ui <- dashboardPage( dashboardHeader(title="Test"), dashboardSidebar( sidebarMenu(id = 'sbar', verbatimTextOutput(&q

当我在菜单项(“文件加载”)中选择一个CSV文件时,该文件将被选中,但不会显示在仪表板主体的选项卡项(选项卡1)中。它试图只显示第二个tabItem“tab2”。我想我错过了一些可以联系的东西。有人能帮帮我吗

ui <-  dashboardPage(
   dashboardHeader(title="Test"),

   dashboardSidebar(
        sidebarMenu(id = 'sbar', verbatimTextOutput("text1"),
              menuItem("File Loading", tabName = 'tab1', icon = icon('line-chart'),
                        fileInput("file1", "Select CSV File", accept = c("text/csv","text/comma- 
                                  separated- values,text/plain",".csv"))),
              menuItem('Data', tabName = 'tab2',icon = icon('line-chart')),
              menuItem('c',tabName = 'tab3',icon = icon('line-chart')))),
            
  dashboardBody(
        tabItems(
              tabItem("tab1", DT::dataTableOutput("contents"),style = "height:500px; overflow-y: 
                       scroll;overflow-x: scroll;",
                          title = "Dashboard example"),
              tabItem("tab2", "Sub-item 2 tab content") 
     )
   ) 
 )  
server <- function(input, output, session) {
   observe(input$sbar)
   output$text1 <- renderText(print(input$sbar))

   output$contents <- renderDataTable({
       inFile <- input$file1
       if (is.null(inFile))
          return(NULL)
         read.csv(inFile$datapath) })  
}  

ui我已经注释掉了每个菜单项中的多个选项卡。如果需要,您可以添加更多。试试这个

ui <-  dashboardPage(
  dashboardHeader(title="Test"),
  dashboardSidebar(
    shinyjs::useShinyjs(),
    sidebarMenu(id = 'sbar', verbatimTextOutput("text1"),
                
                menuItem("File Loading", tabName = 'page1', icon = icon('line-chart'),
                         fileInput("file1", "Select CSV File",
                                    accept = c("text/csv","text/comma-separated-values,text/plain",".csv")),
                         menuSubItem(actionButton(inputId="next1", label="NEXT"),
                                     tabName="next", icon=""),
                         hidden(menuSubItem("My tab1", tabName="tab1",icon = icon("line-chart")))
                ),
                menuItem('Data', tabName = 'tab2',icon = icon('line-chart')),
                menuItem('c',tabName = 'tab3',icon = icon('line-chart'))
                )
    ),

  dashboardBody(
    tabItems(
      tabItem(tabName = "tab1",
            fluidRow(
              tabBox(id = "tabset1", height = "650px", width=12, title = "Dashboard example",
                     # The id lets us use input$tabset1 on the server to find the current tab
                     tabPanel(" ", value="tab11", " ",
                              fluidRow(DT::dataTableOutput("contents1"))
                     )#,
                     # tabPanel("Plot", value="tab12", " ",
                     #          fluidRow(plotOutput("plot1"))
                     # ),
                     # tabPanel("tab3 title", value='tab13', " ",
                     #          valueBoxOutput('tab3_valuebox'))
              )
            )
      ),
      tabItem(tabName="tab2",
              fluidRow(
                tabBox(id = "tabset2", height = "650px", width=12, # title = "Sub-item 2 tab content",
                       # The id lets us use input$tabset1 on the server to find the current tab
                       tabPanel("Data", value="tab21", " ",
                                fluidRow(DT::dataTableOutput("contents2"))
                       )#,
                       # tabPanel("Plot", value="tab22", " ",
                       #          fluidRow(plotOutput("plot2"))
                       # )
                )
              )
      )
    )
  )
)

server <- function(input, output, session) {
  #observe(input$sbar)
  observeEvent(input$next1, {
    updateTabItems(session, "sbar", "tab1")

    req(input$next1)
    if (input$next1 == 0) {
      return(NULL)
    }else if (input$next1 == 1 & is.null(input$file1)) {
      return(NULL)
    }else {

      inFile <- input$file1
      myfile <- read_csv(inFile$datapath)
      output$contents1 <- renderDataTable({
        myfile
      })
      output$contents2 <- renderDataTable({
        myfile
      })
    }

  })
  output$text1 <- renderText(print(input$sbar))
  output$plot1 <- renderPlot({hist(rnorm(10))})
  output$plot2 <- renderPlot({hist(rnorm(20))})

  output$tab3_valuebox <- renderValueBox({
    valueBox('2020',subtitle = "blah blah blah",icon = icon("car"),
             color = "red"
    )
  })

}

shinyApp(ui, server)
ui