Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a file on a network in R

Tags:

path

r

I am importing a csv file in to R using the read.csv method but get the following error.

The network path is "\\shared\data\abc.csv"

read.csv("\\shared/data/abc.csv",header=T)
                    or 
read.csv("\\shared\\data\\abc.csv",header=T)

If I use copy paste the address in the address bar in the file explorer, it opens the file but R somehow cannot read it. What's the reason? Is it because the network name starts with "//" instead of traditional drive name like C,D etc?

like image 353
Meesha Avatar asked Sep 02 '16 15:09

Meesha


People also ask

How do I find a file path in R?

If we want to check the current directory of the R script, we can use getwd( ) function. For getwd( ), no need to pass any parameters. If we run this function we will get the current working directory or current path of the R script.


2 Answers

You need to escape each backslash, so for the double backslash you need four backslashes, i.e.

read.csv("\\\\shared\\data\\abc.csv",header=T)
like image 62
andyyy Avatar answered Sep 21 '22 02:09

andyyy


in addition, the below also works and should be OS agnostic:

read.csv("//shared/data/abc.csv",header=T)

when running getwd() note how the separator between folders is forward slash (/), as it is on Linux and Mac systems. If you use the Windows operating system, the forward slash will look odd, because you’re familiar with the backslash (\) of Windows folders. When working in Windows, you need to either use the forward slash or escape your backslashes using a double backslash (\\).

like image 29
fkPtn Avatar answered Sep 23 '22 02:09

fkPtn