How to access OS environment variable from shiny app's server.R

I am getting blank when accessing OS environment variable USER from app's server.R installed on shiny server.
output$textoutput=renderText(paste("user name is ",Sys.getenv(c("USER"))))

The variable is available on bash prompt.
I tried exporting the variable as well.

Hi Jasmin, I was trying to recreate your request and it should work based on your mentioned code-snippet. :wink:

Best regards
Adam

P.S. Below is a short example: :slight_smile:

library(shiny)

ui <- fluidPage(
  #display your 'textoutput'
  textOutput("textoutput")
)

server <- function(input, output, session){
  #get your user name based on 'Sys.getenv' (static object)
  user.name <- paste0("user name is ",Sys.getenv(c("USER")))
  #test user.name easy by printing on console
  print(user.name)
  
  #create UI output object 
  output$textoutput <- renderText({
    user.name
  })
}

shinyApp(ui, server)

1 Like

Thanks for confirming Adam, I will have to find why I am getting a blank

For security reason, a shiny that runs in a shiny server does not see all the environment variable of the system. It is like a sandbox process.

A discussion on this:

Sorry, this isn't possible today. We scrub most environment variables before launching R, in case there is sensitive information in them

Renviron.site or .Renviron in you app is a way to pass environment variable, but it could not apply in your case.

4 Likes

This helps, thank you

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.