What Are The Rules for Global Variables in R Shiny?

Variables in R or any other scripting language function similarly to variables in math. They are used to store pieces of data which are used in functions and other parts of the code.

A simple example of a variable named a set to 1, or a – 1. Unless specified, R and Shiny variables have a local scope. A local scope implies that the variable can only be accessed by the function using it. If the variable needs to be used outside of a function, it needs to have a global scope. Otherwise, R and Shiny will not know how to be able to interpret these variables and will return back with an error function.

What Are Global Variables? Why Use Them?

You would want to use a global variable when you need to access data outside of a function. Imagine that you’re writing code and creating two functions, each of which are to be called separately. There is one piece of information which can be used between both functions.

Example:

a – 2

addFunction – function(a) {
b – a + 1
return(b)
}
print(“First Value “, a)
print(“Sum “, addFunction(a))

The example above would work successfully and return both values. However, if the a variable were declared locally in addFunction() as in the example below, it would not have global scope.

addFunction – function() {
a – 2
b – a + 1
return(b)
}

print(“First Value “, a)
print(“Sum “, addFunction())

The function would return an error because it would not be able to access the value for a function outside of itself. If you are passing a value between functions, then a global variable would be very useful. Otherwise, your alternative would be to return values from each function. This would create a very messy situation with your code. Using a global variable is significantly simpler solution for both readability and performance.

Declaring Global Variables in Shiny

In shiny, you cannot declare a variable for global use as you would normally do in R, however, this is a simple change. Instead of declaring variables using a single a < sign, use two instead. To use a <- 2 globally, use a <<- 2 instead. Using the previous example with the a variable declared in the function:

addFunction – function() {
a – 2
b – a + 1
return(b)
}
print(“First Value “, a)
print(“Sum “, addFunction())

The a variable now has a global scope, meaning it can be used outside of addFunction. This function would not return an error and the print First Value statement would print First Value 2.

Using the Global.R file

The previous example above declares global variables within the scope of a single script. There are times when you will want to declare global variables to be used across multiple R scripts. When you need to use variables across a system, you need to do this a bit differently.

Another way of being able to scope variables globally is with the Global.R file. This is a file on the shiny server which provides instructions for the shiny app as a whole before it. It can contain things other than global variables such as instructions or other items. The Global.R file makes the variable values available for each session, or for each instance of the code that you are running on your server.

The great part about declaring variables in the Global.R file is that you can declare them as you normally would in your R Code. You can simply set a <- 1 if you are looking to set your a variable to one. Whether it is a simple variable or a more complex data frame, you can declare it.

The Global.R file should be stored in the same directory on your server as the UI.R and Shiny.R files. This is typically going to be the www folder on your server, which is typically also where the rest of the Shiny app is held. The Global.R file is also where you would store other setting options for your script.

Does Using Global Variables Hurt Performance?

Global variables are designed to allow data to be shared across sessions or within the scope of a single script. If you are accustomed to working in other scripting languages, you are often advised to not use global variables. The logic is that global variables can hurt your code’s performance or security.

Without a doubt, global very useful for many Shiny applications. Using global variables should not hurt the performance of your app. Shiny is built to handle often high demand applications. Fortunately, you can create global variables within your Shiny code without having to worry about the effect that they will have on your code. So, go ahead and use them if you would like to.

Scroll to top
Privacy Policy