Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop R from parsing ~ to user's home directory

I try to run the following script in R (minimized example):

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))

The problem is that R is parsing this as following (using Linux):

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

How can I prevent R from parsing ~ into /home/[user-directory]?

like image 506
stfn Avatar asked Feb 19 '23 21:02

stfn


1 Answers

Try using the ,text= argument. Right now you're using the ,file= argument to parse since that comes first:

> 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)
like image 70
Ari B. Friedman Avatar answered Feb 22 '23 00:02

Ari B. Friedman