Global variables not being generated


Hi, unfortunately this post will be a little frustrating as I can’t create a reproducable example so I’m just looking for any advice and if anyone else is experiencing the same thing.

I am using about 200 lines of code, placed before the UI function, to generate some data frames and variables that are being referenced later on by the server and UI. The end lines are being used to generate some variables to subset the data frame by the user later on.

However when hitting “Run App” these lines aren’t being run. All the functions being used are from base R - things such as grep and as.character - and are working as intended in the lines above. If I add a line after the final line such as:


then that line does get run and the app errors like you’d expect. If I load these lines before I run the app the variables generate without any problems and the app works as it should.

Has anyone had any similar experiences? Feel free to ask for more info, I appreciate this is annoyingly vague but I can’t recreate this anywhere and I obviously can’t post the entire app online.



Hi Matt,

Did you try putting these lines of code in a “global.R” script ? I think it’s the best place for global variables which need to be generated only once at the launch of the app.

HTH :slight_smile:



Hi Aline,

Thank you I looked into using a global.R and set one up. By using source(global.R) it essentially runs all the code before hand automatically so it works a treat! No idea why it wasn’t doing this before but I’ll continue using this moving forward.

Thank you very much!



Glad it’s working for you. I thought I’d point out that I don’t think explicitly calling source() on your global.R file is necessary; the global.R file is “special” and Shiny will/should know to run it before calling your UI and server functions.