Skip to content
Prev 66631 / 398525 Next

Can I use a variable to pass a file name to scan() ?

What operating system and which version of R? 

      I got a similar error message under R 2.0.1 under Windows 2000: 

 > y <- scan(file = x[1])
Error in scan(file = x[1]) : "scan" expected a real, got "?????"

      Part of the function definition for "scan" is 'scan(file = "", 
what = double(0), ...'.  This means that by default, "scan" was 
expecting double precision numbers.  When it found an unrecognizable 
binary, it complained.  Using 'what="raw"' produced something: 

 > y <- scan(file = x[1], what="raw")
Read 1 items

      The file x[1] was an MS Word *.wbk file, and scan was only able to 
read a portion of the header using "raw". 

      Have you considered "try"? 

 > y <- try(scan(file=x[1]))
Error in scan(file = x[1]) : "scan" expected a real, got "?????"
 > y
[1] "Error in scan(file = x[1]) : \"scan\" expected a real, got 
\"??\021??\261\"\n" 
attr(,"class")
[1] "try-error"

       What are you trying to do?  The following worked for me just now? 

 > write(pi, "pi.txt")
 > scan('pi.txt')
Read 1 items
[1] 3.141593
 > all.equal(scan('pi.txt'), pi)
Read 1 items
[1] "Mean relative  difference: 1.102658e-07"

      hope this helps.  spencer graves
Andrew Peterson wrote: