【问题标题】:UI not rendering selected tab properlyUI 未正确呈现选定的选项卡
【发布时间】:2020-03-04 19:00:35
【问题描述】:

请注意,我在侧边栏菜单中有 SELECTED = TRUE 标志指定,我的印象是假设该选项卡成为“主页”,因为它是渲染时加载的第一页。但如您所见,它并没有这样做。输入用户名/密码 (sam/123) 后,它会进入一个空白页面,并且该选项卡在您选择之前不会出现。

如果 SELECTED = TRUE 标志实际上没有按照我的想法进行,那么获得所需输出的正确方法是什么?

用户界面:

 library(shiny)
 library(shinydashboard)

 header <- dashboardHeader(title = "x")
 sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))
 body <- dashboardBody(uiOutput("body"))
 ui <- dashboardPage(header, sidebar, body)

login_details <- data.frame(user = c("sam"),
                            pswd = c("123"))
login <- box(
  textInput("userName", "Username"),
  passwordInput("passwd", "Password"),
  actionButton("Login", "Log in")
)

服务器:

    server <- function(input, output, session) {
  login.page = paste(
    isolate(session$clientData$url_protocol),
    "//",
    isolate(session$clientData$url_hostname),
    ":",
    isolate(session$clientData$url_port),
    sep = ""
  )
  histdata <- rnorm(500)
  USER <- reactiveValues(Logged = F)
  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(login_details$user %in% Username)
          Id.password <- which(login_details$pswd %in% Password)
          if (length(Id.username) > 0 & length(Id.password) > 0){
            if (Id.username == Id.password) {
              USER$Logged <- TRUE
            }
          }
        }
      }
    }
  })
  output$sidebarpanel <- renderUI({
    if (USER$Logged == TRUE) {
      div(
        sidebarMenu(
          menuItem(
            "Item 1",
            tabName = "t_item1",
            icon = icon("line-chart"),
            selected = TRUE
          )
        )
      )
    }
  })

  output$body <- renderUI({
    if (USER$Logged == TRUE) {
      tabItems(
        tabItem(tabName = "t_item1",
                fluidRow(
                  output$plot1 <- renderPlot({
                    data <- histdata[seq_len(input$slider)]
                    hist(data)
                  }, height = 300, width = 300) ,
                  box(
                    title = "Controls",
                    sliderInput("slider", "observations:", 1, 100, 50)
                  )
                ))
      )
    } else {
      login
    }
  })
}

【问题讨论】:

    标签: r shiny shinydashboard shiny-server shinyapps


    【解决方案1】:

    请试试这个:

    library(shiny)
    library(shinydashboard)
    
    header <- dashboardHeader(title = "x")
    sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))
    body <- dashboardBody(uiOutput("body"))
    ui <- dashboardPage(header, sidebar, body)
    
    login_details <- data.frame(user = c("sam"),
                                pswd = c("123"))
    login <- box(
        textInput("userName", "Username"),
        passwordInput("passwd", "Password"),
        actionButton("Login", "Log in")
    )
    
    server <- function(input, output, session) {
        login.page = paste(
            isolate(session$clientData$url_protocol),
            "//",
            isolate(session$clientData$url_hostname),
            ":",
            isolate(session$clientData$url_port),
            sep = ""
        )
        histdata <- rnorm(500)
        USER <- reactiveValues(Logged = F)
        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(login_details$user %in% Username)
                        Id.password <- which(login_details$pswd %in% Password)
                        if (length(Id.username) > 0 & length(Id.password) > 0){
                            if (Id.username == Id.password) {
                                USER$Logged <- TRUE
                            }
                        }
                    }
                }
            }
        })
        output$sidebarpanel <- renderUI({
            if (USER$Logged == TRUE) {
                div(
                    sidebarMenu(id = "tabs",
                        menuItem(
                            "Item 1",
                            tabName = "t_item1",
                            icon = icon("line-chart")
                        )
                    )
                )
            }
        })
    
        output$body <- renderUI({
            if (USER$Logged == TRUE) {
                tabItems(
                    tabItem(tabName = "t_item1",
                            fluidRow(
                                output$plot1 <- renderPlot({
                                    data <- histdata[seq_len(input$slider)]
                                    hist(data)
                                }, height = 300, width = 300) ,
                                box(
                                    title = "Controls",
                                    sliderInput("slider", "observations:", 1, 100, 50)
                                )
                            ))
                )
            } else {
                login
            }
        })
    
        observeEvent(USER$Logged == TRUE, {
            updateTabItems(session, "tabs", selected = "t_item1")
        })
    }
    shinyApp(ui, server)
    

    我只是给 sidebarMenu 一个id = "tabs", 然后添加:

        observeEvent(USER$Logged == TRUE, {
            updateTabItems(session, "tabs", selected = "t_item1")
        })
    

    【讨论】:

      【解决方案2】:

      这个怎么样?

      library(shiny)
      library(shinydashboard)
      
      header <- dashboardHeader(title = "x")
      sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))
      body <- dashboardBody(uiOutput("body"))
      ui <- dashboardPage(header, sidebar, body)
      
      login_details <- data.frame(user = c("sam"),
                                  pswd = c("123"))
      login <- box(
        textInput("userName", "Username"),
        passwordInput("passwd", "Password"),
        actionButton("Login", "Log in")
      )
      
      server <- function(input, output, session) {
        login.page = paste(
          isolate(session$clientData$url_protocol),
          "//",
          isolate(session$clientData$url_hostname),
          ":",
          isolate(session$clientData$url_port),
          sep = ""
        )
        histdata <- rnorm(500)
        USER <- reactiveValues(Logged = F)
        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(login_details$user %in% Username)
                Id.password <- which(login_details$pswd %in% Password)
                if (length(Id.username) > 0 & length(Id.password) > 0){
                  if (Id.username == Id.password) {
                    USER$Logged <- TRUE
                  }
                }
              }
            }
          }
        })
      
      
        output$sidebarpanel <- renderUI({
      
          if (USER$Logged == TRUE) {
      
                sidebarMenu(
      
                  shinydashboard::menuItem("Item 1", tabName = "t_item1", icon = icon("clipboard-check"), selected = TRUE)
      
                )
          }
        })
      
        output$body <- renderUI({
      
          if (USER$Logged == TRUE) {
      
              menuItem(tabName = "t_item1",
                      fluidRow(
                        output$plot1 <- renderPlot({
                          data <- histdata[seq_len(input$slider)]
                          hist(data)
                        }, height = 300, width = 300) ,
                        box(
                          title = "Controls",
                          sliderInput("slider", "observations:", 1, 100, 50)
                        )
                      ))
      
          } else {
            login
          }
        })
      }
      
      app<-shinyApp(ui = ui, server = server)
      runApp(app, host="0.0.0.0",port=5050, launch.browser = TRUE)
      
      
      

      我在output$body 中将tabItems 替换为menuItem

      【讨论】:

      • 这个答案非常简洁。就两件事。 1. 直方图上方似乎添加了一个要点。 2. 从技术上讲,侧边栏菜单项在登录后实际上并没有被选中(它被绕过了)。
      • 选中后侧边栏菜单项变暗,左侧有一条蓝线。
      • 我没有注意到要点...不知道这是从哪里来的,所以我现在来看看。不知道你的意思是绕过它 - 你能详细说明吗?干杯!
      • 查看下一条评论
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 1970-01-01
      相关资源
      最近更新 更多