在不附加包的情况下评估包环境中的功能
Evaluate function within package environment without attaching package
背景
- 我想评估一组具有包环境的 R 函数没有附加这个包
- 我想避免使用
package::
例子
数据
给定样本虚拟数据集:
# Data --------------------------------------------------------------------
tmpCSV <- tempfile(fileext = ".CSV", pattern = "mtcars_data_")
write.csv(x = mtcars[, 1:5], file = tmpCSV, row.names = FALSE)
# Confirm
# readLines(con = tmpCSV)[1]
阅读
library
我可以使用 read_csv
function offered within the readr
包阅读它。
library(readr)
dta <- read_csv(
file = tmpCSV,
col_types = cols(
mpg = col_double(),
cyl = col_integer(),
disp = col_integer(),
hp = col_integer(),
drat = col_double()
)
)
readr::
也可以直接调用readr::
函数:
# detach("package:readr", unload=TRUE)
dta <- readr::read_csv(
file = tmpCSV,
col_types = readr::cols(
mpg = readr::col_double(),
cyl = readr::col_integer(),
disp = readr::col_integer(),
hp = readr::col_integer(),
drat = readr::col_double()
)
)
问题
我想使用 eval/evalq
(如果可能)得到相同的结果。所需的语法类似于:
eval(expr = read_csv(
file = tmpCSV,
col_types = cols(
mpg = col_double(),
cyl = col_integer(),
disp = col_integer(),
hp = col_integer(),
drat = col_double()
)
),
# Naturally, the "" bit does not make sense
envir = "package::readr")
预期错误:
Error in read_csv(file = tmpCSV, col_types = cols(mpg =
col_double(),
: could not find function "read_csv"
备注
该任务主要关注访问包函数无需加载包和无需直接通过[=调用函数25=] 和 :::
。概念上的等价物是使用 with
函数并引用没有 $
:
的数据框列
with(mtcars, t.test(disp ~ am))
更好的例子:
with(mtcars, mpg[cyl == 8 & disp > 350])
就用with
?我不明白你为什么不想使用 ::
.
setwd("E:/temp")
tmpCSV <- tempfile(fileext = ".CSV", pattern = "mtcars_data_")
write.csv(x = mtcars[, 1:5], file = tmpCSV, row.names = FALSE)
dta <- readr::read_csv(
file = tmpCSV,
col_types = readr::cols(
mpg = readr::col_double(),
cyl = readr::col_integer(),
disp = readr::col_integer(),
hp = readr::col_integer(),
drat = readr::col_double()
)
)
sessionInfo()
#attached base packages:
# [1] stats graphics grDevices datasets utils methods base
#loaded via a namespace (and not attached):
#[1] readr_1.1.1 compiler_3.4.4 assertthat_0.2.0 R6_2.2.2 cli_1.0.0
#[6] hms_0.4.2 tools_3.4.4 pillar_1.2.1 rstudioapi_0.7 tibble_1.4.2
#[11] crayon_1.3.4 Rcpp_0.12.16 utf8_1.1.3 pkgconfig_2.0.1 rlang_0.2.0
#[16] fortunes_1.5-4
dtb <- with(asNamespace("readr"), read_csv(
file = tmpCSV,
col_types = cols(
mpg = col_double(),
cyl = col_integer(),
disp = col_integer(),
hp = col_integer(),
drat = col_double()
)))
#same happens here
identical(dta, dtb)
#[1] TRUE
背景
- 我想评估一组具有包环境的 R 函数没有附加这个包
- 我想避免使用
package::
例子
数据
给定样本虚拟数据集:
# Data --------------------------------------------------------------------
tmpCSV <- tempfile(fileext = ".CSV", pattern = "mtcars_data_")
write.csv(x = mtcars[, 1:5], file = tmpCSV, row.names = FALSE)
# Confirm
# readLines(con = tmpCSV)[1]
阅读
library
我可以使用 read_csv
function offered within the readr
包阅读它。
library(readr)
dta <- read_csv(
file = tmpCSV,
col_types = cols(
mpg = col_double(),
cyl = col_integer(),
disp = col_integer(),
hp = col_integer(),
drat = col_double()
)
)
readr::
也可以直接调用readr::
函数:
# detach("package:readr", unload=TRUE)
dta <- readr::read_csv(
file = tmpCSV,
col_types = readr::cols(
mpg = readr::col_double(),
cyl = readr::col_integer(),
disp = readr::col_integer(),
hp = readr::col_integer(),
drat = readr::col_double()
)
)
问题
我想使用 eval/evalq
(如果可能)得到相同的结果。所需的语法类似于:
eval(expr = read_csv(
file = tmpCSV,
col_types = cols(
mpg = col_double(),
cyl = col_integer(),
disp = col_integer(),
hp = col_integer(),
drat = col_double()
)
),
# Naturally, the "" bit does not make sense
envir = "package::readr")
预期错误:
Error in
read_csv(file = tmpCSV, col_types = cols(mpg = col_double(),
: could not find function"read_csv"
备注
该任务主要关注访问包函数无需加载包和无需直接通过[=调用函数25=] 和 :::
。概念上的等价物是使用 with
函数并引用没有 $
:
with(mtcars, t.test(disp ~ am))
更好的例子:
with(mtcars, mpg[cyl == 8 & disp > 350])
就用with
?我不明白你为什么不想使用 ::
.
setwd("E:/temp")
tmpCSV <- tempfile(fileext = ".CSV", pattern = "mtcars_data_")
write.csv(x = mtcars[, 1:5], file = tmpCSV, row.names = FALSE)
dta <- readr::read_csv(
file = tmpCSV,
col_types = readr::cols(
mpg = readr::col_double(),
cyl = readr::col_integer(),
disp = readr::col_integer(),
hp = readr::col_integer(),
drat = readr::col_double()
)
)
sessionInfo()
#attached base packages:
# [1] stats graphics grDevices datasets utils methods base
#loaded via a namespace (and not attached):
#[1] readr_1.1.1 compiler_3.4.4 assertthat_0.2.0 R6_2.2.2 cli_1.0.0
#[6] hms_0.4.2 tools_3.4.4 pillar_1.2.1 rstudioapi_0.7 tibble_1.4.2
#[11] crayon_1.3.4 Rcpp_0.12.16 utf8_1.1.3 pkgconfig_2.0.1 rlang_0.2.0
#[16] fortunes_1.5-4
dtb <- with(asNamespace("readr"), read_csv(
file = tmpCSV,
col_types = cols(
mpg = col_double(),
cyl = col_integer(),
disp = col_integer(),
hp = col_integer(),
drat = col_double()
)))
#same happens here
identical(dta, dtb)
#[1] TRUE