繁体   English   中英

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

[英]R: Problems with testthat test_file() within local environment

我在本地环境中调用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 )读取一些代码,并检查它是否有效(mets一些定义的要求)。 我正在使用'local()',因此在shinyAce块中定义的任何指定变量都不会保留在环境中。

这是test_file的源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仅在您创建的本地环境中可用。

你的最终目标究竟是什么? 我们可以尝试提供帮助。 或者你只是好奇?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM