Edit a toml file by providing a dataframe replacement
Source:R/user_toml_interaction.R
edit_toml.Rd
Users can use edit_toml()
to edit a toml file (should they opt not to use a
text editor) by providing a dataframe of the desired contents. The function
will check the dataframe for validity and then rewrite the toml file using
the dataframe as a blueprint.
new_toml_dataframe
edit_toml()
will check the dataframe for the following columns:
type
: The type of the toml file, a character that is either "strata" or "laminae"name
: The character string that is the name of the stratum or laminaorder
: The numeric order of the stratum or laminaskip_if_fail
: (if type == laminae) A logical indicating if the lamina should be skipped if it failscreated
: A valid date that is the day the stratum or lamina was created
Unexpected columns will be dropped, and edit_toml()
will warn the user.
If there are any missing columns, edit_toml()
will return an error, stop
and inform the user what is missing.
If there are duplicates in the order
than strata
will rewrite the order
using its best guess.
usage
Users using this function will likely want to combine some of the other
helpers in strata
. This may looks something like this:
User runs
survey_tomls()
to find all the toml files in the projectUser runs
view_toml()
to view the contents of the toml file and saves to an object, likeoriginal_toml
or similiarUser edits the
original_toml
object to their liking and saves as a new object, likenew_toml
.User runs
edit_toml()
with the path to the original toml andnew_toml
objects and can then useview_toml()
to confirm the changes.
Examples
tmp <- fs::dir_create(fs::file_temp())
strata::build_quick_strata_project(tmp, 2, 3)
original_toml_path <- survey_tomls(tmp)[[1]]
original_toml <- view_toml(original_toml_path)
original_toml
#> # A tibble: 2 × 4
#> type name order created
#> <chr> <chr> <int> <date>
#> 1 strata stratum_1 1 2024-11-27
#> 2 strata stratum_2 2 2024-11-27
new_toml <- original_toml |>
dplyr::mutate(
created = as.Date("2021-01-01")
)
new_toml_path <- edit_toml(original_toml_path, new_toml)
#> [2024-11-27 15:57:03.5309] INFO: Backed up /tmp/RtmpRK1XGV/file163d2f179f07/strata/.strata.toml to /tmp/RtmpRK1XGV/file163d2f179f07/strata/.strata.bak
view_toml(new_toml_path)
#> # A tibble: 2 × 4
#> type name order created
#> <chr> <chr> <int> <date>
#> 1 strata stratum_1 1 2021-01-01
#> 2 strata stratum_2 2 2021-01-01
fs::dir_delete(tmp)