Skip to contents

Creates a JSON (JavaScript Object Notation) file containing the specified name/value pairs. These files are hugely flexible and interpretable by a wide variety of coding languages and thus extremely useful in many contexts. This function is meant to assist those who wish to use JSON files to store user-specific information (e.g., email addresses, absolute file paths, etc.) in collaborative contexts.

Usage

make_json(x = NULL, file = NULL, git_ignore = FALSE)

Arguments

x

(character) named vector from which to generate JSON content. Vector elements become JSON values and the vector element names become JSON names. A named vector can be created like so: c("greeting" = "hello", "farewell" = "goodbye"). The characters on the left of the equal signs are names and the characters on the right are values.

file

(character) name of JSON file to create with contents provided to x. Must end with ".json"

git_ignore

(logical) whether to add the file name (defined in file) to the '.gitignore' if one exists. Defaults to FALSE

Value

Nothing. Called for side-effects (i.e., creating JSON file)

Examples

# Create contents
my_info <- c("data_path" = "Users/me/documents/my_project/data")

# Generate a local folder for exporting
temp_folder <- tempdir()

# Create a JSON with those contents
make_json(x = my_info, file = file.path(temp_folder, "user.json"), git_ignore = FALSE)

# Read it back in
(user_info <- RJSONIO::fromJSON(content = file.path(temp_folder, "user.json")))
#>                            data_path 
#> "Users/me/documents/my_project/data"