Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different pages in Shiny App

I want to have different pages in my shiny dashboard. First of all I created a login page to give authentication to user and admin. After that if admin login to system want to see some options that the user cannot access to them. Question: when I login as user or admin I can see the main ui.r page in the background how can I fix this problem to see only admin.R or user.R? When the user login the dashboard shows and when the admin login the dashboard and widget show. So I decided to create 4 pages in R as following: ui.R

library(shiny)
library(shinydashboard)
shinyUI( 
  dashboardPage(
    dashboardHeader(title = "Navigational Support System"),
    dashboardSidebar(),
    dashboardBody(
      box(
        uiOutput("page")
      )
    )
  )
)

server.R

library(shiny)
library(shinydashboard)
source("user.R")
source("admin.R")
############################################################################################################
#Login USER and ADMIN TO the System
my_username <- c("test","admin")
my_password <- c("test","123")
get_role=function(user){
  if(user=="test") {
    return("TEST")
  }else{
    return("ADMIN")
  }
}

get_ui=function(role){
  if(role=="TEST"){
    return(list_field_user)
  }else{
    return(list_field_admin)
  }
}


shinyServer(function(input, output,session) {

  USER <- reactiveValues(Logged = FALSE,role=NULL)

  ui1 <- function(){
    tagList(
      div(id = "login",
          wellPanel(textInput("userName", "Username"),
                    passwordInput("passwd", "Password"),
                    br(),actionButton("Login", "Log in")))
      #tags$style(type="text/css", '#login{ width:750px; float:left;}')

    )}

  ui2 <- function(){tagList(tabPanel("NSS",get_ui(USER$role)))}

  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)
          Id.password <- which(my_password == Password)
          if (length(Id.username) > 0 & length(Id.password) > 0) {
            if (Id.username == Id.password) {
              USER$Logged <- TRUE
              USER$role=get_role(Username)

            }
          } 
        }
      }
    }
  })
  observe({
    if (USER$Logged == FALSE) {

      output$page <- renderUI({
        div(class="outer",do.call(bootstrapPage,c("Please Login",ui1())))
      })
    }
    if (USER$Logged == TRUE)    {
      output$page <- renderUI({
        div(class="outer",do.call(navbarPage,c(inverse=TRUE,title = "Welcome Admin!",ui2())))
      })
      #print(ui)
    }
  })
  ##################################################################################################

})

admin.r

list_field_admin =

  shinyUI( 
  dashboardPage(
    dashboardHeader(title = "Decison Support System"),
    dashboardSidebar( sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Widgets", tabName = "widgets", icon = icon("th"))
    )
    ),
    dashboardBody(
  )

))

user.r

list_field_user =  shinyUI( 
  dashboardPage(
    dashboardHeader(title = "Decison Support System"),
    dashboardSidebar( sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))

    )
    ),
    dashboardBody(
    )

  ))
like image 512
user Avatar asked Nov 19 '15 21:11

user


People also ask

What are the main 2 parts of an R Shiny app?

A Shiny app consists of two parts, a user interface ( ui ) and an R session that runs code and returns results ( server ). These two parts can be in their own files called ui. R and server. R, or they can be combined into a single file called app.

What is fluid page in Shiny?

A fluid page layout consists of rows which in turn include columns. Rows exist for the purpose of making sure their elements appear on the same line (if the browser has adequate width). Columns exist for the purpose of defining how much horizontal space within a 12-unit wide grid it's elements should occupy.

Why is the Shiny app so slow?

There are many reasons why a shiny app runs slower than expected. The most common reason is the Shiny app code has not been optimized. You can use the profvis package to help you understand how R spends its time. You also might want to make sure your server is large enough to host your apps.

Is Shiny hard to learn?

Along with Shiny elements, you can use HTML elements to stylize your content in your application. In my opinion, R Shiny is very easy to learn despite how powerful the tool is. If you're working on a side project or looking to add something to your portfolio, I highly recommend trying it out.


1 Answers

If i undertand you right you can create different lists in you additional files

1) admin.r

admin_title="Decison Support System"
admin_side=list(sidebarMenu(
  menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
  menuItem("Widgets", tabName = "widgets", icon = icon("th"))
))
admin_main=list(

  tabItems(
    tabItem(tabName = "dashboard", list(h1("1234"),h2("234"))),
    tabItem(tabName = "widgets", list(fluidRow(column(6,numericInput("inputtest", "test", value = 0),column(6,actionButton(inputId ="test1",label ="go")))))
  )
  ))

2) user.r

test_title="Decison Support System"
test_side=list(sidebarMenu(
  menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))))

test_main=list(

  tabItems(
    tabItem(tabName = "dashboard", list(h1("user"),h2("user")))
  ))

Then change a bit you UI and server

UI :

library(shiny)
library(shinydashboard)
shinyUI( 
  dashboardPage(
    dashboardHeader( title=textOutput("title")),
    dashboardSidebar(uiOutput("side")),
    dashboardBody(
          uiOutput("page")

    )
  )

)

server :

library(shiny)
library(shinydashboard)
source("user.R")
source("admin.R")

my_username <- c("test","admin")
my_password <- c("test","123")
get_role=function(user){

  if(user=="test") {

    return("TEST")
  }else{

    return("ADMIN")
  }
}

get_ui=function(role){
  itog=list()
  if(role=="TEST"){
    itog$title=test_title
    itog$main=test_main
    itog$side=test_side
    return(itog)
  }else{
    itog$title=admin_title
    itog$main=admin_main
    itog$side=admin_side
    return(itog)
  }
}


shinyServer(function(input, output,session) {

  USER <- reactiveValues(Logged = FALSE,role=NULL)

  ui1 <- function(){
    tagList(
      div(id = "login",
          wellPanel(textInput("userName", "Username"),
                    passwordInput("passwd", "Password"),
                    br(),actionButton("Login", "Log in")))
      ,tags$style(type="text/css", "#login {font-size:10px;   text-align: left;position:absolute;top: 40%;left: 50%;margin-top: -10px;margin-left: -150px;}")
    )}


  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)
          Id.password <- which(my_password == Password)
          if (length(Id.username) > 0 & length(Id.password) > 0) {
            if (Id.username == Id.password) {
              USER$Logged <- TRUE
              USER$role=get_role(Username)

          }
        } 
      }
    }
    }
  })
  observe({
    if (USER$Logged == FALSE) {

      output$page <- renderUI({
        box(
        div(class="outer",do.call(bootstrapPage,c("",ui1()))))
      })
    }
    if (USER$Logged == TRUE)    {
      itog=get_ui(USER$role)
      output$title<- renderText({
        itog$title
      })
      output$side <- renderUI({
        itog$side
      })
      output$page <- renderUI({
        itog$main
      })
      }
  })
})
like image 58
Batanichek Avatar answered Oct 26 '22 00:10

Batanichek