Error in writeLines: invalid 'text' argument

I'm trying to write a line based on the below matrix,

print(mat)

     [,1]        [,2]     
[1,] "wesd"     "2.0E-5" 
[2,] "lesd"     "8.0E-8" 
[3,] "nf"       "16.0"   
[4,] "ldop"     "1.25E-6"
[5,] "lsop"     "2.0E-7" 
[6,] "esdoxide" "1.0"    
[7,] "gns"      "1.0" 

Below is my code, .

ipFile = 'text.txt'
fConn <- file(ipFile,open="w")
paramname <- c("a","b","c","d","e","f","g")
for(a in 1:length(paramname)) {
        
        
          tmpvar = 1
        #Using the matrix value printed above
          writeLines(c('parameters ',mat[a,1],'_',as.character(tmpvar),'=',as.character(mat[a,2]),'\n'),con=fConn,sep="")
        
      a=a+1
}

   close(fconn)

I'm getting the below Error,

Error in writeLines: invalid 'text' argument

I tried to change the integer values used in writeLines to as.character but no hope. And meanwhile when I try to run the above code in the console it works well (by removing the conn=fconn argument in the writeLines code). This code doesn't work when I try to run from the rstudio app.

Instead of c() together string fragments you should paste0() them together. Also glue() is very good at the cost of another dependency

1 Like

Yes paste worked! May I know why it worked in console though without paste?

There must have been another relevant difference because it would be needed for what you are doing.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.