Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we set constant variables while building R packages?

We are building a package in R for our service (a robo-advisor here in Brazil) and we send requests all the time to our external API inside our functions.

As it is the first time we build a package we have some questions. :(

When we will use our package to run some scripts we will need some information as api_path, login, password.

How do we place this information inside our package?

Here is a real example:

get_asset_daily <- function(asset_id) {
    api_path <- "https://api.verios.com.br"

    url <- paste0(api_path, "/assets/", asset_id, "/dailies?asc=d")
    data <- fromJSON(url)
    data
}

Sometimes we use a staging version of the API and we have to constantly switch paths. How we should call it inside our function?

Should we set a global environment variable, a package environment variable, just define api_path in our scripts or a package config file?

How do we do that?

Thanks for your help in advance.

Ana

like image 621
Ana Vitória Baraldi Avatar asked Jan 15 '16 18:01

Ana Vitória Baraldi


People also ask

How do you create a constant variable?

Variables can be declared as constants by using the “const” keyword before the datatype of the variable. The constant variables can be initialized once only. The default value of constant variables are zero.

What is R package development?

R packages are an ideal way to package and distribute R code and data for re-use by others. The RStudio IDE includes a variety of tools that make developing R packages easier and more productive, including: Build pane with package development commands and a view of build output and errors.


1 Answers

One approach would be to use R's options interface. Create a file zzz.r in the R directory (this is the customary name for this file) with the following:

.onLoad <- function(libname, pkgname) {
    options(api_path='...', username='name', password='pwd')

}

This will set these options when the package is loaded into memory.

like image 133
Matthew Plourde Avatar answered Sep 20 '22 05:09

Matthew Plourde