Accessing authenticated user ID from global.R

20 views
Skip to first unread message

Jared Knowles

unread,
Jun 21, 2017, 1:29:34 PM6/21/17
to Shiny - Web Framework for R
Hi!

I have an application where I want to run a SQL query once at the start of the application that is dependent on the user ID the user presents after authenticating. Ideally I'd like to pull the data down in global.R and then use the data throughout the rest of the application. 

Is there a way to access the user ID from within global.R and not have to include code in each of the server calls (the application is modularized) to reactively get the user ID from the session object?

I read in the release notes that there is a `Shiny.user` object created, so that session$user is not necessary, but I have not been able to find how to access Shiny.user from outside of the `server(session, input, output)` call. 

Any help?

      Jared

Jared Knowles

unread,
Jun 21, 2017, 5:17:27 PM6/21/17
to Shiny - Web Framework for R
For context, it looks like the `Shiny.user` object is created in JavaScript when the Shiny session is initialized. 


So what is the best way to get at that JavaScript element from global.R?

Dean Attali

unread,
Jun 21, 2017, 5:47:28 PM6/21/17
to Shiny - Web Framework for R
I might be misunderstanding your goal, but it sounds like you're trying to perform user-specific actions in global. User-specific is roughly equivalent to session-specific, which means these things must go through the session. The global file is meant for running code that is, by definition, *global* to the entire application - for running code that runs *once* in total instead of once per user. Anything that you want to do *per-user* generally means you want to do that in the server function because it needs to be in the context of every session. 

If you give more information regarding the actual usecase, people here might be able to tell you how to achieve what you want. But you can't really do something globally per-user, because that is by definition not global. What happens when two different users access the app at the same time - I assume they shouldn't share the same information?

I hope this was helpful at all rather than just confusing :)

Jared Knowles

unread,
Jun 21, 2017, 6:29:00 PM6/21/17
to Shiny - Web Framework for R
It helps! Thanks, I admit to being a little confused. 

I have a modularized app that is user-authenticated. When a user logs in on Shiny Server, I want to user their identification credentials to make a one-time DB query to access the data they need to use the application, and I want that data to be available across all the modules of the application. I need the Shiny.user parameter to plug into the DB query, so that they only get the data associated with their user account. 

The reason I thought to use global.R is that I don't want to make multiple calls to the DB for each module for each user -- I want one DB query per user with only their results, but I want to run that query only one time, not for each separate module of the application.

It seems pretty standard, so maybe I am stuck thinking about this in a weird way that is making the answer elusive to me..

Dean Attali

unread,
Jun 21, 2017, 6:31:59 PM6/21/17
to Shiny - Web Framework for R
Then it sounds like you do just want to run this code inside the server function, near the top, so that it will run once for each user, as soon as their session starts. You can pass the results object into all the different modules as an argument for instance
Reply all
Reply to author
Forward
0 new messages