Serialzing lists into text-lists can make it easier to work with web-service APIs. This package can help you work with http-query lists in the "R world", helping you serialize it for use in the "remote world". In many cases, parameters for http queries are formatted differently from R:
"true"
or "false"
The goal of this package is to make that translation process easier.
You can install srlst from github with:
# install.packages("devtools")
devtools::install_github("ijlyttle/srlst")
library("httr")
library("lubridate")
library("srlst")
Let's consider a web-serice endpoint that accepts query-parameters in the format described above. You wish to work with the query-parameters in an R-sensible way. The function serialize_list()
is used to serialize the elements of your list into the format expected by the web-service.
query_params <- list(
delay = dseconds(3),
print = TRUE,
next_steps = c("collate", "send"),
number = 20
)
serialize_list(query_params)
#> $delay
#> [1] "3000"
#>
#> $print
#> [1] "true"
#>
#> $next_steps
#> [1] "collate,send"
#>
#> $number
#> [1] "20"
Thus, you can use the httr package to build your URL, attaching the query.
url <- parse_url("https://useful.site.com/service")
url$query <- serialize_list(query_params)
build_url(url)
#> [1] "https://useful.site.com/service?delay=3000&print=true&next_steps=collate%2Csend&number=20"
If you need to change the default behavior of the serializer, the serialize_list()
function lets you set the delimiter. It also lets you set the locale()
, which is used to specify the format for individual types (like logical and time-difference).
There are other situations where list-serialization may be useful. For example if you are writing HTML elements for use with JavaScript, you will often have to set parameters like data-foo = "true"
. You can use serialize_list()
to help you think in the R world, then write to the HTML/JavaScript world.
Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.