Obtaining filepaths for files

I am currently working on a shiny app where I have created a fileInput button through which the user can browse and select a zip file and the file will get unzipped and stored in the chosen directory as specified through the argument 'exdir' in the function unzip().

The next step involves accessing the filepath for a csv file inside this unzipped file. The structure of the unzipped file is somewhat of a folder within a folder which is again inside a folder.
The conventional way of writing the filepath for this csv will be something like "C:\x\y\z\file.csv" where z contains the csv file and is within y which is within another folder x. But since it's a shiny app deployed on cloud, users might use various zip files although containing the same number of folders but different folder names other than a fixed "x","y","z " but the said zip file will always contain the csv file 3 folders deep. Hence it's possible to mention the path in terms of some x,y or z for general purpose.

I am looking to access this csv file which will have the same name for all users (file.csv) with only the preceding folder names being different each time. Is there any other way to get this filepath than manually defining it everytime as written above?

Hi, you can search the "file.csv" since you know its name, using the "dir" command with recursive=T. And it should work even if the number of folders is not 3.

dir(selected_folder, pattern = "file.csv", recursive = T)

you need to carefully set selected_folder to the folder of the uploaded zip (exdir). Otherwise, if you use the root folder, it will return all "file.csv" files in all the subfolders.

1 Like

Hi, I don't know exactly what's the reason of this problem, but you can verify that the zipfile exists and the path is correct.

I am being able to extract them to other directories but not a temporary directory

What OS you're using? and what's the output of tempdir() ?
maybe the tempdir isn't writable, if this is the case, you should change it or allow permission to the R user to write on that folder or run R/RStudio as Administrator/root.

Thanks for the insight. This is really helpful.
Can you also help me out with an error?
While unzipping a zipped file in a temporary file location I am getting the following warning/error :

error -1 in extracting from zip file

Any idea how to solve this?

The zip file does exist and the path is correct

Can you provide more details about it:

  • Does it happen only for a specific zip file?
  • if not, are you able to extract the other zip files to that directory?
  • if it happens to all files, are you able to extract them to an other directory?