[R] error message from read.csv in loop

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Sat Jul 10 20:42:08 CEST 2021


On 10/07/2021 12:30 p.m., Bert Gunter wrote:
> "It seems that your problem is that you are using single quotes inside of
> the double quotes."
> 
> That is FALSE. From ?Quotes:
> "Single and double quotes delimit character constants. They can be
> used interchangeably but double quotes are preferred (and character
> constants are printed using double quotes), so single quotes are
> normally only used to delimit character constants containing double
> quotes."
> 
> Of course, pairs of each type of quote must properly match, must not
> get confused with quotes in the delineated string, etc. , but they are
> otherwise interchangeable. The whole of ?Quotes, especially the
> examples, is informative and worth the read (imo).

I think Migdonio is right.  From the error message the problem is that 
the filename was being specified as 
"'w:/project/_Joe.B/Oracle/data/ASSAY_DEFINITIONS.csv'"

That is not a legal filename:  the single quotes probably tell Windows 
to interpret it as a single filename entry, not drive, path, filename. 
Or maybe the drive is being interpreted as "'w", which isn't a legal 
drive id.

In any case, if you set f to an existing full path, and file.exists(f) 
returns TRUE, you'll find that file.exists(paste0("'", f, "'")) returns 
FALSE.

Duncan Murdoch

> 
> Bert Gunter
> 
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
> 
> On Sat, Jul 10, 2021 at 8:20 AM Migdonio González
> <migdonio.gonzalez02 using gmail.com> wrote:
>>
>>   It seems that your problem is that you are using single quotes inside of
>> the double quotes. This is not necessary. Here is the corrected for-loop:
>>
>> for (j in 1:nrow(ora))
>> {
>>          mycol  <- ora[j,"fname"]
>>          mycsv  <- paste0(mycol,".csv")
>>          rdcsv  <- noquote(paste0("w:/project/_Joe.B/Oracle/data/", mycsv))
>>          rr     <- read.csv(rdcsv)
>> }
>>
>> Also note that the rr variable will only store the last CSV, not all CSV.
>> You will need to initialize the rr variable as a list to store all CSVs if
>> that is what you require. Something like this:
>>
>> # Initialize the rr variable as a list.
>> rr <- as.list(rep(NA, nrow(ora)))
>>
>> # Run the for-loop to store all the CSVs in rr.
>> for (j in 1:nrow(ora))
>> {
>>          mycol  <- ora[j,"fname"]
>>          mycsv  <- paste0(mycol,".csv")
>>          rdcsv  <- noquote(paste0("w:/project/_Joe.B/Oracle/data/", mycsv))
>>          rr[[j]]     <- read.csv(rdcsv)
>> }
>>
>> Regards
>> Migdonio G.
>>
>> On Fri, Jul 9, 2021 at 1:10 PM Kai Yang via R-help <r-help using r-project.org>
>> wrote:
>>
>>> Hello List,
>>> I use for loop to read csv difference file into data frame rr.  The data
>>> frame rr will be deleted after a comparison and go to the next csv file.
>>> Below is my code:
>>> for (j in 1:nrow(ora))
>>> {
>>>    mycol  <- ora[j,"fname"]
>>>    mycsv  <- paste0(mycol,".csv'")
>>>    rdcsv  <- noquote(paste0("'w:/project/_Joe.B/Oracle/data/", mycsv))
>>>    rr     <- read.csv(rdcsv)
>>> }
>>> but when I run this code, I got error message below:
>>> Error in file(file, "rt") : cannot open the connection
>>> In addition: Warning message:
>>> In file(file, "rt") :
>>>    cannot open file
>>> ''w:/project/_Joe.B/Oracle/data/ASSAY_DEFINITIONS.csv'': No such file or
>>> directory
>>>
>>> so, I checked the rdcsv and print it out, see below:
>>> [1] 'w:/project/_Joe.B/Oracle/data/ASSAY_DEFINITIONS.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/ASSAY_DISCRETE_VALUES.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/ASSAY_QUESTIONS.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/ASSAY_RUNS.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/DATA_ENTRY_PAGES.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/DISCRETE_VALUES.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/ENTRY_GROUPS.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/GEMD_CODELIST_GROUPS.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/GEMD_CODELIST_VALUES.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/GEMD_LOT_DEFINITIONS.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/GEMD_SAMPLES.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/MOLECULAR_WAREHOUSE.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/QUESTION_DEFINITIONS.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/QUESTION_GROUPS.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/RESPONDENTS.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/RESPONSES.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_LIST.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_LIST_NAMES.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/SAMPLE_PLATE_ADDRESSES.csv'
>>> [1] 'w:/project/_Joe.B/Oracle/data/STORAGE_UNITS.csv'
>>> it seems correct. I copy and paste it into a code :
>>>   rr     <- read.csv( 'w:/project/_Joe.B/Oracle/data/RESPONDENTS.csv')
>>> and it works fine.
>>> Can someone help me debug where is the problem in my for loop code?
>>> Thanks,
>>> Kai
>>>
>>>
>>>
>>>
>>>
>>>          [[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> R-help using 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.
>>>
>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using 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.
> 
> ______________________________________________
> R-help using 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.
>



More information about the R-help mailing list