admin管理员组文章数量:1122846
I made a mistake and wrote this:
library(tidyverse)
list(x = rnorm(10)) %>% save(file = "test.RData")
instead of this:
x <- list(x = rnorm(10))
save(x, file = "test.RData")
As a result I cannot read "test.RData" back
load("test.RData")
ls()
character(0)
Yet the ".RData" file seems to contain something as the following command creates a 73 MB file compared with a 77 B with just ten values. It is just impossible to read back.
list(x = rnorm(10000000)) %>% save(file = "test.RData")
Interestingly the base r pipe |>
throws an error
list(x = rnorm(10)) |> save(file = "test.RData")
Error in save(list(x = rnorm(10)), file = "test.RData") :
object ‘list(x = rnorm(10))’ not found
The mistake is easy to fix and I won't do it again but I am wondering what is happening under the hood:
- Why is save() not throwing an error when passed a nameless object with
%>%
? - Would there be a way to load a .RData file written this way?
I made a mistake and wrote this:
library(tidyverse)
list(x = rnorm(10)) %>% save(file = "test.RData")
instead of this:
x <- list(x = rnorm(10))
save(x, file = "test.RData")
As a result I cannot read "test.RData" back
load("test.RData")
ls()
character(0)
Yet the ".RData" file seems to contain something as the following command creates a 73 MB file compared with a 77 B with just ten values. It is just impossible to read back.
list(x = rnorm(10000000)) %>% save(file = "test.RData")
Interestingly the base r pipe |>
throws an error
list(x = rnorm(10)) |> save(file = "test.RData")
Error in save(list(x = rnorm(10)), file = "test.RData") :
object ‘list(x = rnorm(10))’ not found
The mistake is easy to fix and I won't do it again but I am wondering what is happening under the hood:
- Why is save() not throwing an error when passed a nameless object with
%>%
? - Would there be a way to load a .RData file written this way?
1 Answer
Reset to default 5In fact it is loaded as the name .
(dot) which was created by the %>%
pipe. It is equivalent to the following which explicitly shows the name:
rnorm(10) %>% save(., file = "test.RData")
On the other hand the base pipe |>
does not create a new name which accounts for the error if it is used. It works via substitution of the left hand side into the right hand side without creating a new name so is equivalent to the following which is an error:
save(rnorm(10), file = "test.RData") }
Note that
rnorm(10) |> save(file = "test.RData") |> quote()
## save(rnorm(10), file = "test.RData")
The reason the name does not appear is that ls
does not show names that begin with dot unless called as
ls(all = TRUE)
Also note that load
returns a character vector of names in it so that another way to get the name is to look at Names
where
Names <- load("test.RData")
Names
## [1] "."
本文标签: rretrieve RData file of nameless object created with magrittrgtStack Overflow
版权声明:本文标题:r - retrieve .RData file of nameless object created with `magrittr::%>%` - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302206a1931452.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论