4

我尝试在 R 中运行以下脚本(最小化示例):

library(neuralnet)

arrhythmia <- read.csv(file=".../arrhythmia-edited.csv", head=TRUE, sep=",")

# create the first parameter for neuralnet formular 
# format like 'classification ~ attribute1 + attribute2 + ...
# i have so many features, that why i use a for
input <- ""
for (i in 1:259)
  input <- paste(input, paste(paste('arrhythmia[,',i),'] +'))
input <- paste(input, 'arrhythmia[,260]')

# create string for function call
nnet.func <- paste('neuralnet(arrhythmia[,261] ~', input)
nnet.func <- paste(nnet.func, ', data=arrhythmia)')

# call function neuralnet
# should be like: neuralnet(arrhythmia[,261] ~ arrhythmia[,1] + arrhythmia[,2] + ... + arrhytmia[,260], data=arrhythmia)
net.arrhythmia <- eval(parse(nnet.func))

问题是 R 将其解析如下(使用 Linux):

'neuralnet(arrhythmia[,261] /home/user  arrhythmia[, 1 ] + ....

如何防止 R 将 ~ 解析为 /home/[user-directory]?

4

1 回答 1

8

尝试使用,text=参数。现在你正在使用,file=参数 to ,parse因为它是第一位的:

> args('parse')
function (file = "", n = NULL, text = NULL, prompt = "?", srcfile = NULL, 
    encoding = "unknown") 

> parse("test ~ test")
Error in file(filename, "r") : cannot open the connection

> parse(text="test ~ test")
expression(test ~ test)
于 2012-10-10T12:31:17.860 回答