在Rmarkdown中执行Perl 6代码 [英] Executing Perl 6 code in Rmarkdown

查看:111
本文介绍了在Rmarkdown中执行Perl 6代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Perl 6上写一些教程.为此,我相信Rmarkdown会很有帮助.

I want to write some tutorials on Perl 6. For this I believe Rmarkdown would be of great help.

所以我试图在Rmarkdown文档中执行Perl 6代码.

So I am trying to execute Perl 6 code within Rmarkdown document.

我的Perl 6可执行文件在C:\rakudo\bin中. 因此,我的.Rmd文件以及用于完成此操作的示例代码如下:

My Perl 6 executable is in C:\rakudo\bin. So my .Rmd file with example code to accomplish this is as follow:

---
title: "Example"
output: html_document
---

```{r, engine='perl6', engine.path='C:\\rakudo\\bin'}
my $s= "knitr is really good";
say $s;
```

但是,在Rstudio中编织以上文档时,显示以下内容而没有Perl 6输出.

However knitting the above document in Rstudio shows the following without Perl 6 output.

我想念的地方有帮助吗?

Any help where I am missing?

推荐答案

不是我的专业领域,而是具有

Not my area of expertise, but with a help of a blog I managed to get it to produce output.

首先,查看RStudio的R Markdown选项卡.它向您显示一条警告,解释为什么您的版本未呈现任何内容:

First, look in RStudio's R Markdown tab. It shows you a warning that explains why your version isn't rendering anything:

Warning message:
In get_engine(options$engine) :
  Unknown language engine 'perl6' (must be registered via knit_engines$set()).

因此请记住这一点,我们可以查找如何注册引擎并进行注册:

So with that in mind, we can look up how to register an engine and do so:

```{r setup, echo=FALSE}
library(knitr)
eng_perl6 <- function(options) {
  # create a temporary file
  f <- basename(tempfile("perl6", '.', paste('.', "perl6", sep = '')))
  on.exit(unlink(f)) # cleanup temp file on function exit
  writeLines(options$code, f)
  out <- ''

  # if eval != FALSE compile/run the code, preserving output
  if (options$eval) {
    out <- system(sprintf('perl6 %s', paste(f, options$engine.opts)), intern=TRUE)
  }

  # spit back stuff to the user
  engine_output(options, options$code, out)
}

knitr::knit_engines$set(perl6=eng_perl6)
```

```{r, engine='perl6'}
my $s= "knitr is really good";
say $s;
```

引擎注册有一个函数,该函数首先将要运行的代码保存到一个临时文件中,然后执行Rakudo编译器,要求它编译该文件.

The engine is registered with a function that first saves the code to run to a temporary file and then executes the Rakudo compiler, asking it to compile that file.

在收集了所需的输出后,该函数删除了临时文件,并提供了用于渲染的输出.

After collecting the needed output, the function deletes the temporary file and gives us the output for rendering.

这篇关于在Rmarkdown中执行Perl 6代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆