Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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将变量从一个脚本调用到另一个脚本?_R_Variables - Fatal编程技术网

如何使用R将变量从一个脚本调用到另一个脚本?

如何使用R将变量从一个脚本调用到另一个脚本?,r,variables,R,Variables,我解释我的问题:我有两个R脚本。我在第一个脚本中创建了一个变量,我想在第二个脚本中调用该变量。问题是要将这个变量写入第一个脚本,我需要写一些R行,但我不想将这些行重写到第二个脚本中 有什么帮助吗 编辑:在上一个主题中,我在第二个R脚本中尝试了以下几行: loadRData <- function(fileName) { #loads an RData file, and returns it load(fileName) print(ls()) n <- readli

我解释我的问题:我有两个R脚本。我在第一个脚本中创建了一个变量,我想在第二个脚本中调用该变量。问题是要将这个变量写入第一个脚本,我需要写一些R行,但我不想将这些行重写到第二个脚本中

有什么帮助吗

编辑:在上一个主题中,我在第二个R脚本中尝试了以下几行:

loadRData <- function(fileName) {
  #loads an RData file, and returns it
  load(fileName)
  print(ls())
  n <- readline(prompt="combs")  #combs is the name of my variable 
  get(ls()[as.matrix(n)])
}

select_var <- loadRData('RHO_COR.R') #RHO_COR.R is the name of my first script , I execute this command in my second script 

你可能想看看
源代码
。你的意思是我应该
源代码
将我的第一段代码转换成第二段代码?是的,脚本的来源是一个选项。只需
source(myfile.R)
即可执行整个脚本(所有变量都已加载)。如果您真的只需要脚本中的一个变量,有一些技巧可以做到这一点(例如,使用
rm
,在
source
中使用可选参数重写脚本)。或者,将变量写入文件,然后将其读回。请参见
?write.csv
?read.csv
。使用源代码更容易、更快,但可能会导致加载不需要的变量、包或函数。我不知道如何使用
rm
?如果我这样做:#script1
x=my_变量
save(x,file=“data.Rda”)
#script2
x=load('data.Rda')
y=get(x
)<代码>rm(x),它应该工作吗?
Error in load(fileName) :
bad restore file magic number (file may be corrupted) -- no data loaded
In addition: Warning message:
file ‘RHO_COR.R’ has magic number 'libra'
   Use of save versions prior to 2 is deprecated