Reputation: 3796
If I observe a convention of saving my file paths as variables with the common prefix "file_", it seems I could create a wrapper function for read_rds()
that would name my read files based on whatever text came after "file_" in the file path's name.
I run into trouble when I evaluate the name I want the read file to take.
library(here)
library(readr)
library(stringr)
file_survey <- here("my_survey_2019.rds")
my_read_rds <- function(file){
name <- deparse(substitute(file))
name <- stringr::str_remove(name, "^file_")
eval(name) <- readr::read_rds(file) # Does not work
}
my_read_rds(file_survey) # would ideally create a dataframe named `survey`
Upvotes: 0
Views: 444
Reputation: 9705
You can use assign
.
my_read_rds <- function(file){
name <- deparse(substitute(file))
name <- stringr::str_remove(name, "^file_")
assign(name, readr::read_rds(file), envir=globalenv())
}
Upvotes: 1