Global Variables in R Packages

September 7, 2017
By

(This article was first published on R on datawookie, and kindly contributed to R-bloggers)

I know that global variables are from the Devil, but sometimes you just can’t get around them.

I’m building a small package for a client that relies on a data file. For various reasons that file is not part of the package and can reside in different locations on users’ machines. Furthermore there are users on both Windows and Linux machines.

I decided to set up a global variable to store the data file path and a function to modify it.

Naive Implementation

This is what my initial implementation looked like:

data_path = "data"

set_data_path <- function(path) {
  data_path <<- path
}

It seems perfectly reasonable. And, in fact, would work… were in not in a package. However, building the package and giving a try yielded an error.

> set_data_path("/tmp")
Error in set_data_path("/tmp") : 
  cannot change value of locked binding for 'data_path'

While investigating the problem I learned about unlockBinding(), which is good to know.

Improved Implementation

It turns out that you need to use an environment to get this to work. Here is the revised (working) code:

pkg.globals <- new.env()

pkg.globals$data_path <- "data"

set_data_path <- function(path) {
  pkg.globals$data_path <- path
}

It’s slightly more verbose, but it works!

To leave a comment for the author, please follow the link and comment on their blog: R on datawookie.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)