user3022875
user3022875

Reputation: 9018

How to access session$user in Shiny using Shiny Pro

I have a simple shiny app that I am testing SHINY pro and I would like to access the session$user like the documentation suggests:

http://rstudio.github.io/shiny-server/latest/#flat-file-authentication. See section 4.1 which shows this code:

shinyServer(function(input, output, session) {
  output$username <- reactive({
    session$user
  })

That code works but I need to access session$user in the ui.r file via the GetUser() function

Here is my ui.r File:

library(shiny)
shinyUI(fluidPage(
  textOutput("HeaderTime"),
  sidebarPanel(
    selectInput("t",   "t:",  as.character(GetUser()), selected = as.character(GetUser())), width = 2
  ),
  mainPanel(
    tabsetPanel(
      tabPanel("test",
               dataTableOutput("Table"), 
               plotOutput("Plot"),
#                verbatimTextOutput("Txt"),
               width = 12
      )
    )
  )
))

You can see the GetUser() function in the selectInput. I have placed GetUser() in my server.R file here:

shinyServer(function(input, output, session) {

  GetUser<-reactive({
    return(session$user)
  })

output$Plot<-renderPlot({

  hist(rnorm(1000))
})


output$Table<- renderDataTable({ 
  data.frame(a=c(1,2,3,4),b = c("TEst","test","test","test"))
})

})

when I run this code I get the error:

Error in GetUser() : argument "session" is missing, with no default

Any idea how to allow the ui.r to access GetUser() in the server.r file so session$user can be used in the ui?

Here is code to run project:

library(rJava)
library(shiny)
install.packages("shiny")
runApp("C://me/pathtoproject")

Thank you.

Upvotes: 2

Views: 5384

Answers (1)

Enzo
Enzo

Reputation: 2611

The error you get does address the problem (albeit in a cryptic way).

When you call your GetUser function in the ui, the "session" argument is not known (i.e. it is only "known" by the server).

I suggest to use updateSelectInput (or renderUI if you prefer) and send the session$user value from the server side.

Something like (not tested):

 server = function(session,input, output) {

  GetUser<-reactive({
    return(session$user)
  })

 updateSelectInput(session, "t", "t:",  as.character(GetUser()),selected = as.character(GetUser()))

Upvotes: 3

Related Questions