It seem %>%
in the magrittr package is not working for the function load()
. This is my minimal example to reproduce my question.
## Create two example variables and save to tempdir()
a <- 1
b <- 1
save(list = ls(), file = file.path(tempdir(), 'tmp.RData'))
## Remove all variables and load into global environment
# rm(list = ls())
load(file.path(tempdir(), 'tmp.RData'))
ls()
# [1] "a" "b"
# Write the same code with pipe "%>%", but not variable is loaded
# rm(list =ls())
library(magrittr)
tempdir() %>% file.path('tmp.RData') %>% load
ls()
# character(0)
I don't understand why the pipe is not working for load()
. Thanks for any suggestions.
The
envir
argument inload()
needs to be specified as eitherglobalenv()
orparent.frame(3)
.The following also works:
I'll try to explain why. When you call
load()
from any environment, the function loads the new objects in the parent environment.Now, the global environment
globalenv()
is your R workspace. So, if you call load from the global environment (i.e. the workspace) everything works as you expect. Visualise this:load()
However, if you call
load()
from inside a function, then you've inserted an environment in between load and the global environment. Visualise this:load()
This is exactly what happens when you put
%>%
into the mix:%>%
load()
There are two solutions for resolving this. Either explicitly point to
globalenv()
or walk 3 steps up the chain to the global environment usingparent.frame(3)
.Note: There was an issue reported on GitHub for this. Not sure what the resolution was, or if there is one yet. The issue was just reported in September.
Many thanks to @Andrie for improving this explanation.