12

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?

Meesha
  • 801
  • 1
  • 9
  • 27

3 Answers3

20

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)
andyyy
  • 986
  • 8
  • 8
2

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 (\\).

gregV
  • 987
  • 9
  • 28
1

Using R's built-in file system functions:

CSVfile <- file.path('\\\\shared', 'data', 'abc.csv')
read.csv(CSVfile, header=T)`
Ant
  • 790
  • 7
  • 18