Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:我可以运行source()和#x2019;不改变主环境的d代码?_R_Include_Environment_Child Process - Fatal编程技术网

R:我可以运行source()和#x2019;不改变主环境的d代码?

R:我可以运行source()和#x2019;不改变主环境的d代码?,r,include,environment,child-process,R,Include,Environment,Child Process,在R中,我想要source()为其他目的编写的代码,以获取它创建的一些对象。但我不希望源代码覆盖主程序中的对象,也不希望源代码加载可能会屏蔽我在主程序中使用的函数的库(比如在dplyr之后加载plyr将如何屏蔽某些dplyr函数) 在纯代码(nosource()'d code)中,我可以在子环境中运行一些代码,而不会影响主环境对象,只需将选定对象从子环境传递回主环境: myGlobal <- "initial value" # create an object in

在R中,我想要
source()
为其他目的编写的代码,以获取它创建的一些对象。但我不希望源代码覆盖主程序中的对象,也不希望源代码加载可能会屏蔽我在主程序中使用的函数的库(比如在dplyr之后加载plyr将如何屏蔽某些dplyr函数)

在纯代码(no
source()
'd code)中,我可以在子环境中运行一些代码,而不会影响主环境对象,只需将选定对象从子环境传递回主环境:

myGlobal <- "initial value"  # create an object in the main environment
objectsOfMyDesire <- with(new.env(), {
  myGlobal <- paste(myGlobal, "CHANGED by the with()"); message(paste0("in with(): myGlobal = ", myGlobal, "\""))  # alter object inherited from main environment
  mySub <- paste("mySub:", toupper(myGlobal)); message(paste0("in with(), mySub = ", mySub))    # create object
  return(list("mySub" = mySub))  # Return the created objects that I want to keep
})  # run code within a child environment, and return certain values to the main environment.

# Result: in the main environment, myGlobal is unchanged, mySub does not exist, but objectsOfMyDesire$mySub has been created
myGlobal; objectsOfMyDesire$mySub
mySub
# Put code from within the "with()" above into a file, and then source() that file within a "with()":
rm(myGlobal, objectsOfMyDesire, mySub)
myTmpFile <- tempfile(pattern = "file", tmpdir = tempdir(), fileext = ".txt")
writeLines(con = myTmpFile, text = 'myGlobal <- paste(myGlobal, "CHANGED by the with()"); message(paste0("in with(): myGlobal = ", myGlobal));\n mySub <- paste("mySub:", toupper(myGlobal)); message(paste0("in with(), mySub = ", mySub))') 

# Having created a file to source(), run the example above, replacing some of the "with()" code with "source(myTmpFile)":
myGlobal <- "initial value"  # create an object in the main environment
objectsOfMyDesire <- with(new.env(), {
  source(myTmpFile)
  return(list("mySub" = mySub))  # Return the created objects that I want to keep
})  # run code within a child environment, and return certain values to the main environment.

# Result: in the main environment, myGlobal is CHANGED, mySub EXISTS, and objectsOfMyDesire$mySub has been created
myGlobal; objectsOfMyDesire$mySub
mySub

file.remove(myTmpFile)  # delete the temporary file

那么,除了传回我明确指定的内容之外,我是否有办法从主程序中
source()
程序,而不让源程序影响主程序环境?

我找到了一个部分解决方案:

# Call the program that creates some objects I want to use in the current program.
#   Run it in a new, child environment that I'll name calledProgram, so that it will not affect objects in the main environment.
source(file=programFileAndPath, local = calledProgram <- new.env(), echo=T)
# The called programs objects are now in the calledProgram object.
#   So I now add code to bring the objects I wanted from the called program into the main environment.
usefulDataFrame <- calledProgram$usefulDataFrame
niceGGPlot      <- calledProgram$niceGGPlot
rm(calledProgram)  # I have moved the objects that I need into the main environment, so I now delete the calledProgram object
#调用创建我要在当前程序中使用的某些对象的程序。
#在我将命名为calledProgram的新子环境中运行它,这样它就不会影响主环境中的对象。

source(file=programFileAndPath,local=calledProgram不
source(myTmpFile,local=TRUE)
做你想做的事吗?但对我来说,这似乎真的是对
source()的滥用
命令。最好只用于编写R函数,以便您以后调用。将脚本视为函数本身不允许您利用正常的R作用域规则。或者只是为更可重用的代码制作自己的包。我还想知道…您为什么要这样做…真的有理由不这样做吗他的方法?@MrFlick-thanking!local=TRUE解决了我问题的前半部分。使用该选项,在源代码中创建的对象不再在我的主环境中创建。关于函数和包:源代码包括许多由许多人出于许多不同原因编写的程序-我不想要求我们的分析小组以函数或包的形式编写他们的特别程序。@Steffen-我在一家政府机构中运行了一个相当大的分析小组。我们有许多分析师用R编写有用的程序。有些程序生成对象(如图形或表格)我们希望集成到多个仪表板、报告、电子邮件等中,或者它们可能会创建一个具有良好、更广泛用途的数据框架。我正在尝试创建一种方法,我们可以利用彼此的工作,而不必对如何完成工作施加太多限制。
# Call the program that creates some objects I want to use in the current program.
#   Run it in a new, child environment that I'll name calledProgram, so that it will not affect objects in the main environment.
source(file=programFileAndPath, local = calledProgram <- new.env(), echo=T)
# The called programs objects are now in the calledProgram object.
#   So I now add code to bring the objects I wanted from the called program into the main environment.
usefulDataFrame <- calledProgram$usefulDataFrame
niceGGPlot      <- calledProgram$niceGGPlot
rm(calledProgram)  # I have moved the objects that I need into the main environment, so I now delete the calledProgram object