Skip to main content
  1. About
  2. For Teams
Asked
Modified 3 days ago
Viewed 106 times
Part of R Language Collective
4

Im studying about data but there is a problem. There is a lot of data that I use to study but everytime I want to load the data with read_csv() I use a lot of time to change \ to /.

My files are locaten like this C:\\Users\\Me\\Desktop\\Data\\hotel_bookings.csv

So I can not do;

x <- "C:\Users\Me\Desktop\Data\hotel_bookings.csv"

Because my folder directory saves as \ not as \\

One of the solitions I found is

hotel <- read_csv(file.choose()) 

The other one is also

path <- "C:\\Users\\Me\\Desktop\\Data\\hotel_bookings.csv"
path <- normalizePath(path)
hotel <- read_csv(path)

but still takes time to insert an extra \.

Need an easier way to get my file location that contains all /

New contributor
Ozan Tivriz is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • 1
    Did stackoverflow.com/questions/17605563/… not answer ypur question not answer your question as suggested in stackoverflow.com/staging-ground/79786558 ?
    jps
    –  jps
    2025-10-10 08:29:17 +00:00
    Commented Oct 10 at 8:29
  • Using file.path would remove the need to type even a single \... and has the advantage of being robust to changes of operating system.
    Limey
    –  Limey
    2025-10-10 08:34:15 +00:00
    Commented Oct 10 at 8:34
  • 3
    Firstly, there’s no need to use normalizePath in your code. Secondly, you should never hard-code paths to absolute files in your code, it makes the code unusable by others (and also by future you, if your folder structure changes). Either use relative paths, or make the path a parameter (as you’ve done via file.choose(), but this particular method has the substantial disadvantage of requiring user interaction when running the analysis).
    Konrad Rudolph
    –  Konrad Rudolph
    2025-10-10 08:41:21 +00:00
    Commented Oct 10 at 8:41
  • 1
    @Limey I don’t understand how these questions are similar.
    Konrad Rudolph
    –  Konrad Rudolph
    2025-10-10 08:43:37 +00:00
    Commented Oct 10 at 8:43

3 Answers 3

4

Since R 4.0.0, you can use raw character constants (mentioned on r-devel NEWS):

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.

x <- r"(C:\Users\Me\Desktop\Data\hotel_bookings.csv)"
Sign up to request clarification or add additional context in comments.

10 Comments

This is true, but it doesn’t really matter since the underlying issue in OP’s code isn’t the need to escape backslashes, it’s the presence of hard-coded absolute paths.
Fundamentally agree, but I get the impression though that OP is just writing some one-off scripts as a learning exercise.
@KonradRudolph, I don't see any mention in the OP to a problem relating to abs-vs-rel paths. We don't know the working directory of the R process. While there are certainly best-practices when working with projects within R, that is not the only way to do things, and it is not the best way for 100% of coding out there (I'll demonstrate my use-cases if needed, I cannot use "projects"). Bottom line, while I agree that the OP may not need abs paths, it is not certain that that is what is actually causing a problem.
I’m confused. Are you suggesting it’s ever an OK practice to use absolute paths to data files (excluding OS-specific configuration files that are in a fixed location)? Because if you are, I’d be very surprised and you’d be dead wrong. It might not be causing OP problems right now but I’m sure you know that this bad practice is causing problems in general all the time, and that we should explicitly discourage it especially for beginners, who don’t know better. — I’m confused we’re even having a debate.
@KonradRudolph, your argument "the underlying issue ... hard-coded absolute paths" is categorically wrong: if the OP had started with rel-paths, the question would be just as relevant, and while there would be fewer of them, there remain (back)slashes to work around, and that is the core of the question. You arguing that it must be rel-paths is an X/Y point of its own: you are solving a problem the OP did not request, and resolving your preference will change nothing in the original problem.
If OP had started with relative paths, they probably wouldn’t have copied-and-pasted them into the code so they wouldn’t need to replace a bunch of backslashes, they’d just write them properly directly. — This exact problem is a typical problem when beginners copy-and-paste paths from the explorer into their code. That’s why this is (probably) the underlying issue. I might be wrong, and this isn’t what OP is doing, but you’d be surprised how often this is exactly what happens.
I am very gratefull. It solved my problem and I'll also learn not to use absolute path. Thank you both
@KonradRudolph BTW, I'm not "dead wrong": you don't know my data/code structure, my op-temp, or the more-than 400 pages I generate every weekend from several disparate data sources. If you don't understand, that's fine, you don't know the environment I work in. Please keep an open mind, I'm a hammer that recognizes not everything is a nail.
… Now you just seem to be arguing for argument’s sake.
I'm sorry if it's coming across that way, I'm not angry nor do I feel defensive (other than perhaps "dead wrong" ;-). You're right, 99% or more of the time rel is better, definitely. In this case, that is certainly a best-practice that we feel the OP should adopt, we agree. But even as the OP copies from somewhere and pastes into R, it seems likely that the .R script is not within the data directory, which means that there will still be the need to change the (back)slashes in the pasted text.
3

Use forward-slashes instead.

On macos and linux, R uses forward-slashes only for directory separators. On windows, however, it accepts either, so all of the following will work on windows:

path <- "C:\\Users\\Me\\Desktop\\Data\\hotel_bookings.csv"
path <- "C:/Users/Me/Desktop/Data/hotel_bookings.csv"
# raw-strings, from Robby's answer
path <- r"{C:\Users\Me\Desktop\Data\hotel_bookings.csv}"

Personally I prefer using forward-slashes everywhere, first for cross-platform compatibility (backslashes don't work on non-windows platforms afaict), second because I've been in non-windows more than windows in my career so I'm more comfortable with forward-slashes.

Side note: I tend to agree that most of the time one should avoid absolute paths when working on projects. I'm assuming for this answer that there is (or can be) reason for using a full path. However, this answer is applicable to relative paths as well, such as

relpath <- "data\\hotel_bookings.csv"
relpath <- "data/hotel_bookings.csv"
relpath <- r"{data\hotel_bookings.csv}"

though clearly the number of slashes typed is far fewer.


On rereading the question a few more times, I see the utility of Rui's answer that suggests a programmatic way to change the text of what you're pasting. I'll add to that view a slightly different code-snippet that may be useful.

slashes <- function(txt) {
  if (missing(txt)) txt <- clipr::read_clip()
  clipr::write_clip(gsub("\\\\{1,2}", "/", txt))
}
# copy C:\\Users\\Me\\Desktop\\Data\\hotel_bookings.csv to your clipboard
slashes()
# you now have C:/Users/Me/Desktop/Data/hotel_bookings.csv
# in your clipboard, paste whereever

This is slightly different from Rui's in that it will replace single or up-to-double backslashes by a single forward slash.

Comments

1

Though there already is an accepted answer and this one is much closer to the question in user Abdul Aziz's comment, this method to convert backslashes into forward slashes was not posted as an answer to that linked to question.

path <- "C:\\Users\\Me\\Desktop\\Data\\hotel_bookings.csv"

chartr("\\", "/", path)
#> [1] "C:/Users/Me/Desktop/Data/hotel_bookings.csv"

Created on 2025-10-10 with reprex v2.1.1


The OP says that they spend

a lot of time to change \ to /.

The solution above is also fast, around twice as fast as gsub.

path <- "C:\\Users\\Me\\Desktop\\Data\\hotel_bookings.csv"

bench::mark(
  chartr = chartr("\\", "/", path),
  gsub = gsub("\\\\", "/", path)
)
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 chartr        2.4µs    2.6µs   312210.        0B        0
#> 2 gsub          5.1µs    5.7µs   167013.        0B        0

Created on 2025-10-10 with reprex v2.1.1

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.