Skip to content
This repository has been archived by the owner on Jan 10, 2025. It is now read-only.

path_choices(), choices_prioritize(), try_read() #23

Open
maurolepore opened this issue Apr 30, 2020 · 0 comments
Open

path_choices(), choices_prioritize(), try_read() #23

maurolepore opened this issue Apr 30, 2020 · 0 comments

Comments

@maurolepore
Copy link
Collaborator

maurolepore commented Apr 30, 2020

If you keep stubbing your toe in a particular way as you work on your package, zoom out and consider if you can design the problem away ...
-- Jenny Bryan

These functions would allow you to programatically switch where you read data from based on where you prefer to read the data from and based on availability. When working locally you may read from a local cache, but when you code runs on a server you may need to read data from a remote source.
Or maybe you moved files from one local folder to another and now all code breaks. You can fix this with try_read(), where you specify the new location to try on error.

  • path_choices(path, choices): Create alternative paths from a given path. Useful to reroute a call from one place to another.

    • path [character(1)] Original path, e.g. "~/Dropbox/a/file.csv"
    • choices: List of functions (maybe lambdas) to create alternative paths e.g. list(asis = identity), and list(local_drop_path = identity, online_drop_path = ~sub("~/Dropbox", "", .x)).
    • Value: Named character vector
  • choises_prioritize(choices, priority): Reorder multiple paths from highest to lowest priority.

    • choices: Named character vector like the output of path_choices.
    • priority: The order given by name or position.
    • Value: Named character vector.
  • try_read(choices, .f, ...): Try in order to read from each path. Each attempt first check if fs::file_exists(), then read or fall back to the next path. If no file exists throw an error.

    • choises: Named character vector.
    • .f: List of functions (maybe lambdas) to try read from each path in choises.
    • ...: Additional arguments to pass to .f.

This tools minimize refactoring of a code base with hard coded paths. Develop try_read() that works for you, then either replace globally each call to a specific read function with try_read() or leave the code as is and overwrite the definition via a function factory that uses try_read(). For example consider this pseudo code:

# Overwrite via a function factory
read.csv <- with_try_read(
  choices = list(local_dropbox = identity,  online_dropbox = ~sub("~/Dropbox", "", .x)),
  .f = c(readr::read_csv, rdrop2::drop_read_csv)
)

# All code remains the same but behaves differently
read.csv("~/Dropbox/path/to/some/file.csv")
read.csv("~/Dropbox/path/to/some/file.csv")
@maurolepore maurolepore changed the title detour path_best Apr 30, 2020
@maurolepore maurolepore changed the title path_best path_detour May 1, 2020
@maurolepore maurolepore changed the title path_detour path_choices(), choices_prioritize(), try_read() May 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant