简体   繁体   中英

R Shiny dashboard loading tabs only once

I started building my first shiny app but am now struggling with a strange behaviour. First, when I initially load the app, no tab is selected by default. Second, when clicking on any menu on the sidebar it shows the body only on the first time. When I go from "Overview" to "Pivot-Tabelle" and back, the body is blank. What am I missing? Below is the code I used.

library(shiny)
library(shinydashboard)

df<-data.frame(a=c(1,2,3,4),
               b=c("A","B","C","D"))

###################Beginn der App################
ui <- dashboardPage(
  # Application title
  dashboardHeader(),

  ##----DashboardSidebar----

  dashboardSidebar(
    menuItem("Overview", tabName = "overview",selected=TRUE),
    menuItem("Pivot-Tabelle", tabName = "pivot"),
    menuItem("Test", tabName = "farmer")
  ),

  ##----DashboardBody----
  dashboardBody(
    tabItems(
      ##----TabItem: Overview----
      tabItem(tabName="overview",
              fluidRow(
                valueBoxOutput("A"),
                valueBoxOutput("B")
              )
      ),
      ###----TabItem:Pivot----
      tabItem(tabName = "pivot",
              ##Pivot
              column(6,offset=4,titlePanel("Daten-Explorer")),
              column(12,
                     mainPanel(
                       rpivotTableOutput("pivot")
                     )
              )
      ),
      ##----TabItem:Test----
      tabItem(tabName = "Test",
              h2("In Progress"))
    )
  )
)

server <- function(input, output) {
  ##----server:overview----
  output$A<-renderValueBox({
    valueBox(
      paste0(25, "%"), "Landwirte in der Datenbank", icon = icon("Person"),
      color = "purple"
    )
  })
  output$B<-renderValueBox({
    valueBox(
      paste0(55, "%"), "Landwirte in der Datenbank", icon = icon("Person"),
      color = "purple"
    )
  })



  ##----server:pivot----
  output$pivot <- renderRpivotTable({
    rpivotTable(data = df)
  })
}

# Run the application
shinyApp(ui = ui, server = server)

This seems to work. You need to have sidebarMenu for your menuItem s. Also, you need to change tabName to farmer so it matches your menuItem . And I don't think you need mainPanel in there (you can use mainPanel with sidebarPanel as part of a sidebarLayout if you wanted that layout - see layout options ). See if this works for you.

library(shiny)
library(shinydashboard)
library(rpivotTable)

df<-data.frame(a=c(1,2,3,4),
               b=c("A","B","C","D"))

###################Beginn der App################
ui <- dashboardPage(
  # Application title
  dashboardHeader(),

  ##----DashboardSidebar----

  dashboardSidebar(
    sidebarMenu(
      menuItem("Overview", tabName = "overview",selected=TRUE),
      menuItem("Pivot-Tabelle", tabName = "pivot"),
      menuItem("Test", tabName = "farmer")
    )
  ),

  ##----DashboardBody----
  dashboardBody(
    tabItems(
      ##----TabItem: Overview----
      tabItem(tabName="overview",
              fluidRow(
                valueBoxOutput("A"),
                valueBoxOutput("B")
              )
      ),
      ###----TabItem:Pivot----
      tabItem(tabName = "pivot",
              ##Pivot
              column(6,offset=4,titlePanel("Daten-Explorer")),
              column(12,
                     #mainPanel(
                       rpivotTableOutput("pivot")
                     #)
              )
      ),
      ##----TabItem:Test----
      tabItem(tabName = "farmer",
              h2("In Progress"))
    )
  )
)

server <- function(input, output) {
  ##----server:overview----
  output$A<-renderValueBox({
    valueBox(
      paste0(25, "%"), "Landwirte in der Datenbank", icon = icon("Person"),
      color = "purple"
    )
  })
  output$B<-renderValueBox({
    valueBox(
      paste0(55, "%"), "Landwirte in der Datenbank", icon = icon("Person"),
      color = "purple"
    )
  })
  ##----server:pivot----
  output$pivot <- renderRpivotTable({
    rpivotTable(data = df)
  })
}

# Run the application
shinyApp(ui = ui, server = 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