2

I am trying to write files and getting error:

this is what I am doing:

sites<-c("New York", "Boston", "San Francisco")
p<-1:10
for(i in 1:length(sites)) {
    print(sites[i])
        writeLines(p, paste0(gsub(" ", "_", sites[i]),".txt")
}

I get this error:

Error in writeLines(p, gsub(" ", "_", sites[i])) : 
  invalid 'text' argument
user1471980
  • 10,127
  • 48
  • 136
  • 235
  • 1
    The error message "Error: unexpected '}'" generally means you forgot a right-paren. – IRTFM Feb 05 '14 at 01:15
  • When you correct that error the next error message " invalid 'text' argument" generally means you need to use a coercion operation, in this case it woudl obviously be adding `as.character" to make `p` something that `writeLines` knows what to do with. – IRTFM Feb 05 '14 at 01:18
  • To explain my down vote it's just because the provided code does not create the reported error. Hence lack of research effort and not useful. – IRTFM Feb 05 '14 at 01:20
  • @IShouldBuyBoat, I have done tremendous amount of work. I have list of names, I'm trying to create file names based on the name of the list and write to them. I am truly stuck. Therefore I posted here. just an FYI. – user1471980 Feb 05 '14 at 01:26
  • IShouldBuyABoat's initial comment should prompt you to edit your question and add a `)` to close `writeLines`. Once you do that, the error message you describe appears. – jbaums Feb 05 '14 at 01:30
  • It's very unclear what your intended output is. It would help if you provided an example of what you are expecting your code to produce. – jbaums Feb 05 '14 at 01:32
  • 1
    I deleted my answer since IShouldBuyABoat fixed it already. The code you have is fine, just change p (an integer) to `as.character(p)` and it works as intended. writeLines can only take a character vector, not an integer. – JeremyS Feb 05 '14 at 01:42
  • @IShouldBuyABoat, can you post your comment as an answer so that I can accept it. It worked. Thank you. – user1471980 Feb 05 '14 at 02:04

1 Answers1

4
sites<-c("New York", "Boston", "San Francisco")
p<-1:10
for(i in 1:length(sites)) {
    print(sites[i])
        writeLines(as.character(p), paste0(gsub(" ", "_", sites[i]),".txt"))
}
#----
[1] "New York"
[1] "Boston"
[1] "San Francisco"
IRTFM
  • 258,963
  • 21
  • 364
  • 487