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.
我们正在R中为我们的服务构建一个软件包(这是巴西的一个机器人顾问),我们一直向我们的函数内部的外部API发送请求。
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
.
当我们使用我们的包运行一些脚本时,我们需要一些信息,如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?
有时我们使用API的暂存版本,我们必须不断切换路径。我们应该如何在我们的功能中调用它?
Should we set a global environment variable, a package environment variable, just define api_path
in our scripts or a package config file?
我们应该设置一个全局环境变量,一个包环境变量,只需在我们的脚本或包配置文件中定义api_path吗?
How do we do that?
我们怎么做?
Thanks for your help in advance.
感谢您的帮助。
Ana
安娜
1 个解决方案
#1
11
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:
一种方法是使用R的选项界面。使用以下命令在R目录中创建文件zzz.r(这是此文件的惯用名称):
.onLoad <- function(libname, pkgname) {
options(api_path='...', username='name', password='pwd')
}
This will set these options when the package is loaded into memory.
这将在程序包加载到内存时设置这些选项。
#1
11
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:
一种方法是使用R的选项界面。使用以下命令在R目录中创建文件zzz.r(这是此文件的惯用名称):
.onLoad <- function(libname, pkgname) {
options(api_path='...', username='name', password='pwd')
}
This will set these options when the package is loaded into memory.
这将在程序包加载到内存时设置这些选项。