I'm pulling in a .rda
file and loading the data. It works fine like this:
library(magrittr)
conn <- "http://www.mosaic-web.org/go/datasets/DCF/MeasTreatTables.rda"
%>% url()
load(conn)
But when I use the pipe throughout:
"http://www.mosaic-web.org/go/datasets/DCF/MeasTreatTables.rda" %>%
url() %>%
load()
Nothing is loaded into the Global Environment. Why is this so?
It's because when using magrittr
command chains you're not operating in the global environment.
Let's run your command, but returning the environment by the same token:
library(magrittr)
rm(list=ls())
parallel_universe <- "http://www.mosaic-web.org/go/datasets/DCF/MeasTreatTables.rda" %>%
url() %>%
{load(.);environment()}
Now let's explore those environments:
ls()
#[1] "parallel_universe"
ls(parallel_universe)
#[1] "Measurements" "Treatments"
We've been populating the wrong space. Now let's try again :
rm(list=ls())
"http://www.mosaic-web.org/go/datasets/DCF/MeasTreatTables.rda" %>%
url() %>% load(globalenv())
ls()
# [1] "Measurements" "Treatments"
Tada
Note that you cannot assign to parent.frame()
, it won't work, because magrittr
is nesting environments, so if you really want to use this outside of the global environment, this will work:
my_env <- environment()
"http://www.mosaic-web.org/go/datasets/DCF/MeasTreatTables.rda" %>%
url() %>% load(my_env)
See also : Assigning to temp variables inside of `maggritr` command chain