How do I replace "/" with "\" in r? - r

I am trying do that "C:/Users/Vitor/Documents" become "C:\Users\Vitor\Documents".
I tried :
gsub("//", "\", file)
paste(dirname(file),basename(file),sep="\")
normalizePath(file,"\",mustWork=FALSE)
But didn't work!

We can escape the \ with another set and use it in gsub as the \\ is just one character
gsub("/", "\\\\", "C:/Users/Vitor/Documents")
which would print correctly with cat
cat(gsub("/", "\\\\", "C:/Users/Vitor/Documents"))
#C:\Users\Vitor\Documents
and can check the number of characters
nchar("\\")
#[1] 1

Related

Run cmd command from within R

I want to trigger/run a certain cmd command from within R. I found the functions system, system2 and shell, but am a bit lost on how to exactly use them.
So if I would do it directly in Windows, I'd open the cmd and then run the following command:
"C:\Program Files\LatentGOLD6.0\lg60" "C:\Users\USER\myfile.lgs" /b /o "C:\Users\USER\myfile.html" /h
However, I struggle with how and where I'd specify such a command in R so that it runs the thing. All of the mentioend functions above require a character string, but since I need to pass the paths with quotes, I'm actually not sure how I can glue all of this together.
Any ideas?
So here's my current code:
program_path <- "C:\Program Files\LatentGOLD6.0\lg60"
lgs_path <- "C:\Users\USER\myfile.lgs"
out_path <- "C:\Users\USER\myfile.html"
batchline <- paste0(program_path, " ", lgs_path, " /b /o ", out_path, " /h")
system(batchline)
system2(batchline)
Alternative also doesn't work: batchline <- paste0("'", program_path, "'", " ", "'", lgs_path, "'", " /b /o ", "'", out_path, "'", " /h")
Use r"{...}" if there are backslashes within the literal string as we do here or else double each backslash (see ?Quotes) or in some cases using forward slash in place of backslash will work. Then use sprintf to generate batchline.
program_path <- r"{C:\Program Files\LatentGOLD6.0\lg60}"
lgs_path <- r"{C:\Users\USER\myfile.lgs}"
out_path <- r"{C:\Users\USER\myfile.html}"
batchline <- sprintf("%s %s /b /o %s /h", program_path, lgs_path, out_path)
or if you want to surround each path with quotes then replace last line with:
batchline <- sprintf('"%s" "%s" /b /o "%s" /h', program_path, lgs_path, out_path)
EDIT: updated bug where I initially used forward slashes.
ok, found a solution with the help of this here: Adding double quotes to string in R, i.e. wrapping the file paths into shQuote works.
program_path <- normalizePath("C:/Program Files/LatentGOLD6.0/lg60")
lgs_path <- normalizePath("C:/Users/USER/myfile.lgs")
out_path <- normalizePath("C:/Users/USER/myfile.html")
batchline <- paste0(shQuote(program_path),
" ",
shQuote(lgs_path),
" /b /o ",
shQuote(out_path),
" /h")
system(batchline)

Remove all punctuation except underline between characters in R with POSIX character class

I would like to use R to remove all underlines expect those between words. At the end the code removes underlines at the end or at the beginning of a word.
The result should be
'hello_world and hello_world'.
I want to use those pre-built classes. Right know I have learn to expect particular characters with following code but I don't know how to use the word boundary sequences.
test<-"hello_world and _hello_world_"
gsub("[^_[:^punct:]]", "", test, perl=T)
You can use
gsub("[^_[:^punct:]]|_+\\b|\\b_+", "", test, perl=TRUE)
See the regex demo
Details:
[^_[:^punct:]] - any punctuation except _
| - or
_+\b - one or more _ at the end of a word
| - or
\b_+ - one or more _ at the start of a word
One non-regex way is to split and use trimws by setting the whitespace argument to _, i.e.
paste(sapply(strsplit(test, ' '), function(i)trimws(i, whitespace = '_')), collapse = ' ')
#[1] "hello_world and hello_world"
We can remove all the underlying which has a word boundary on either of the end. We use positive lookahead and lookbehind regex to find such underlyings. To remove underlying at the start and end we use trimws.
test<-"hello_world and _hello_world_"
gsub("(?<=\\b)_|_(?=\\b)", "", trimws(test, whitespace = '_'), perl = TRUE)
#[1] "hello_world and hello_world"
You could use:
test <- "hello_world and _hello_world_"
output <- gsub("(?<![^\\W])_|_(?![^\\W])", "", test, perl=TRUE)
output
[1] "hello_world and hello_world"
Explanation of regex:
(?<![^\\W]) assert that what precedes is a non word character OR the start of the input
_ match an underscore to remove
| OR
_ match an underscore to remove, followed by
(?![^\\W]) assert that what follows is a non word character OR the end of the input

Replacing a special character does not work with gsub

I have a table with many strings that contain some weird characters that I'd like to replace with the "original" ones. Ä became ä, ö became ö, so I replace each ö with an ö in the text. It works, however, ß became à < U+009F> and I am unable to replace it...
# Works just fine:
gsub('ö', 'REPLACED', "Testing string ö")
# this does not work
gsub("Ã<U+009F>", "REPLACED", "Testing string Ã<U+009F> ")
# this does not work as well...
gsub("â<U+0080><U+0093>", "REPLACED", "Testing string â<U+0080><U+0093> ")
How do I tell R to replace These parts with some letter I want to insert?
As there are metacharacters (+ - to signify one or more), in order to evaluate it literally either escape (as #boski mentioned in the solution) or use fixed = TRUE
sub("Ã<U+009F>", "REPLACED", "Testing string Ã<U+009F> ", fixed = TRUE)
#[1] "Testing string REPLACED "
You have to escape the + symbol, as it is a regex command.
> gsub("Ã<U\\+009F>", "REPLACED", "Testing string Ã<U+009F> ")
[1] "Testing string REPLACED "
> gsub("â<U\\+0080><U\\+0093>", "REPLACED", "Testing string â<U+0080><U+0093> ")
[1] "Testing string REPLACED "

having R print a system call that contains "", '', and escape character \

I need to run a perl command from within an R script. I would normally do this via:
system(paste0('my command'))
However, the command I want to paste contains both single and double quotes and an escape character. Specifically, I would like to paste this command:
perl -pe '/^>/ ? print "\n" : chomp' in.fasta | tail -n +2 > out.fasta
I have tried escaping the double quotes with more escape characters, which allows me to pass the command, but it then prints all 3 escape characters, which causes the command to fail. Is there a good way around this, such that I can save the above perl line as a string in R, that I can then pass to the system() function?
Hey I haven't tested your particular perl call (since it involves particular file/directory etc) but tried something trivial by escaping the quotes and it seems to work. You might want to refer this question for more as well.
My approach,
# shouldnt have any text expect for an empty string
my_text <- try(system(" perl -e 'print \"\n\"' ", intern = TRUE))
my_text
[1] ""
# should contain the string - Hello perl from R!
my_text2 <- try(system(" perl -e 'print \"Hello perl from R!\"' ", intern = TRUE))
my_text2
[1] "Hello perl from R!"
So based on the above trials I think this should work for you -
try(system(command = "perl -pe '/^>/ ? print \"\n\" : chomp' in.fasta | tail -n +2 > out.fasta", intern = TRUE))
Note - intern = TRUE just captures the output as a character vector in R.

How to completely remove head and tail white spaces or punctuation characters?

I have string_a, such that
string_a <- " ,A thing, something, . ."
Using regex, how can I just retain "A thing, something"?
I have tried the following and got such output:
sub("[[:punct:]]$|^[[:punct:]]","", trimws(string_a))
[1] "A thing, something, . ."
We can use gsub to match one or more punctuation characters including spaces ([[:punct:] ] +) from the start (^) or | those characters until the end ($) of the string and replace it with blank ("")
gsub("^[[:punct:] ]+|[[:punct:] ]+$", "", string_a)
#[1] "A thing, something"
Note: sub will replace only a single instance
Or as #Cath mentioned [[:punct:] ] can be replaced with \\W

Resources