0

I'm writing a code to read data from a directory. The files have different extensions such as xlsx, xls, CSV etc. I'm using if else conditions to read different formats. My snippet to read xlsx files is as follows:

inputPath = "C:/work1/Apple/Data/LA/Test/L3"
if(str_detect(string = inputPath, pattern = ".xlsx")) {
    dataInput = read.xlsx(xlsxFile = filePath,
                          sheet = 1)}

When I run this code, I get an error like this and the traceback is as follows:

 Error in file(description = xlsxFile) : invalid 'description' argument 
4.
file(description = xlsxFile) 
3.
getFile(xlsxFile) 
2.
read.xlsx.default(xlsxFile = inputPath, sheet = 1) 
1.
read.xlsx(xlsxFile = inputPath, sheet = 1) 

I have created this input path as follows:

identifier = "Test"
dataDirPath = file.path(getwd(), "Data/LA", identifier)
mode = "L3"
inputPath = list.files(file.path(dataDirPath, mode), full.names = TRUE)

inputPath contains this:

> inputPath
[1] "C:/work1/Apple/Data/LA/Test/L3/12303?????????(???????).xlsx"
[2] "C:/work1/Apple/Data/LA/Test/L3/12306?????????(???????).xlsx"
[3] "C:/work1/Apple/Data/LA/Test/L3/12309?????????(???????).xlsx"
[4] "C:/work1/Apple/Data/LA/Test/L3/12312?????????(???????).xlsx"
[5] "C:/work1/Apple/Data/LA/Test/L3/34503?????????(???????).xlsx"
[6] "C:/work1/Apple/Data/LA/Test/L3/34506?????????(???????).xlsx"
[7] "C:/work1/Apple/Data/LA/Test/L3/34509?????????(???????).xlsx"
[8] "C:/work1/Apple/Data/LA/Test/L3/34512?????????(???????).xlsx"
[9] "C:/work1/Apple/Data/LA/Test/L3/56703?????????(???????).xlsx"
> 

One thing is that the files in this directory contains japanese characters and some numbers. Here in the above inputPath output, '?' actually contains japanese characters. But code doesn't read it.

Note: I have set the locales to Japanese at the start itself.

For example,

1234林檎.xlsx

Is there anything wrong here?

M--
  • 25,431
  • 8
  • 61
  • 93
Luiy_coder
  • 321
  • 2
  • 11
  • The error complains that the `xlsxFile` value is invalid. Where's the loop? Does `filePath` contain one file or all files? – Panagiotis Kanavos Jul 08 '20 at 16:05
  • xlsxFile contains 9 files – Luiy_coder Jul 08 '20 at 16:08
  • `xlsxFile` expects the path to a single file. That's why this error is thrown – Panagiotis Kanavos Jul 08 '20 at 16:09
  • No it didn't! That question answers for a vector while in my case it isn't a vector. – Luiy_coder Jul 08 '20 at 16:11
  • Ok any fix for reading multiple files? – Luiy_coder Jul 08 '20 at 16:11
  • You already said that `filePath` *is* multiple files. So what does it contain? A single string? Multiple strings? Post the loop code, don't force people to guess – Panagiotis Kanavos Jul 08 '20 at 16:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217474/discussion-between-luiy-coder-and-panagiotis-kanavos). – Luiy_coder Jul 08 '20 at 16:40
  • First of all, you don't need and if-statement. `list.files` has a `pattern` argument. You can list the xlsx files with that like this: ```inputPath <- list.files(file.path(dataDirPath, mode), full.names = TRUE, pattern = "*.xlsx$")``` It list the files which end with that extension. Then, you need to feed the files one by one to `read.xlsx`. For that you can do this: ```lapply(inputPath, function(x) read.xlsx(xlsxFile = x, sheet = 1)``` – M-- Jul 09 '20 at 15:34
  • Besides that, I don't see where you set a value for `filePath`. You have `inputPath` and then out of nowhere, `filePath` appears in the code. Moreover, in your first code block, you have `inputPath` as a directory before your if-statement (not the files which you further below assigned using `list.files`. Share the exact code you're running, share it in the exact order you have it. As of now, your post is a bit unclear to me. – M-- Jul 09 '20 at 15:40
  • ```identifier <- "Test"; dataDirPath <- file.path(getwd(), "Data/LA", identifier); mode <- "L3"; inputPath <- list.files(file.path(dataDirPath, mode), full.names = TRUE, pattern = "*.xlsx$"); dataInput <- lapply(inputPath, function(p) read.xlsx(path=p, sheet = 1))``` – M-- Jul 09 '20 at 21:02

0 Answers0