dynamic body in shiny dashboard

主宰稳场 提交于 2019-12-03 07:46:34

问题


I am using R/shinydasboard to create a web app that I am putting behind a login screen.

I'm having trouble with getting the main body to render based on the sidebar menu tabs.

I've tried to ensure one of the tab items has selected = TRUE still to no avail.

Sample code below:

require(shiny)
require(shinydashboard)

Logged <- FALSE;
LoginPass <- 0; #0: not attempted, -1: failed, 1: passed

login <- box(title = "Login",textInput("userName", "Username (user)"),
             passwordInput("passwd", "Password (test)"),
             br(),actionButton("Login", "Log in"))

loginfail <- box(title = "Login",textInput("userName", "Username"),
                 passwordInput("passwd", "Password"),
                 p("Username or password incorrect"),
                 br(),actionButton("Login", "Log in"))

mainbody <- div(tabItems(
  tabItem(tabName = "t_item1", box(title = "Item 1 information")),
  tabItem(tabName = "t_item2", box(title = "Item 2 information")),
  tabItem(tabName = "t_item3", box(title = "Item 3 information"))
)
)

header <- dashboardHeader(title = "dashboard header")

sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))

body <- dashboardBody(uiOutput("body"))

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output, session) {
  USER <<- reactiveValues(Logged = Logged, LoginPass = LoginPass)
  observe({
    if (USER$Logged == FALSE) {
      if (!is.null(input$Login)) {
        if (input$Login > 0) {
          username <- isolate(input$userName)
          password <- isolate(input$passwd)
          #Id.username <- which(my_username == Username)
          if (username == "user" & password == "test") {
            USER$Logged <<- TRUE
            USER$LoginPass <<- 1
          }
          USER$LoginPass <<- -1
        }
      }
    }
  })

  output$sidebarpanel <- renderUI({
    if (USER$Logged == TRUE) {
      div(
        sidebarMenu(
          menuItem("Item 1", tabName = "t_item1", icon = icon("line-chart"), selected = TRUE),
          menuItem("Item 2", tabName = "t_item2", icon = icon("users")),
          menuItem("item 3", tabName = "t_item3", icon = icon("dollar"))
        )
      )}
  })
  output$body <- renderUI({
    if (USER$Logged == TRUE) {
      mainbody
    }
    else {
      if(USER$LoginPass >= 0) {
        login
      }
      else {
        loginfail
      }
    }
  })
}

shinyApp(ui, server)

Any suggestions on how to get the mainbody to show one of the tabs when it loads would be greatly appreciated. I have a suspicion it may due to the load order of the sidebar and the body however I am not sure how to investigate further.

I have also tried a conditionalPanel however couldn't get that to work either.

UPDATE This screenshot shows the behaviour that occurs straight after login.

This screenshot shows the desired behaviour after login.


回答1:


You need to set the tabItem as active:

tabItem(tabName = "t_item1", class = "active", box(title = "Item 1 information"))

It will render the first tab when you dynamically render the content after login.



来源:https://stackoverflow.com/questions/33794588/dynamic-body-in-shiny-dashboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!