I am creating a filepath to read in R through a hard coded raw string (because otherwise the filepath raises an error):
filepath <- r"(C:\User\me\folder\data)"
However, I would like to create the following functionality to prevent the user from writing directly the filepath within the raw string:
filepath <- "C:\User\me\folder\data"
# I know the as_raw function doesn't exist, but I am wondering whether there is something similar
filepath <- as_raw(filepath)
Is there any R function that does this? Or should the user always modify the raw string (or use a readline function)?
CodePudding user response:
Any of these could be used:
filepath <- "C:/User/me/folder/data"
filepath <- "C:\\User\\me\\folder\\data"
filepath <- file.path("C:", "User", "me", "folder", "data")
filepath <- file.choose() # if file exists. Interactive.
filepath <- choose.dir() # same but for directory
filepath <- readClipboard() # if filepath is on clipboard. \ is ok.
filepath <- r"(C:\User\me\folder\data)"
CodePudding user response:
You could either ask the user to submit a file path with two forward slashes:
filepath <- "C://User//me//folder//data"
Or you can just ask for the part after the user name and then glue it together with the user path:
folder <- "folder//data"
filepath <- paste0("C://Users//", Sys.info()["user"], "//", folder)
CodePudding user response:
charToRaw(filepath)
Should do what you want, I think.