gpt4 book ai didi

R:本地环境中的 testthat test_file() 问题

转载 作者:行者123 更新时间:2023-11-28 19:59:19 25 4
gpt4 key购买 nike

我在本地环境中调用 test_file 时遇到问题。

local({ newvar <- 1; expect_equal(newvar, 1); });

工作正常。

local({ 
newvar <- 1;
test_that('newvar is equal to 1:', { expect_equal(newvar, 1) });
});

工作正常。

local( { newvar <- 1; test_file('simple.test.R'); });

错误:找不到对象'newvar'

simple.test.R 的内容很简单:

context('local env test');
test_that('local env test', { expect_equal(newvar, 1) })

感谢帮助!谢谢。


编辑:

我想做的是从 shinyAce ( https://github.com/trestletech/shinyAce ) 中读取一些代码,并检查它是否有效(满足一些定义的要求)。我使用的是“local()”,这样在 shinyAce block 中定义的任何已分配变量都不会保留在环境中。

最佳答案

这是test_file 的来源:

function (path, reporter = "summary") 
{
reporter <- find_reporter(reporter)
with_reporter(reporter, {
sys.source(path, new.env(parent = globalenv()), chdir = TRUE)
end_context()
})
}

关键行是这样的:

sys.source(path, new.env(parent = globalenv()), chdir = TRUE)

文件在全局环境下的新环境中执行,而您的newvar仅在您创建的本地环境中可用。

您的最终目标到底是什么?我们可以尝试提供帮助。还是您只是好奇?

关于R:本地环境中的 testthat test_file() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19992449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com