Onclick Hide / Unhide in Shiny

时间:2017-08-10 05:02:45

标签: r shiny

下面是我的Shiny应用程序的屏幕截图,我创建了一个简单的login页面,仅向经过身份验证的用户授予对数据的访问权限。但我有以下问题,

  1. 无法在应用开始时隐藏Mainpanel,这意味着mainpanel应该在应用开始时为hidden,并且应该仅在visible之后按下Login按钮。
  2. mainpanel visible sidebar之后UsernamePasswordLogin button应隐藏在UI.R之后。

    enter image description here

  3. library(shiny) library(shinyjs) shinyUI(fluidPage( useShinyjs(), titlePanel( fluidRow( column(9, h1("VHMS")), column(1, img(src = '2.png', width = 100))), windowTitle = "my"), br(), br(), br(), br(), sidebarLayout( sidebarPanel( textInput("Username", "Username"), passwordInput("Password", "Password"), actionButton("Login", "Login")),position = "right", mainPanel( fluidRow( column(12, (tabsetPanel("views", type = "tabs", tabPanel("TableView", dataTableOutput("df_directions")), tabPanel("MapView", dataTableOutput("df_locations")) ) ) ) ) ) ) ) )

    Server.R

    library(shiny) library(shinyjs) useShinyjs() shinyServer(function(input, output) { output$df_directions <- renderDataTable({ json_data1 }, options = list(scrollX = TRUE)) })

    Hide/Show

    我能够单独解决我的问题,但我无法将<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Homepage</title> <link href="css/bootstrap.css" rel="stylesheet"> <link href="css/style2.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row"> <div class="col-sm-4 brand"> <h1>CruisingFree!</h1> </div> <div class="col-sm-8 navlinks"> <p>Some more stuff</p> </div> </div> </div> </body> </html> 登录信息整合到一个脚本中。

1 个答案:

答案 0 :(得分:2)

以下是您修改的代码,用于隐藏主面板,直到用户单击“登录”。它仍然需要验证用户名和密码。

library(shiny)
library(shinyjs)

ui <- shinyUI(fluidPage(
  useShinyjs(),
  titlePanel(
    fluidRow(  
      column(9, h1("VHMS")),
      column(1, img(src = '2.png', width = 100))
    ), 
    windowTitle = "my"
  ),
  br(),
  br(),
  br(),
  br(),
  sidebarLayout(
    sidebarPanel( id = "loginID",
             textInput("Username", "Username"),
             passwordInput("Password", "Password"),
             actionButton("Login", "Login")
    ),
    position = "right",
    shinyjs::hidden(
      mainPanel( id = "panelID",
        fluidRow( 
          column(12,
            tabsetPanel("views", type = "tabs",
                       tabPanel("TableView", dataTableOutput("df_directions")),
                       tabPanel("MapView", dataTableOutput("df_locations"))
            )
          )
        )         
      ) 
    )
  )
))

server <- shinyServer(function(input, output) {
  output$df_directions <- renderDataTable(
    mtcars, 
    options = list(scrollX = TRUE)
  )  

  observeEvent(input$Login, {
    # TODO: verify username and password
    shinyjs::hide("loginID")
    shinyjs::show("panelID")
  })
})

shinyApp(ui = ui, server = server)