Reputation: 89
Im trying to read a directory from a txt-file. In the txt file the syntax is like follows:
dirScript;"C:/User/Folder_1/R/Script-Folder 1/"
"empty line"
The information that I want is to get var equal to a string with the directory like this:
var <- "C:/User/Folder_1/R/Script-Folder 1/"
setwd(var)
And my R code looks like this:
tempString <- str_c(str_extract_all(textInTxtFile, regex("(?<=;).*"), simplify=TRUE), sep="", collapse="")
# Real variable, gsub expression deletes extra backslashes added by functions above
var <- gsub("[^A-Za-z0-9/.:-_; ]", "", tempString)
# Tempstring: "\""C:/User/Folder 1/R/Script-Folder 1/"\""
# Var: "C:/User/Folder 1/R/ScriptFolder 1/"
# *UPDATE* Or like this, seems to work, Safe enough?
var <- gsub('"', "", tempString)
So my "-" disappears and also the "_". I don't understand why, I think that my regex should search anything after a semicolon ";", is this wrong? Maybe I shouldn't use str_c ? (but does it make a difference?)
Also how do i fix my gsub to not take away "-" and (?) "_"? Or how fix my regex so that gsub isn't necessary, my regex or other functions there add some backslashes and citation-signs.
And lastly is there a way to check my directory after I've found my correct directory?
Is this a good idea to use?
dir.create(file.path(mainDir, subDir), showWarnings = FALSE)
setwd(file.path(mainDir, subDir))
from: Check existence of directory and create if doesn't exist
Extra question: Is there a way to easy find if the direction is written with backslashes (windows standard), if so I would just use gsub("[\]", "/", text). I was thinking of just searching for a number of backslashes and if its bigger than say 3 I use this, but it's not too safe. Aslo it will complain in the regex I think since R will interpret backslash as escape character.
So the "extra question" is, how find and fix this to a good path:
dirScript;"C:\User\Folder_1\R\Script-Folder 1\"
"empty line"
Upvotes: 1
Views: 567
Reputation: 89
I fixed it. Thanks to the helped I received.
I think my main problem was in the gsub-function. It works with this now:
var <- gsub('"', "", tempString)
Also my regex-expression with the str_c and all might add some weird stuff that give me an extra-string, but thats deleted by the new gsub.
Also, I tried it with a path with backslashes "\" and it works.
I don't know if leave this question up, maybe not? Comment what you think.
Thanks for the help.
Upvotes: 0