无法在RShiny应用程序中隐藏/显示流畅的页面

问题描述 投票:1回答:1

我目前正在开发一个闪亮的应用程序,我需要隐藏登录页面并在成功登录后显示闪亮的仪表板。如果没有,应显示登录页面。

我访问了几个站点,因此决定使用shinyjs程序包显示和隐藏活动页面/仪表板页面。

使用的全局函数如下:

`%AND%` <- function (x, y) {
  if (!is.null(x) && !anyNA(x))
    if (!is.null(y) && !anyNA(y))
      return(y)
  return(NULL)
}

passwordInputAddon <- function (inputId, label, value = "", placeholder = NULL, addon, width = NULL)
{
  value <- shiny::restoreInput(id = inputId, default = value)
  htmltools::tags$div(
    class = "form-group shiny-input-container",
    label %AND% htmltools::tags$label(label, `for` = inputId),
    style = if (!is.null(width)) paste0("width: ", htmltools::validateCssUnit(width), ";"),
    htmltools::tags$div(
      style = "margin-bottom: 5px;", class="input-group",
      addon %AND% htmltools::tags$span(class="input-group-addon", addon),
      htmltools::tags$input(
        id = inputId, type = "password", class = "form-control",
        value = value, placeholder = placeholder
      )
    )
  )
}

使用的UI代码如下:

 ui <- shinyUI(fluidPage(
          tags$div(id = "login_page_ui",
          shinyjs::useShinyjs(),
          tags$style(".container-fluid {margin-top: 13%}"),
          setBackgroundColor(color = "#2d3c44"),
          fluidRow(
            column(8, align = "center", offset = 2,
                   textInputAddon("name", label = "", placeholder = "Username", addon = icon("user"),width = "25%"),
                   tags$style(type="text/css", "#string { height: 50px; width: 50%; text-align:center;
                        font-size: 30px; display: block;}")
            )
          ),
          fluidRow(
            column(8, align = "center", offset = 2,
                   passwordInputAddon("password", label = "", placeholder = "Password", addon = icon("key"),width = "25%"),               
                   tags$style(type="text/css", "#string { height: 50px; width: 50%; text-align:center;
                        font-size: 30px; display: block;}")
            )
          ),

          fluidRow(
            column(12, div(style = "height:20px;background-color: #2d3c44;")
            )

          ),

          fluidRow(
            column(6, align = "center", offset = 3,
                   actionButton("login",label = "Login", width = "35%", style = "color: #fff; background-color: #1bc3d7; border-color: #1bc3d7;")))


  )
  ),

  shinyjs::hidden(
    tags$div(
      id = "dashboard_page_ui",
    dashboardPage(
      dashboardHeader(
        title="Shiny Dashboard",
        tags$li(
          class="dropdown"
        )
      ),
      dashboardSidebar(
        sidebarMenu(
          id = 'dashboard_menu',
          sidebarMenuOutput("menu")

        )
      ),
      dashboardBody(
        tabItems(
          tabItem(tabName="Item1"),
          tabItem(tabName="Item2"),
          tabItem(tabName="Item3")
        )

      )

    )

   )
 )
)

使用的服务器代码如下:

server <- function(input, output,session){
  observeEvent(input$login,{

    if((input$name == "test") & (input$password == "test123")){

      shinyjs::show("dashboard_page_ui")
      shinyjs::hide("login_page_ui")

    }


  })


}

执行此代码时,我收到此错误消息

Error in shinyUI(fluidPage(tags$div(id = "login_page_ui", shinyjs::useShinyjs(),  : 
  unused argument (shinyjs::hidden .....

我不知道确切的问题是什么。谁能帮我解决这个问题?

r shiny shinydashboard shinyjs
1个回答
0
投票

shinyUI接受单个参数ui(用户界面定义)。但是,您提供了两个参数:请参见代码中shinyjs::hidden(...)之前的逗号。

请检查以下内容:

library(shiny)
library(shinydashboard)

`%AND%` <- function (x, y) {
  if (!is.null(x) && !anyNA(x))
    if (!is.null(y) && !anyNA(y))
      return(y)
  return(NULL)
}

passwordInputAddon <-
  function (inputId,
            label,
            value = "",
            placeholder = NULL,
            addon,
            width = NULL)
  {
    value <- shiny::restoreInput(id = inputId, default = value)
    htmltools::tags$div(
      class = "form-group shiny-input-container",
      label %AND% htmltools::tags$label(label, `for` = inputId),
      style = if (!is.null(width))
        paste0("width: ", htmltools::validateCssUnit(width), ";"),
      htmltools::tags$div(
        style = "margin-bottom: 5px;",
        class = "input-group",
        addon %AND% htmltools::tags$span(class = "input-group-addon", addon),
        htmltools::tags$input(
          id = inputId,
          type = "password",
          class = "form-control",
          value = value,
          placeholder = placeholder
        )
      )
    )
  }

ui <- fluidPage(
  tags$div(
    id = "login_page_ui",
    shinyjs::useShinyjs(),
    tags$style(".container-fluid {margin-top: 13%}"),
    setBackgroundColor(color = "#2d3c44"),
    fluidRow(
      column(
        8,
        align = "center",
        offset = 2,
        textInputAddon(
          "name",
          label = "",
          placeholder = "Username",
          addon = icon("user"),
          width = "25%"
        ),
        tags$style(
          type = "text/css",
          "#string { height: 50px; width: 50%; text-align:center;
                        font-size: 30px; display: block;}"
        )
      )
    ),
    fluidRow(
      column(
        8,
        align = "center",
        offset = 2,
        passwordInputAddon(
          "password",
          label = "",
          placeholder = "Password",
          addon = icon("key"),
          width = "25%"
        ),
        tags$style(
          type = "text/css",
          "#string { height: 50px; width: 50%; text-align:center;
                        font-size: 30px; display: block;}"
        )
      )
    ),
    fluidRow(column(
      12, div(style = "height:20px;background-color: #2d3c44;")
    )),
    fluidRow(column(
      6,
      align = "center",
      offset = 3,
      actionButton(
        "login",
        label = "Login",
        width = "35%",
        style = "color: #fff; background-color: #1bc3d7; border-color: #1bc3d7;"
      )
    ))
  ),
  shinyjs::hidden(tags$div(
    id = "dashboard_page_ui",
    dashboardPage(
      dashboardHeader(title = "Shiny Dashboard",
                      tags$li(class = "dropdown")),
      dashboardSidebar(sidebarMenu(id = 'dashboard_menu',
                                   sidebarMenuOutput("menu"))),
      dashboardBody(tabItems(
        tabItem(tabName = "Item1"),
        tabItem(tabName = "Item2"),
        tabItem(tabName = "Item3")
      ))
    )
  ))
)

server <- function(input, output, session) {
  observeEvent(input$login, {
    if ((input$name == "test") & (input$password == "test123")) {
      shinyjs::show("dashboard_page_ui")
      shinyjs::hide("login_page_ui")
    }
  })
}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.