从R中为另一种编程语言创建一个脚本文件(在文件中编写一个具有许多特殊字符的长文本)

时间:2022-02-12 15:26:51

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.

我有一个R脚本,它调用Bash来执行perl脚本。但是,我需要将所有内容都放入一个. r文件中。因此,我想将Perl脚本添加到R代码的主体中,这样.pl文件就会从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.

到目前为止,我已经尝试过使用writeLines来实现这一点,但这似乎非常棘手,因为我不能简单地将整个脚本粘贴到一个对象中,并且必须用粘贴将其分解成多个部分。这主要是由于我的Perl脚本包含了许多“和”字符,这些字符混淆了R。

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

是否有一种简单的方法用最少的浆糊来达到这个目的?

1 个解决方案

#1


3  

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()功能可能有帮助。如果我正确理解了您想要的是一个可以生成perl脚本的R脚本。如果是的话,你可以这样使用cat:

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

use append=T to keep editing the script:

使用append=T来继续编辑脚本:

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

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

然后用system()调用脚本(我想你已经知道了):

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:

关于引号,我想您必须在R脚本中正确地处理它们,即:

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.

希望这个有帮助。

#1


3  

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()功能可能有帮助。如果我正确理解了您想要的是一个可以生成perl脚本的R脚本。如果是的话,你可以这样使用cat:

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

use append=T to keep editing the script:

使用append=T来继续编辑脚本:

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

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

然后用system()调用脚本(我想你已经知道了):

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:

关于引号,我想您必须在R脚本中正确地处理它们,即:

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.

希望这个有帮助。