To run Shiny app, is it mandatory to make global variables which are employed on the App itself i.e.(Server.R File)

I am skipping the reprex. Will try to make the question theoretical & understandable. :thinking:

I extracted a table from server into data.frame named X.

Made Shiny app with separate ui.R and server.R files.

Plotted some graph and played with plyr functions using X.

The App run fine.

if I updated(renamed & removed variables from Global Environment) Say, Changed X to Y.

The error pops up

Error: object 'X' not found

In general, all the variable declaration is done in the server.R file. :thinking: Then why do I need to execute variable declaration commands in the console first :exploding_head: to make the app run, if the Global Environment is updated (Say I have renamed, removed some variables from Global Environment Not from server.R file).

Thanking You for your time and concern. :hugs:

There is no requirement that you define global variables in the console prior to running your app. In fact, I would take it a step further and say that it is highly discouraged to define these global variables in the console prior to running your app because the app is no longer reproducible. Instead, you should be defining any global variables in a separate script named global.R. This script is recognized by shiny and will run once at the beginning of your session. This script is good for loading required libraries and defining global variables that do not change.

In terms of the variable name changing from X to Y, that would be an issue even if you changed the name in the global.R script. If your server.R code is looking for a variable named X then it would not run correctly if you change the variable name without updating the server code, no matter where you define the variable.

2 Likes

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