问题
I want to write some tutorials on Perl 6. For this I believe Rmarkdown would be of great help.
So I am trying to execute Perl 6
code within Rmarkdown document.
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;
```
However knitting the above document in Rstudio shows the following without Perl 6 output.
Any help where I am missing?
回答1:
Not my area of expertise, but with a help of a blog I managed to get it to produce output.
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;
```
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.
回答2:
You had two problems in your example. First, I think you can still use the existing perl
engine (perl6
is not a valid engine name). Second, the engine.path option should point to the path of the executable instead of the directory name, e.g.
---
title: "Example"
output: html_document
---
```{perl, engine.path='C:\\rakudo\\bin\\perl6.exe'}
my $s= "knitr is really good";
say $s;
```
You can also set the engine path globally for the perl
engine:
```{r, setup, include=FALSE}
knitr::opts_chunk$set(engine.path = list(
perl = 'C:\\rakudo\\bin\\perl6.exe'
))
```
回答3:
From the command prompt on windows, this works:
perl6 -e "say 'hello'"
but this fails:
perl6 -e 'say "hello"'
You have to use double quotes to quote arguments in the command prompt.
来源:https://stackoverflow.com/questions/45857934/executing-perl-6-code-in-rmarkdown