我想Sys.getenv()用来检索环境变量。
在本地,我可以.Renviron在我的工作目录中保存一个包含变量的文件。这些负载非常好并且测试通过。
存储库在这里供参考:https : //github.com/Stoltzman-Consulting/githubActionsR
但是,由于存储了机密,我无法将此文件上传到我的存储库中。我在 GitHub 机密部分创建了机密:

我的测试如下:
test_that("multiplication works", {
expect_equal(2 * 2, 4)
})
test_that("Environment variable exists", {
expect_equal(Sys.getenv('MY_SECRET'), 'i_want_this')
})
test_that("Environment variable 2 exists", {
expect_equal(Sys.getenv('MY_SECRET2'), 'i_want_this_2')
})
Run Code Online (Sandbox Code Playgroud)
我的 GitHub Actions 文件如下:
# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master
name: R-CMD-check
jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os …Run Code Online (Sandbox Code Playgroud) 我正在尝试重新创建类似的东西
sklearn.preprocessing.LabelEncoder
但是我不想使用sklearnor pandas。我只想使用numpyPython 标准库。这是我想要实现的目标:
import numpy as np
input = np.array([['hi', 'there'],
['scott', 'james'],
['hi', 'scott'],
['please', 'there']])
# Output would look like
np.ndarray([[0, 0],
[1, 1],
[0, 2],
[2, 0]])
Run Code Online (Sandbox Code Playgroud)
如果能够将其映射回来也很棒,这样结果就会再次看起来与输入完全相同。
我正在构建一个用于内部使用的包,并试图从用户中抽象出所有可能的数据库交互.我需要连接到数据库并断开与函数内的数据库的连接(我认为).但是,断开连接不起作用.
`my_func = function(){
con = DBI::dbConnect(RSQLite::SQLite(), 'db_location.sqlite')
r = DBI::dbSendQuery("SELECT * ...")
dat = DBI::dbFetch(r)
DBI::dbDisconnect(con)
return(dat)
}`
Run Code Online (Sandbox Code Playgroud)
如果你调用这个函数:
MY_LIBRARY::my_func()
返回数据但连接未终止并显示警告.
`Warning message:
In connection_release(conn@ptr) :
There are 1 result in use. The connection will be released when
they are closed`
Run Code Online (Sandbox Code Playgroud) 我正在尝试添加在 dplyr 中使用动态数量的变量进行过滤的功能。我希望用户能够在函数调用中以简单的方式输入命令,即...- 下面的示例应该会有所帮助。用户应该能够通过简单地将它们输入到函数中来分割出来seg1 == 'b',但所有尝试都失败了。使用标准 SQL 很容易做到这一点,只是不熟悉 NSE 格式。seg2 == 'd'my_func(example_data, seg1 = 'b', seg2 = 'd')
library('tidyverse')
example_data = tibble(seg1 = c('a','b','b','c'),
seg2 = c('d', 'd', 'd', 'e'),
out = c(1, 10, 20, 40))
my_func = function(dat, ...){
args = list(...)
arg_names = names(args)
### ????
dat = dat %>%
filter(???)
### ????
return(dat)
}
my_func(example_data, seg1 = 'b', seg2 = 'd')
# Desired output
> example_data %>% filter(seg1 == 'b', seg2 …Run Code Online (Sandbox Code Playgroud) 如何利用魔术点 (...) / 省略号来过滤任意列?
df = tibble::tibble(col1 = c('a', 'b', 'c'), col2 = c(1,3,4))
my_func = function(x, ...){
df %>%
dplyr::filter(... == x)
}
my_func('a', col1)
# Should return:
# A tibble: 1 x 2
col1 col2
<chr> <dbl>
1 a 1
Run Code Online (Sandbox Code Playgroud)