R:eval(parse())错误消息:即使"text ="也无法打开文件在解析中指定 [英] R: eval(parse()) error message: cannot open file even though "text=" is specified in parse

查看:433
本文介绍了R:eval(parse())错误消息:即使"text ="也无法打开文件在解析中指定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我多次对一个国家列表进行分析,并且在每次迭代期间,应将结果添加到向量中.下面我展示了一个简化的示例,其中没有一个国家的循环.即使我彻底寻找解决方案,也找不到答案.

I'm running analysis for a list of countries several times and during each iteration the result should be added to a vector. Below I show a simplified example without the loop for just one country. Even though I thoroughly looked for solutions, I could not find an answer.

#this is my simplified country vector with just 1 country    
country<-c("Spain")

#This is the vector that should hold the results of multiple iterations
#for now, it contains only the result of the first iteration   
Spain.condition1<- 10

#reading result vector in a variable (this is automized in a loop in my code)
resultVector<-paste(country,"condition1",sep=".")

#when I call the content of the vector with parse, eval
#I see the content of the vector as expected

eval(parse(text=resultVector))

#however, when I try to add a second result to it

eval(parse(text=resultVector))[2]<-2

#I get following error message: 

#Error in file(filename, "r") : cannot open the connection
#In addition: Warning message:
#In file(filename, "r") :
#  cannot open file 'Spain.condition1': No such file or directory

有人可以帮助我还是让我朝正确的方向前进?

Could anyone help me or put me in the right direction?

推荐答案

不能保证分配给eval的工作.这是多种原因之一,通常使用eval并不是一个好主意.

Assigning to eval isn't guaranteed to work. This is one of multiple reasons it's usually not a good idea to use eval.

为什么不将国家及其条件存储在命名列表中,如下所示:

Why not just store countries and their conditions in a named list, something like this:

conditions = list()
conditions[["Spain"]] = list()
conditions[["Spain"]][["condition1"]] <- 10
conditions[["Spain"]][["condition1"]][2] <- 2

conditions[["Spain"]][["condition1"]]
# [1] 10  2

ETA:使用循环(我不确切知道问题的结构是什么,但这是总体思路):

ETA: To work with a loop (I don't know precisely what the structure of your problem is, but here's the general idea):

countries = c("Spain", "England", "France", "Germany", "USA") # and so on
conditions = c("Sunny", "Rainy", "Snowing") # or something

data = list()
for (country in countries) {
    data[[country]] <- list()
    for (condition in conditions) {
        data[[country]][[condition]] <- 4 # assign appropriate value here
    }
}

它也可以由制表符分隔的文件构造,或以适合您的问题的任何方式生成-R胜任其能力.

It can also be constructed from a tab-delimited file, or generated in whatever way is appropriate for your problem- R is more than capable.

这篇关于R:eval(parse())错误消息:即使"text ="也无法打开文件在解析中指定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆