This function saves a data.frame as a .csv
file (using
write.csv
), stores a checksum in '.worcs',
appends the .gitignore
file to exclude filename
, and saves a
synthetic copy of data
for public use. To generate these synthetic
data, the function synthetic
is used.
Usage
closed_data(
data,
filename = paste0(deparse(substitute(data)), ".csv"),
codebook = paste0("codebook_", deparse(substitute(data)), ".Rmd"),
value_labels = paste0("value_labels_", deparse(substitute(data)), ".yml"),
worcs_directory = ".",
synthetic = TRUE,
save_expression = write.csv(x = data, file = filename, row.names = FALSE),
load_expression = read.csv(file = filename, stringsAsFactors = TRUE),
...
)
Arguments
- data
A data.frame to save.
- filename
Character, naming the file data should be written to. By default, constructs a filename from the name of the object passed to
data
.- codebook
Character, naming the file the codebook should be written to. An 'R Markdown' codebook will be created and rendered to
github_document
('markdown' for 'GitHub'). By default, constructs a filename from the name of the object passed todata
, adding the word 'codebook'. Set this argument toNULL
to avoid creating a codebook.- value_labels
Character, naming the file the value labels of factors and ordinal variables should be written to. By default, constructs a filename from the name of the object passed to
data
, adding the word 'value_labels'. Set this argument toNULL
to avoid creating a file with value labels.- worcs_directory
Character, indicating the WORCS project directory to which to save data. The default value
"."
points to the current directory.- synthetic
Logical, indicating whether or not to create a synthetic dataset using the
synthetic
function. Additional arguments for the call tosynthetic
can be passed through...
.- save_expression
An R-expression used to save the
data
. Defaults towrite.csv(x = data, file = filename, row.names = FALSE)
, which writes a comma-separated, spreadsheet-style file. The argumentsdata
andfilename
are passed fromopen_data()
to the expression defined insave_expression
.- load_expression
An R-expression used to load the
data
from the file created bysave_expression
. Defaults toread.csv(file = filename, stringsAsFactors = TRUE)
. This expression is stored in the project's.worcs
file, and invoked byload_data()
.- ...
Additional arguments passed to and from functions.
Examples
old_wd <- getwd()
test_dir <- file.path(tempdir(), "data")
dir.create(test_dir)
setwd(test_dir)
worcs:::write_worcsfile(".worcs")
df <- iris[1:3, ]
closed_data(df, codebook = NULL)
#> ✔ Storing original data in 'df.csv' and updating the checksum in '.worcs'.
#> ✔ Generating synthetic data for public use. Ensure that no identifying information is included.
#>
|
| | 0%
|
|============== | 20%
|
|============================ | 40%
|
|========================================== | 60%
|
|======================================================== | 80%
#> Warning: Dropped unused factor level(s) in dependent variable: versicolor, virginica.
#>
|
|======================================================================| 100%
#> ✔ Storing synthetic data in 'synthetic_df.csv' and updating the checksum in '.worcs'.
#> ✔ Updating '.gitignore'.
#> ✔ Updating '.gitignore'.
#> ✔ Storing value labels in 'value_labels_df.yml'.
setwd(old_wd)
unlink(test_dir, recursive = TRUE)