1

When I want to load a file into R, what would be a way to do so programmatically (or semi programmatically) instead of just manually copy paste the path and switch \ to / so R could read it (because \ is an escape character in R)

One method mentioned by @Roland is using scan

list.files(scan(file=file(description = "clipboard"), what = "character", allowEscapes = FALSE))

Which will need modifications if the path contains a space like

"C:\Users\David Arenburg\"

Cœur
  • 37,241
  • 25
  • 195
  • 267
David Arenburg
  • 91,361
  • 17
  • 137
  • 196

2 Answers2

3

normalizepath maybe what you are looking for:

> normalizePath(c(R.home(), tempdir()))
[1] "C:\\Program Files\\R\\R-3.1.0"                    
[2] "C:\\Users\\john\\AppData\\Local\\Temp\\RtmpysKuFi"
> normalizePath(c(R.home(), tempdir()), winslash = '/')
[1] "C:/Program Files/R/R-3.1.0"                 
[2] "C:/Users/john/AppData/Local/Temp/RtmpysKuFi"

Use readClipboard if you are on windows and have problems copying and pasting filepaths. For example I copy a file path from a windows folder so it is on the clip board then I can use:

> readClipboard()
[1] "C:\\Users\\john\\AppData\\Local\\Temp"

You can then use `normalizePath to correct the windows flavour of paths.

jdharrison
  • 30,085
  • 4
  • 77
  • 89
0

Perhaps use choose.files() to pick the file followed by normalizePath() if you want to convert its path name to R's forward slashes.

lawyeR
  • 7,488
  • 5
  • 33
  • 63