subfoldr2
is an R package to facilitate the saving and loading of
and the opening and closing of
By default files are saved to output
unless overridden using sbf_set_main()
which can take a relative or absolute path.
An R object can be written using sbf_save_object()
and read using sbf_load_object()
.
All R objects are saved as .rds
files in folder objects
(in the main folder).
The user can save objects to a particular sub folder within objects
using sbf_set_sub()
or the sub = sbf_get_sub()
argument of sbf_save_object()
.
sbf_set_sub("times2")
x <- x * 2
sbf_save_object(x)
sbf_save_object(x * 3, x_name = "x3", sub = "times2/times3")
sbf_save_object(data.frame(), x_name = "df", sub = "times2/times3")
list.files(sbf_get_main(), recursive = TRUE)
#> [1] "objects/times2/times3/df.rds" "objects/times2/times3/x3.rds" "objects/times2/x.rds"
#> [4] "objects/x.rds"
The user can load all the objects in the current sub folder into the calling environment using sbf_load_objects()
or recursively load all the objects as a list column in a data frame using sbf_load_objects_recursive()
.
print(sbf_set_sub("times2", "times3"))
#> [1] "times2/times3"
sbf_load_objects()
ls()
#> [1] "df" "x" "x3"
sbf_load_objects_recursive(sub = "times2")
#> # A tibble: 3 × 4
#> objects name sub file
#> <list> <chr> <chr> <chr>
#> 1 <df [0 × 0]> df "times3" /var/folders/yg/xzvpt0r53gsgj094f3sxb1n00000gn/T/RtmpuE93oH/output/objects/times…
#> 2 <dbl [1]> x3 "times3" /var/folders/yg/xzvpt0r53gsgj094f3sxb1n00000gn/T/RtmpuE93oH/output/objects/times…
#> 3 <dbl [1]> x "" /var/folders/yg/xzvpt0r53gsgj094f3sxb1n00000gn/T/RtmpuE93oH/output/objects/times…
To recursively delete a sub folder call sbf_set_sub()
with rm = TRUE
.
All the objects in the calling environment can be saved to the current subdirectory using sbf_save_objects()
.
The above functions for saving and loading R objects are members of families of functions for specific types of object. In particular there are family members for
sbf_reset_sub(rm = TRUE, ask = FALSE)
#> ✔ Directory '/var/folders/yg/xzvpt0r53gsgj094f3sxb1n00000gn/T/RtmpuE93oH/output' deleted
data2 <- data.frame(x = 5:6, y = 7:8)
chr <- "some text"
vec <- c(1, 3) # not saved as vector
sbf_save_datas()
sbf_save_strings()
sbf_save_numbers()
list.files(sbf_get_main(), recursive = TRUE)
#> [1] "data/data2.rds" "data/df.rds" "numbers/x.csv" "numbers/x.rds" "numbers/x3.csv" "numbers/x3.rds"
#> [7] "strings/chr.rda" "strings/chr.rds" "strings/chr.txt"
All types of object are saved as rds files. However to facilitate viewing, each number is also saved as a csv file and each string is also saved as a txt file.
Tables are data frames with just logical, numeric, character, factor, Date and POSIXct columns. As tables are intended for reports they have to be saved individually with an optional caption using sbf_save_table()
. As a result tables cannot be bulk saved however they can be bulked loaded. Each table is also saved as csv file (to facilitate viewing) and the metadata including the caption are saved as an rda file (to facilitate report production).
To save a table which is not intended for a report set the argument report = FALSE
.
Plots are ggplot objects with caption, width and height (in inches) and dpi (dots per inch) metadata. Each plot is also saved as a png file of the specified dimensions and resolution and the default dataset if present (and ≤ 1,000 rows) is saved as a csv. By default the last ggplot object created, modified or plotted is saved and the dimensions are taken from the current graphics device.
A platform independent graphics window can be opened using sbf_open_window()
, where the width
and height
arguments are in inches, and closed using sbf_close_window()
.
In addition to individually or bulk loading ggplot objects, the user can individually or bulk load the default datasets using for example sbf_load_plots_data()
.
In the case of base or grid graphics the contents of the current window can be copied to a png file (without an associated rds file) using sbf_save_window()
which takes caption, width, height and dpi arguments. The window names and file paths can be loaded using sbf_load_windows_recursive()
.
If there are plots and windows with the same name then the duplicate plot is dropped from the report.
An existing png file can be imported into the windows subdirectories using sbf_save_png()
.
A pdf graphics device can be opened (in the current subfolder of the pdfs folder) using sbf_open_pdf()
and the current device can be closed using sbf_close_pdf()
. pdf graphics devices have the advantage of accepting multiple plots. They are not incorporated into reports.
A connection to a SQLite database (in the current subfolder of the dbs folder) can be opened using conn <- sbf_open_db()
and an individual connection can be closed using sbf_close_db(conn)
.
Alternatively, sbf_save_datas_to_db()
automatically open and closes the connection and uses readwritesqlite::rws_read_sqlite.environment
to write all the data frames in the calling environment.
The opposite function, sbf_load_datas_from_db()
, uses rws_read_sqlite.SQLiteConnection
to save all the tables to the calling environment.
Reports can be generated using the subreport package.
To install the latest development version from GitHub repository
Please note that the subfoldr2 project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms