Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a script file for another programming language from within R (writing a long text with many special characters to a file)

Tags:

r

I have an R script that makes a Bash call to execute a perl script. However, I need to fit everything into a single .R file. Hence, I want to add the Perl script into the body of my R code, so that the .pl file will be generated from within R.

I have so far tried to do this with writeLines but this seems to be very tricky as I cannot simply paste the entire script into one object and have to break it into pieces with paste. This is mainly caused by the fact that my Perl script contains many " and ' characters that confuses R.

Is there an easy way to achieve this with the least amount of usage of paste?

like image 864
nelinora Avatar asked Oct 15 '25 09:10

nelinora


1 Answers

I think the cat() function could be of help. If I understand correctly what you want is a R script that will generate a perl script. If so, what you can do is use cat like so:

cat("piece of script", file = "path/to/script.pl")

use append=T to keep editing the script:

cat("piece of script", file = "path/tp/script.pl", append =T)

Then call the script with system() (I guess you already know that):

system("perl path/to/script.pl")

Regarding the quotation marks, I guess you have to deal with them properly in your R script, i.e:

cat("`piece of script`", file = "path/tp/script.pl", append =T)

for single quotation marks and

 cat("\"piece of script\"", file = "path/tp/script.pl", append =T)

for double.

Hope this helps.

like image 92
Kevin Cazelles Avatar answered Oct 18 '25 10:10

Kevin Cazelles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!