Meesha
Meesha

Reputation: 821

Reading a file on a network in 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?

Upvotes: 13

Views: 21393

Answers (4)

Elizabeth
Elizabeth

Reputation: 11

I had to do an additional step before any of the above options worked for me. I'm working from a Mac accessing a PC network attached storage which might (?) change the direction of slashes you need to put in.

  1. Find correct file path that R recognizes:

RStudio Tools--> Global Options Browse to folder in default working directory Copy this file path

  1. Replace each single slash in the file path with double slashes (as noted above)

  2. run read.csv function

Example: file path copied from file: S://servername/data/raw/test.csv file path copied from browsing to same file and what RStudio displays in working directory: /Volumes/data/raw/test.csv

read.csv("//Volumes//data//raw//test.csv")

Upvotes: 1

gregV
gregV

Reputation: 1107

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

Upvotes: 2

Ant
Ant

Reputation: 820

Using R's built-in file system functions:

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

Upvotes: 1

andyyy
andyyy

Reputation: 1015

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)

Upvotes: 21

Related Questions