0

I have R code as below. Below code resides in a file called 'iot.R'. I am executing it in Linux.

I want to print content of variable 'fileinformation' to a file mentioned by file=fileConn... I thought that the 3rd line will solve the issue, but it is not giving the required output :(

   fileinformation = system(paste("file", filenames[1]))
    #print(fileinformation)
    cat(print(fileinformation),"\r\n","\r\n", file=fileConn)

When I run the file, i get below result. It prints to my screen, rather than writing to the file :(

> source('iot.R')
CH7Data_20130401T135010.csv: ASCII text, with CRLF line terminators
[1] 0

--------------------update1

I also tried below command, but didnt get the expected rsult

cat(capture.output(fileinformation),"\r\n","\r\n", file=fileConn)
user2543622
  • 5,760
  • 25
  • 91
  • 159

2 Answers2

2

You need to set the intern argument to TRUE in your call to system. For instance:

    fileinformation<-system("file cinzia_2.gif",intern=TRUE)
    fileinformation
    #[1] "cinzia_2.gif: GIF image data, version 89a, 640 x 640"

Of course I tried a file on my pc. Setting intern to TRUE the return value of system becomes the console output of the command. Then, when you call cat, you don't need to enclose fileinformation into print, but a simple cat(fileinformation,"\r\n","\r\n", file=fileConn) will suffice.

nicola
  • 24,005
  • 3
  • 35
  • 56
0

Hi Just a comment as I dont have enough rep to comment in the normal way. but cant you use

write.table

to save the output to a file? It may be easier?

mattbawn
  • 1,358
  • 2
  • 13
  • 33