Raw text strings for file paths in R
It is now possible with R version 4.0.0. See ?Quotes
for more.
Example
r"(c:\Program files\R)"
## "c:\\Program files\\R"
Escaping backslash (\) in string or paths in R
From R 4.0.0 you can use r"(...)"
to write a path as raw string constant, which avoids the need for escaping:
r"(E:\RStuff\test.r)"
# [1] "E:\\RStuff\\test.r"
There is a new syntax for specifying raw character constants similar to the one used in C++:
r"(...)"
with...
any character sequence not containing the sequence)"
. This makes it easier to write strings that contain backslashes or both single and double quotes. For more details see?Quotes
.
Import text file as single character string
Here's a variant of the solution from @JoshuaUlrich that uses the correct size instead of a hard-coded size:
fileName <- 'foo.txt'
readChar(fileName, file.info(fileName)$size)
Note that readChar allocates space for the number of bytes you specify, so readChar(fileName, .Machine$integer.max)
does not work well...
How to pass a chr variable into r (...) ?
I think you're getting confused about what the string literal syntax does. It just says "don't try to escape any of the following characters". For external inputs like text input or files, none of this matters.
For example, if you run this code
path <- readline("> enter path: ")
You will get this prompt:
> enter path:
and if you type in your (unescaped) path:
> enter path: C:\Windows\Dir
You get no error, and your variable is stored appropriately:
path
#> [1] "C:\\Windows\\Dir"
This is not in any special format that R uses, it is plain text. The backslashes are printed in this way to avoid ambiguity but they are "really" just single backslashes, as you can see by doing
cat(path)
#> C:\Windows\Dir
The string literal syntax is only useful for shortening what you need to type. There would be no point in trying to get it to do anything else, and we need to remember that it is a feature of the R interpreter - it is not a function nor is there any way to get R to use the string literal syntax dynamically in the way you are attempting. Even if you could, it would be a long way for a shortcut.
How to include a variable in 'r' raw string
As mentioned in the comments, r
is a literal prefix which you cannot apply to anything but string literals, so path + r'/crif/...'
is enough. However, in this particular case when you need to compose a file path, I'd use the standard library, which makes the code more portable:
import os
path = os.getcwd()
crif0 = os.path.join(path, 'crif', 'gpio_mem_0_crif.xml')
or, in a more modern way using path objects rather than strings:
from pathlib import Path
crif0 = Path.cwd() / 'crif' / 'gpio_mem0_crif.xml'
Related Topics
Can the Value.Var in Dcast Be a List or Have Multiple Value Variables
Output in R, Avoid Writing "[1]"
Plot Data Over Background Image with Ggplot
Mutate Multiple Variable to Create Multiple New Variables
Image Not Showing in Shiny App R
Ggplot Custom Scale Transformation with Custom Ticks
What Does the R Function 'Poly' Really Do
Calculating Number of Days Between 2 Columns of Dates in Data Frame
No Visible Global Function Definition for 'Median'
Raw Text Strings for File Paths in R
Creating (And Accessing) a Sparse Matrix with Na Default Entries
How to Get the Number of Rows in a CSV File Without Opening It
Convert 12 Hour Character Time to 24 Hour
The Simplest Way to Convert a List with Various Length Vectors to a Data.Frame in R