fs
fs copied to clipboard
Feature request: New function `path_glue()`
I often find myself using path() along with stringr::str_glue() (or glue::glue()) to create paths from variables whilst ensuring they are clean and easy to work with.
This ends up with messy looking nested code, I wonder if the functionality of glue (specifically the ability to embed variables with {}) could be included in fs? Either as a new function path_glue() which would work like path() but first parse any path string for variables. Alternatively, it could be an additional argument to path(), glue = FALSE by default, which would have the behaviour described when set to TRUE.
Can you please show a motivating use case? fs currently have no dependencies, and it is unlikely that we would add glue as a dependency, but maybe glue could be optional.
I have a number of use cases of this type:
example folder structure:
data/
data/1516
data/1516/extracts
data/1617
data/1617/extracts
etc...
I will write code like:
library(fs)
library(glue)
library(readr)
base_dir <- path("data")
year <- "1516"
output_date <- Sys.Date()
data_file <- read_rds(path(base_dir, year, "extracts", glue("extract_for_{year}.rds")))
output <- some_stuff(data_file)
write_rds(output, path(base_dir, year, glue("output_for_{year}-{output_date}.rds")))
If fs included some glue functionality it would simplify the code:
library(fs)
library(readr)
base_dir <- path("data")
year <- "1516"
output_date <- Sys.Date()
data_file <- read_rds(path_glue(base_dir, year, "extracts", "extract_for_{year}.rds"))
output <- some_stuff(data_file)
write_rds(output, path_glue(base_dir, year, "output_for_{year}-{output_date}.rds"))