Please guide, I'm exporting data in '.csv' format in the Windows user directory, I have full access to. The write operation happens within a for loop. Each iteration exports data in csv format in the user directory. The issue is that the data gets exported for all the 9 iterations but fails for 10th iteration giving below error message. Error in file(file, ifelse(append, "a", "w")) : cannot open the connection In addition: Warning message: In file(file, ifelse(append, "a", "w")) : cannot open file './/posts_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng h?nh c?ng line_752518568125567.csv': Invalid argument Called from: file(file, ifelse(append, "a", "w")) The export directory path is current directory with 'name' extracted as below: my.file1: .//likes_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng h?nh c?ng line_752518568125567.csv my.file2: .//posts_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng h?nh c?ng line_752518568125567.csv I wondering if the error is due to the the complicated filename ? Below is the line of code I have used within loop to export data. my.file1 <- file.path('./', paste0('likes','_',name,'_',grp_id,'.csv')) my.file2 <- file.path('./', paste0('posts','_',name,'_',grp_id,'.csv')) write.csv(posts_frame, file=my.file2, row.names = F) write.csv(likes_frame, file = my.file1, row.names = F) Regards, Sunny
Hi, You are defeating the purpose of the file.path() function by providing a path separator in the first argument; you used './', but try instead... my.file1 <- file.path('.', paste0('likes','_',name,'_',grp_id,'.csv')) Also, there appear to be spaces in the 'name' argument - that might be causing you an issue but it is hard to know. You might try wrapping the value of my.file1 in quotes using shQuote() or maybe even dQuote() - I'm not sure as I am not on Windows. Cheers, Ben> On Mar 21, 2016, at 7:53 AM, Sunny Singha <sunnysingha.analytics at gmail.com> wrote: > > Please guide, > I'm exporting data in '.csv' format in the Windows user directory, I > have full access to. The write operation happens within a for loop. > > Each iteration exports data in csv format in the user directory. The > issue is that the data gets exported for all the 9 iterations but > fails for 10th iteration giving below error message. > > Error in file(file, ifelse(append, "a", "w")) : > cannot open the connection > In addition: Warning message: > In file(file, ifelse(append, "a", "w")) : > cannot open file './/posts_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i > d<U+1ED3>ng h?nh c?ng line_752518568125567.csv': Invalid argument > Called from: file(file, ifelse(append, "a", "w")) > > The export directory path is current directory with 'name' extracted as below: > my.file1: .//likes_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng > h?nh c?ng line_752518568125567.csv > my.file2: .//posts_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng > h?nh c?ng line_752518568125567.csv > > I wondering if the error is due to the the complicated filename ? > Below is the line of code I have used within loop to export data. > > my.file1 <- file.path('./', paste0('likes','_',name,'_',grp_id,'.csv')) > my.file2 <- file.path('./', paste0('posts','_',name,'_',grp_id,'.csv')) > > write.csv(posts_frame, file=my.file2, row.names = F) > write.csv(likes_frame, file = my.file1, row.names = F) > > Regards, > Sunny > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org
(Re-posting after including R-help) Thanks Ben, Changing './' and '.' and shQuote() didn't work. I'm trying dQuote(). For your information the 'name' value retrieved is --> H?i nh?ng ng??i ??ng h?nh c?ng I guess the string is in Vietnamese language. As you would have observed in error message it wasn't interpreted as above but some other nonsensical characters (posts_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng> h?nh c?ng line).Is there a workaround to handle such strings ? Regards, Sunny On Mon, Mar 21, 2016 at 6:29 PM, Sunny Singha <sunnysingha.analytics at gmail.com> wrote:> Thanks Ben, > Changing './' and '.' and shQuote() didn't work. I'm trying dQuote(). > For your information the 'name' value retrieved is --> H?i nh?ng ng??i > ??ng h?nh c?ng > > I guess the string is in Vietnamese language. As you would have > observed in error message it wasn't interpreted as above but some > other nonsensical characters (posts_H<U+1ED9>i nh<U+1EEF>ng > ngu<U+1EDD>i d<U+1ED3>ng >> h?nh c?ng line). > Is there a workaround to handle such strings ? > > Regards, > Sunny > > On Mon, Mar 21, 2016 at 5:35 PM, Ben Tupper <btupper at bigelow.org> wrote: >> Hi, >> >> You are defeating the purpose of the file.path() function by providing a path separator in the first argument; you used './', but try instead... >> >> my.file1 <- file.path('.', paste0('likes','_',name,'_',grp_id,'.csv')) >> >> Also, there appear to be spaces in the 'name' argument - that might be causing you an issue but it is hard to know. You might try wrapping the value of my.file1 in quotes using shQuote() or maybe even dQuote() - I'm not sure as I am not on Windows. >> >> Cheers, >> Ben >> >>> On Mar 21, 2016, at 7:53 AM, Sunny Singha <sunnysingha.analytics at gmail.com> wrote: >>> >>> Please guide, >>> I'm exporting data in '.csv' format in the Windows user directory, I >>> have full access to. The write operation happens within a for loop. >>> >>> Each iteration exports data in csv format in the user directory. The >>> issue is that the data gets exported for all the 9 iterations but >>> fails for 10th iteration giving below error message. >>> >>> Error in file(file, ifelse(append, "a", "w")) : >>> cannot open the connection >>> In addition: Warning message: >>> In file(file, ifelse(append, "a", "w")) : >>> cannot open file './/posts_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i >>> d<U+1ED3>ng h?nh c?ng line_752518568125567.csv': Invalid argument >>> Called from: file(file, ifelse(append, "a", "w")) >>> >>> The export directory path is current directory with 'name' extracted as below: >>> my.file1: .//likes_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng >>> h?nh c?ng line_752518568125567.csv >>> my.file2: .//posts_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng >>> h?nh c?ng line_752518568125567.csv >>> >>> I wondering if the error is due to the the complicated filename ? >>> Below is the line of code I have used within loop to export data. >>> >>> my.file1 <- file.path('./', paste0('likes','_',name,'_',grp_id,'.csv')) >>> my.file2 <- file.path('./', paste0('posts','_',name,'_',grp_id,'.csv')) >>> >>> write.csv(posts_frame, file=my.file2, row.names = F) >>> write.csv(likes_frame, file = my.file1, row.names = F) >>> >>> Regards, >>> Sunny >>> >>> ______________________________________________ >>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >> >> Ben Tupper >> Bigelow Laboratory for Ocean Sciences >> 60 Bigelow Drive, P.O. Box 380 >> East Boothbay, Maine 04544 >> http://www.bigelow.org >>
Duncan Murdoch
2016-Mar-21 13:53 UTC
[R] Please guide -- Error during export in csv format
On 21/03/2016 7:53 AM, Sunny Singha wrote:> Please guide, > I'm exporting data in '.csv' format in the Windows user directory, I > have full access to. The write operation happens within a for loop. > > Each iteration exports data in csv format in the user directory. The > issue is that the data gets exported for all the 9 iterations but > fails for 10th iteration giving below error message. > > Error in file(file, ifelse(append, "a", "w")) : > cannot open the connection > In addition: Warning message: > In file(file, ifelse(append, "a", "w")) : > cannot open file './/posts_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i > d<U+1ED3>ng h?nh c?ng line_752518568125567.csv': Invalid argument > Called from: file(file, ifelse(append, "a", "w")) > > The export directory path is current directory with 'name' extracted as below: > my.file1: .//likes_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng > h?nh c?ng line_752518568125567.csv > my.file2: .//posts_H<U+1ED9>i nh<U+1EEF>ng ngu<U+1EDD>i d<U+1ED3>ng > h?nh c?ng line_752518568125567.csv > > I wondering if the error is due to the the complicated filename ? > Below is the line of code I have used within loop to export data. > > my.file1 <- file.path('./', paste0('likes','_',name,'_',grp_id,'.csv')) > my.file2 <- file.path('./', paste0('posts','_',name,'_',grp_id,'.csv')) > > write.csv(posts_frame, file=my.file2, row.names = F) > write.csv(likes_frame, file = my.file1, row.names = F) >The <U+1ED9> and similar strings represent Unicode characters. I would guess that they are being translated to the <U+1ED9> format before being used as a filename, and the < and > symbols are not allowed in filenames in Windows. This is a limitation of the way R handles non-ASCII characters in Windows, and there's not much you can do about it other than avoiding characters which do not display in Latin1 (or whatever R sees as your native character set). Duncan Murdoch