How to access session$user in ui.r for Shiny Pro

1,366 views
Skip to first unread message

sycamo...@gmail.com

unread,
Mar 24, 2015, 2:32:02 PM3/24/15
to shiny-...@googlegroups.com
I am having a Shiny Server Pro issue and I was wondering if you could help.

I would like to access the session$user in my ui.   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 if you want to use session$user in output BUT I need to access session$user in the ui.r file via the GetUser() function in my ui

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 and made it reactive thinking this would work but it does not.

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

Joe Cheng

unread,
Mar 24, 2015, 4:32:11 PM3/24/15
to sycamo...@gmail.com, shiny-...@googlegroups.com
You'll need to either use this (in your server function) (normally you'd wrap this kind of thing in observe({ ... }), but in this case there's nothing reactive in determining the selection or choices):

  updateSelectInput(session, "t", selected = GetUser(), choices = GetUser())

or do something fancier with renderUI.

Can I ask why you're making a select input with nothing but the user's username? If that's not a representative example of what you're actually trying to do in ui.R, there might be other ways.

--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/3027b0c3-e878-4928-a244-a97a63e3bd42%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages