简体   繁体   中英

R: purrr: Paying the %.%er: Is walk passing entire piped vector, rather than elements?

Here is a function to take a file name stem, assemble the file name from path, stem, and suffix, read the file, drop a variable, and save the file again. In this simplified version the read stem and the save stem are the same, and I remove just one variable.

library(tidyverse)

strip_cps <-  function(X, path = path){
  readRDS(paste0(path = path, X, ".RDS")) %>%
    select(-"YEAR")  %>%
    saveRDS(file = paste0(path = path, X, ".RDS"))
}

Here is a function to call the previous function repeatedly, once per stem.

compact_cps <- function(stems, path){
  stems %>% 
    walk(strip_cps0(., path=path))
}

Here is a microscopic version of my data. Replace the data directory with a directory on your own system

df1 <- tibble(YEAR = 1, SEX = 1)
df2 = df1[1,] + c(1, 1)

saveRDS(df1, file = paste0("./CPS_1962-2018/", "df1", ".RDS"))
saveRDS(df2, file = paste0("./CPS_1962-2018/", "df2", ".RDS"))

Running the foregoing code on this data gets me the following error:

Error in gzfile(file, "rb") : invalid 'description' argument

From the traceback, I learn that gzfile is called by readRDS . Rerunning the code with debug , I learn that the description argument of gzfile is

chr{1:2]  "./CPS_1962-2018/df1.RDS"   "./CPS_1962-2018/df1.RDS"

which is to say, a length-2 character vector.

Now, it seems to be a characteristic of bugs in my code that I can not resolve myself that they are not where I think they are. But it appears to me that walk, having received the character vector stems = c(“df1”, ”df2”) as its principle argument via %>% from the calling function compact_cps , then hands the whole vector to strip_cps instead of handing it one element at a time.

It seems more likely that I am missing something than that walk would do that, but I can't see what.

try this

compact_cps <- function(stems, path){
  stems %>% 
    walk(strip_cps, path=path))
}


the first argument to walk is the vector stems, the next argument is the name of the function you want to call for each element in the vector, then any additional arguments to the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM