UI无法正确呈现所选标签

请注意,我在侧边栏菜单中拥有SELECTED = TRUE标志名称,这种印象使我印象深刻是因为它是在渲染时加载的第一页,因此应将其设为“首页”。但是正如您所看到的,它不会这样做。输入用户名/密码(sam / 123)后,它将进入空白页,并且直到您选择该选项卡,该选项卡才会出现。

如果SELECTED = TRUE标志实际上没有达到我的预期,那么获得所需输出的正确方法是什么?

UI:

 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
    }
  })
}
mengzai8888 回答:UI无法正确呈现所选标签

请尝试以下操作:

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)

我刚刚将id = "tabs",赋予了sidebarMenu,然后添加了:

    observeEvent(USER$Logged == TRUE,selected = "t_item1")
    })
,

怎么样?

library(shiny)
library(shinydashboard)

header <- dashboardHeader(title = "x")
sidebar <- dashboardSidebar(uiOutput("sidebarpanel"))
body <- dashboardBody(uiOutput("body"))
ui <- dashboardPage(header,pswd = c("123"))
login <- box(
  textInput("userName",session) {
  login.page = paste(
    isolate(session$clientData$url_protocol),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",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)
                  },box(
                    title = "Controls",50)
                  )
                ))

    } else {
      login
    }
  })
}

app<-shinyApp(ui = ui,server = server)
runApp(app,host="0.0.0.0",port=5050,launch.browser = TRUE)


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

本文链接:https://www.f2er.com/3139853.html

大家都在问