

library(rlang)Īll that is left is to evaluate these expressions using eval or eval_tidy in a loop, which will run the code and load the packages. Using rlang, we can achieve a similar result using expr() and !! to replace the pkg with the actual variable. In base, you can replace values in an expression using bquote() and. There are many different ways to do this, but I will suggest two similar methods: one using base R, and one using the tidyverse. To achieve this, we need to build an expression, which is simply code which has not yet been evaluated. Instead, we need to use NSE ourselves to substitute pkg with the name of the package itself, as if you had written it directly into the console. Remember, the issue is that library uses non-standard evaluation on package names, so we can’t use library(pkg). So, now with a brief understanding of NSE, let’s try to use the library function in a loop again. mtcars %>%įor more details on non-standard evaluation, I recommend reading the Advanced R book.

So when this code is used in the mutate function, R is now able to find disp, because dplyr has changed where R looks for the variable. When using this code in the mutate function, dplyr helpfully prevents evaluation, and later re-evaluates by first looking in the provided data, and then in the evaluation environment. R is unable to find the disp variable because it exists as a column in the mtcars dataset, not in the evaluation environment. Standard evaluation in R would find the disp variable and compute the division, so let’s try that: disp / 61.0237 #> Error in eval(expr, envir, enclos): object 'disp' not found The mutate function is calculating disp / 61.0237 and saving the result as a column called displ_l. As an example, let’s look at the dplyr code above. So what is non-standard evaluation? As the name may suggest, it is code which is evaluated in a non-standard way. Mutate(displ_l = disp / 61.0237) library(ggplot2) Try to identify the NSE parts in the following code examples: library(dplyr) Most tidyverse packages also leverage NSE to simplify the typing needed to transform a dataset or plot some data. In fact, NSE is used each time you load in a package without quoting the package name. You may not know what non-standard evaluation is, but you have definitely used it before (perhaps without even realising).

In the loop, R tries to be helpful by loading pkg instead of the value stored inside (“ggplot2”, and then “dplyr”).įor most R users, an understanding of non-standard evaluation (NSE) is rarely needed. That is what allows you to use library(tidyverse) instead of library("tidyverse"). If it were that simple, it wouldn’t warrant a blog post! This doesn’t work because the library function uses non-standard evaluation. So to load packages in a loop, one might try: packages Error in library(pkg): there is no package called 'pkg' Most R users would load packages using the library function, such as library(tidyverse). If you really want have fun, try loading packages in a loopĪlthough not a pop-quiz, it is certainly a challenge, and a common cause of confusion for R users.

As part of this discussion, podcast guest Roger Peng ( noted that: Something like this: f <- function(old.In Nick Tierney ( and Saskia Freytag’s ( second Credibly Curious podcast, they briefly delve into the confusing world of non-standard evaluation (NSE). Take the variable as the argument, and pass it back as the update. This would be similar to the following code: y <- 100Īs for what to do to fix it. # add 1 to it, and store it in a new variable x. # Get the "global" x which has value 100, Fill in the blanks in the for loop to make the following true: price should hold that iteration's price date should hold that iteration's date This time, you want to know if apple goes above 116. To help visualize: # this is the "global" scope Each time you call the function, the same thing happens, so you assign local x to be 101 5 times, and print it out 5 times. You are not updating the global x in the function, but assigning the value of 101 to the local x. It is because in your function you are dealing with a local variable x on the left side, and a global variable x on the right side.
