0

I'm trying to take an input path and convert the slashes from backwards to forwards as part of an R package. There are some suggestions to do this here and elsewhere, but all of these solutions require some amount of interaction by the user: either copying a path, or selecting a path from a prompt. I would be happy if I could find a solution that works on strings, but when I try to use strings with slashes in R, I run into problems.

# example path
path <- "C:\aaa\bbb\fff\"\n" # I have to add this \n linebreak for R to save the object without an error
# replace slashes in string
gsub(pattern="\"", replacement="/", x=path) # I have to put an extra quote in the pattern to avoid error, but this makes it not work. 

Note that to avoid errors in this example, I had to save the path with a linebreak and there is an extra quote in the pattern for gsub. This is only a problem on Windows computers, but I need my package to work on Windows. Thanks in advance for suggestions.

mikey
  • 1,066
  • 9
  • 17
  • 1
    Where are you getting the input path from? The `path` example you have above isn't a real path: the `\a` and `\b` and `\f` all represent special escape characters and can't exist in an actual path. Where will your package be getting the path from? If it is from user input, then `scan` has an option to automatically escape backslashes. If it is from an R function, then all file path backslashes are already escaped. I use windows, and if I call `Sys.getenv("PATH")` then the backslashes are all escaped. If I want to convert a Windows path I just do `gsub('\\', '/', path)` and it works just fine – Allan Cameron Feb 03 '20 at 14:58
  • @AllanCameron Thanks. I'm getting the input path from user input in the `shinyFiles` package, so it's not something I can easily manipulate. When users do this on Windows, they get a path that looks like `C:\documents\name\...` I want to be able to take this string and convert it to `C:/documents/name/...`. – mikey Feb 20 '20 at 18:06

0 Answers0