json2env: Parse JSON Formatted Arguments

Description Usage Arguments Details Value See Also Examples

Description

json2env parses arguments in javascript object notation (JSON) and loads them into an environment (typically that of the enclosing function).

Usage

1
json2env(json_args, replace = T, envir = parent.frame())

Arguments

json_args

JSON-formatted name-value pairs to be converted.

replace

Boolean indicating if provided JSON arguments take priority over formals of the same name. If TRUE, JSON arguments replace the values of formals of the same name (default). If FALSE, JSON arguments are ignored when a formal of the same name is declared.

envir

The environment JSON arguments are to be loaded into. Defaults to the environment of the calling function (parent.frame()); however, specific environments could be named such as the current environment (environment()) or the global environment (.GlobalEnv).

Details

json2env loads JSON formatted name-value pairs into an environment (parent.frame of the calling environment by default). This is particularly useful in web applications where it is desireable for a function to receive arguments in JSON format.

Value

Note that json2env does not have a return value and so does not need to be assigned (<-). Rather the side effect of json2env is being used to assign names with values to an environment.

See Also

list2env, fromJSON, toJSON

Examples

1
2
3
4
5
6
7
8
9
json_args <- '{"a" : 3, "b" : 4, "c" : 5}'

my_fun <- function(json_args, a = 1, b = 2, d = 6, replace = T) {
    json2env(json_args, replace = replace)
    print(c(a, b, c, d))
}

my_fun(json_args) # [1] 3 4 5 6
my_fun(json_args, replace = F) # [1] 1 2 5 6

msinjin/json2env documentation built on May 9, 2019, 5:54 a.m.