Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: '\R' is an unrecognized escape in character string starting "C:\R"

Tags:

r

I am running Windows XP Pro and R Version 2.15.1 R is installed in the following folder:

C:\Program Files\R\R-2.15.1

I am trying to create a function that reads in a .csv file like so:

xxx <- function(id, directory, summarize = FALSE) {
    data <- read.table('C:\xxx\classes\R_Prog\specdata\data.csv')
    head(data)
}

I get the error

Error: '\R' is an unrecognized escape in character string starting "C:\R"

Is there a problem with my directory structure / folder naming conventions?

like image 477
dorkboy Avatar asked Oct 02 '12 18:10

dorkboy


People also ask

Is an unrecognized escape in character string r?

Unfortunately, the RStudio console returns the Error: 'R' is an unrecognized escape in character string starting “”C:R”. The reason for this is that the backslash that we have used in the path is an escape character, and we need to specify that somehow in R.

What is r escape character?

An escape character in R is used whenever you are trying to add an illegal character or a character that would otherwise be difficult to add to a string. Some of the escape characters in R are: Escape Code. Result. \"

How do you escape special characters in R?

R treats backslashes as escape values for character constants.


3 Answers

You have to escape the \ since it is itself an escape character.

read.table('C:\\xxx\\classes\\R_Prog\\specdata\\data.csv') head(data) }

like image 76
Jordan Kaye Avatar answered Oct 23 '22 19:10

Jordan Kaye


As nobody suggested a forward slash yet, allow me to do so:

R> list.files("C:/opt", pattern="R")
[1] "R-current"  "R-library"  "R-local215" "RStudio"    "Rtools"    
R> 

I find forward slashes "easier on the eye" as it makes paths more consistent across OSs, and you do not need to escape them either. Which means you save a whole byte each time. Yippie.

like image 20
Dirk Eddelbuettel Avatar answered Oct 23 '22 19:10

Dirk Eddelbuettel


Noone has suggested file.path yet. This will concatenate a string together to form a file path using a platform specific separator ( default is / on windows)

file.path('c:', 'xxx', 'classes', 'R_prog','specdata', 'data.csv')
## [1] "c:/xxx/classes/R_prog/specdata/data.csv"
like image 8
mnel Avatar answered Oct 23 '22 19:10

mnel