如何在不覆盖当前变量的情况下获取R代码?

时间:2022-01-18 22:58:48

I am trying to retrieve results by executing R code with the source command. Because there are some variables with the same name, the variables in the executed R file will overwrite the current variables. How do I retrieve the result without overwriting the current variables?

我试图通过使用source命令执行R代码来检索结果。由于某些变量具有相同的名称,因此执行的R文件中的变量将覆盖当前变量。如何在不覆盖当前变量的情况下检索结果?

#main.R Code
b=0
source('sub.R')
if(a>1){print(T)}else{print(F)}

#sub.R
b=1
test<-function(x){x=1}
a=test(b)

I want to only retrieve a from sub.R without b in main.R being overwritten by the same name variable in sub.R. Essentially, I want to execute a R file like calling a method with just keeping the return value.

我想只检索一个来自sub.R而没有b在main.R中被sub.R中的同名变量覆盖。本质上,我想执行一个R文件,就像调用一个只保留返回值的方法一样。

1 个解决方案

#1


9  

You can source the contents into a specific environment with sys.source if you like. For example

如果您愿意,可以使用sys.source将内容源代码发送到特定环境中。例如

b <- 0
ee <- new.env()
sys.source('sub.R', ee)
ee$a
# [1] 1     # the ee envir has the result of the sourcing
if(ee$a>1) {print(T)} else{print(F)}
# [1] FALSE
b
# [1] 0     #still zero

But if you're looking to source files like they are functions, you should just include a function in the sourced file and then call that function in your main file. Don't try to pass around values in the environment at all. For example

但是,如果您希望将源文件视为函数,则应该在源文件中包含一个函数,然后在主文件中调用该函数。不要试图在环境中传递值。例如

# sub.R  --------------
runsub<-function() {
    b=1
    test<-function(x){x=1}
    a=test(b)
    a
}

# main.R  -------------
b <- 0
source('sub.R')
a <- runsub()
if(a>1){print(T)}else{print(F)}

Or if you want to write a helper function to return a specific value from a sourced environment, you can do

或者,如果要编写辅助函数以从源环境返回特定值,则可以执行此操作

sourceandgetvar <- function(filename, varname) {
    ee <- new.env()
    sys.source(filename, ee)
    stopifnot(varname %in% ls(envir=ee))
    ee[[varname]]
}

Then you can change main.R to

然后你可以将main.R改为

b <- 0
a <- sourceandgetvar("sub.R", "a")
if(a>1) {print(T)} else {print(F)}
# [1] FALSE
b
# [1] 0

#1


9  

You can source the contents into a specific environment with sys.source if you like. For example

如果您愿意,可以使用sys.source将内容源代码发送到特定环境中。例如

b <- 0
ee <- new.env()
sys.source('sub.R', ee)
ee$a
# [1] 1     # the ee envir has the result of the sourcing
if(ee$a>1) {print(T)} else{print(F)}
# [1] FALSE
b
# [1] 0     #still zero

But if you're looking to source files like they are functions, you should just include a function in the sourced file and then call that function in your main file. Don't try to pass around values in the environment at all. For example

但是,如果您希望将源文件视为函数,则应该在源文件中包含一个函数,然后在主文件中调用该函数。不要试图在环境中传递值。例如

# sub.R  --------------
runsub<-function() {
    b=1
    test<-function(x){x=1}
    a=test(b)
    a
}

# main.R  -------------
b <- 0
source('sub.R')
a <- runsub()
if(a>1){print(T)}else{print(F)}

Or if you want to write a helper function to return a specific value from a sourced environment, you can do

或者,如果要编写辅助函数以从源环境返回特定值,则可以执行此操作

sourceandgetvar <- function(filename, varname) {
    ee <- new.env()
    sys.source(filename, ee)
    stopifnot(varname %in% ls(envir=ee))
    ee[[varname]]
}

Then you can change main.R to

然后你可以将main.R改为

b <- 0
a <- sourceandgetvar("sub.R", "a")
if(a>1) {print(T)} else {print(F)}
# [1] FALSE
b
# [1] 0