For those who, like me, are finding this question years later, the secret of getting this to work depends on two things:
- Setting the environment variable permanently
- Making it available to the user
shiny
on the server computer
So, for example, the app:
library(shiny)
ui <- fluidPage(
h1(paste("Run by", Sys.getenv("USER"))),
textInput("name", "What is your name?", value = "Friendly User"),
actionButton("greet", "Greet"),
textOutput("greeting")
)
server <- function(input, output, session) {
output$greeting <- renderText({
req(input$greet)
paste0(Sys.getenv("greeting"), " ", isolate(input$name), "!")
})
}
shinyApp(ui, server)
Run after the command export greeting='Hello'
should say "Hello Friendly User!" if it's finding the correct environment variable, but:

The export
statement only sets the environment variable for that session (when you have the terminal open). Even with shiny
as user, the session you set the variable in will differ from the session the server is running (which, I think, is constant whilst the server is up). However, see that Sys.getenv("USER")
is correctly discovering the system environment variable specifying that the user running the server is shiny
.
There are a number of ways of setting environment variables for a shiny server, most of which would require the restarting of the server after running the command (not always possible). If you set it in /etc/environment
then it should allow all users (including shiny
) to access it whenever it's requested without requiring a restart:
echo greeting='Hello' >> /etc/environment
gives:

For further details, and to try out various options, here's a docker image of the above app, created from this Dockerfile which runs the setting environment variable command.