2

I am builidng an app that depending on the user loged I want to load one source of data or another. I was able to build the app creating the login page and to load different pages depending on the user logged as refered in different pages in Shiny App

My question is that depending on the user loged I want to load a different source of RData. So if the user loged is Admin I want to load the data_ADMIN.RData or if the user is test I want to load the data_TEST.RData . The data table loaded is used after to build my UI and to make my App run.

Here I put you some of the code that I used:

1) Admin

    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) Test.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")))
  ))

3) ui.R

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

    )
  )

)

4)

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)    {
      if(USER$role=="ADMIN"){
      load("data_ADMIN.RData")
      }else{
      load("data_TEST.RData")
      }  
      itog=get_ui(USER$role)
      output$title<- renderText({
        itog$title
      })
      output$side <- renderUI({
        itog$side
      })
      output$page <- renderUI({
        itog$main
      })
      }
  })
})

This is just copying the example I found but adding the load data part as I have in my orginal code that after in the server side I have all the functions that are used to show in the UI the data table contained in the RData file that is different depending on the user loged. In my orginal code when I run the app I get the error data_table not found (that is the name of the data table that I call).

How can I solve that? If you need more specific scripts let me know!

Aida

Community
  • 1
  • 1
Aida
  • 79
  • 1
  • 8
  • Have a look at the http://stackoverflow.com/questions/28987622/starting-shiny-app-after-password-input/28997605#28997605 where u can render different `ui` per user – Pork Chop Feb 15 '17 at 12:25
  • 1
    @aida, were you able to find a solution for this? I am struggling on the same aspect – user5249203 Sep 26 '17 at 18:11

0 Answers0