12

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. enter image description here

Any help where I am missing?

Chankey Pathak
  • 21,187
  • 12
  • 85
  • 133
Suman Khanal
  • 3,079
  • 1
  • 17
  • 29

3 Answers3

13

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.

  • Any time! Don't forget to mention your tutorials on our IRC support channel so more people know about them :) –  Aug 24 '17 at 17:39
3

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'
))
```
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • 1
    I tried your first help. Its not working. This is the error I get : ```running: C:\rakudo\bin\perl6.exe -e "my $s= \"knitr is really good\"; say $s;" Quitting from lines 7-9 (ds.Rmd) Error in system2(cmd, code, stdout = TRUE, stderr = TRUE, env = options$engine.env) : '"C:\rakudo\bin\perl6.exe"' not found Calls: ... tryCatch -> tryCatchList -> tryCatchOne -> Execution halted ```. Even second solution is not working. What could have gone wrong? – Suman Khanal Aug 25 '17 at 08:39
  • If your perl6.exe is in your `PATH` variable, just set `engine.path="perl6"`; otherwise you need the actual path to perl6.exe, which you didn't say and I just inferred from your example. – Yihui Xie Aug 25 '17 at 14:34
  • Setting the `PATH` as `engine.path="perl6"` is still not giving the output, though it doesn't throw error. A little in depth look shows that there is `perl6.bat` in the Perl 6 `bin` folder. So is this `.bat` instead of `.exe` file the problem? – Suman Khanal Aug 25 '17 at 15:31
  • Probably not. `perl6.bat` should be fine. Can you check the output of `Sys.which('perl6')` and `Sys.getenv('PATH')`? – Yihui Xie Aug 25 '17 at 15:35
  • `Sys.which('perl6')` gives `"C:\\rakudo\\bin\\perl6.bat" ` and `Sys.getenv('PATH')` gives `"C:\\Program Files\\R\\R-3.4.1patched\\bin\\x64;c:\\Rtools\\bin;c:\\Rtools\\mingw_32\\bin;C:\\WINDOWS\\system32;C:\\WINDOWS\\System32\\WindowsPowerShell\\v1.0\\;C:\\curl\\bin;C:\\wget;C:\\Strawberry Perl\\perl\\bin;C:\\Program Files\\R\\R-3.4.1patched\\bin\\x64;C:\\Program Files\\Git\\cmd;C:\\texlive\\2017\\bin\\win32;C:\\Miniconda3\\Scripts;C:\\Miniconda3;C:\\rakudo\\bin;C:\\rakudo\\share\\perl6\\site\\bin;C:\\Program Files\\Docker Toolbox"` as outputs. I am still wondering !! – Suman Khanal Aug 25 '17 at 15:47
  • Perhaps `perl6` does not support the argument `-e`. – Yihui Xie Aug 25 '17 at 15:47
  • 1
    Perl 6 does support `-e` argument. What I found out is Perl 6 works with double quotes but not single quotes in Windows OS which I am running. So `perl6 -e "say 'Hello' "` works in Windows but `perl6 -e 'say "Hello" '` won't. – Suman Khanal Aug 25 '17 at 16:16
  • 1
    The code should be quoted in double quotes on Windows (see `shQuote`). I have run out of ideas. If you want to debug this issue by yourself, you may take a look at the function `eng_interpreted` in https://github.com/yihui/knitr/blob/master/R/engine.R – Yihui Xie Aug 26 '17 at 04:44
1

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.

Coke
  • 843
  • 4
  • 10