Adding a prompt while suppressing Julia output with JuliaCall and knitr in an .Rmd document

Viewed 87

When creating mixed-language .Rmd documents with knitr, I sometimes use a prompt to make it clear which language is being used in a particular chunk. I recently tried this with a julia language chunk using the JuliaCall package and noticed some strange behavior when I used a semicolon ; to suppress output.

Here's a minimal example:

---
output: html_document
---

```{r setup, echo = FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(prompt = TRUE)
```

```{r, echo = FALSE}
options(prompt = "julia> ")
```

```{julia}
x = 3;
3x
```

```{julia, prompt = FALSE}
x = 3;
3x
```

```{r, echo = FALSE}
options(prompt = "R> ")
```

```{r}
x <- 3
3 * x
```

The resulting output was as follows:

screenshot of knitr html output

In the first julia chunk, knitr seems to be interpreting the semicolon as R syntax: it adds a + to break the input line. In the second, when I remove the prompt, this is no longer the case. I'd like to obtain the same output as I get from the second chunk except with a julia> preceding each line of code. In other words, I'd like the same output as in my final R chunk except with julia replacing R. I'd very much appreciate any suggestions you may have.

Here's my session info for reference:

## ─ Session info ───────────────────────────────────────────────────────────────
##  setting  value                       
##  version  R version 4.0.5 (2021-03-31)
##  os       Ubuntu 20.04.2 LTS          
##  system   x86_64, linux-gnu           
##  ui       X11                         
##  language en_US:en                    
##  collate  en_US.UTF-8                 
##  ctype    en_US.UTF-8                 
##  tz       ---               
##  date     2021-04-20                  
## 
## ─ Packages ───────────────────────────────────────────────────────────────────
##  package     * version date       lib source        
##  bslib         0.2.4   2021-01-25 [1] CRAN (R 4.0.3)
##  cachem        1.0.4   2021-02-13 [1] CRAN (R 4.0.5)
##  callr         3.6.0   2021-03-28 [1] CRAN (R 4.0.5)
##  cli           2.4.0   2021-04-05 [1] CRAN (R 4.0.5)
##  crayon        1.4.1   2021-02-08 [1] CRAN (R 4.0.5)
##  desc          1.3.0   2021-03-05 [1] CRAN (R 4.0.5)
##  devtools      2.4.0   2021-04-07 [1] CRAN (R 4.0.5)
##  digest        0.6.27  2020-10-24 [1] CRAN (R 4.0.3)
##  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.3)
##  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.3)
##  fastmap       1.1.0   2021-01-25 [1] CRAN (R 4.0.3)
##  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.3)
##  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.3)
##  htmltools     0.5.1.1 2021-01-22 [1] CRAN (R 4.0.3)
##  jquerylib     0.1.3   2020-12-17 [1] CRAN (R 4.0.3)
##  jsonlite      1.7.2   2020-12-09 [1] CRAN (R 4.0.3)
##  JuliaCall     0.17.2  2021-01-04 [1] CRAN (R 4.0.5)
##  knitr         1.32    2021-04-14 [1] CRAN (R 4.0.5)
##  lifecycle     1.0.0   2021-02-15 [1] CRAN (R 4.0.5)
##  magrittr      2.0.1   2020-11-17 [1] CRAN (R 4.0.3)
##  memoise       2.0.0   2021-01-26 [1] CRAN (R 4.0.5)
##  pkgbuild      1.2.0   2020-12-15 [1] CRAN (R 4.0.3)
##  pkgload       1.2.1   2021-04-06 [1] CRAN (R 4.0.5)
##  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.3)
##  processx      3.5.1   2021-04-04 [1] CRAN (R 4.0.5)
##  ps            1.6.0   2021-02-28 [1] CRAN (R 4.0.5)
##  purrr         0.3.4   2020-04-17 [1] CRAN (R 4.0.3)
##  R6            2.5.0   2020-10-28 [1] CRAN (R 4.0.3)
##  Rcpp          1.0.6   2021-01-15 [1] CRAN (R 4.0.5)
##  remotes       2.3.0   2021-04-01 [1] CRAN (R 4.0.5)
##  rlang         0.4.10  2020-12-30 [1] CRAN (R 4.0.5)
##  rmarkdown     2.7     2021-02-19 [1] CRAN (R 4.0.5)
##  rprojroot     2.0.2   2020-11-15 [1] CRAN (R 4.0.3)
##  sass          0.3.1   2021-01-24 [1] CRAN (R 4.0.3)
##  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.3)
##  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.3)
##  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.3)
##  testthat      3.0.2   2021-02-14 [1] CRAN (R 4.0.5)
##  usethis       2.0.1   2021-02-10 [1] CRAN (R 4.0.5)
##  withr         2.4.2   2021-04-18 [1] CRAN (R 4.0.5)
##  xfun          0.22    2021-03-11 [1] CRAN (R 4.0.5)
##  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.3)
## 
## [1] /home/inhuretnakht/R/x86_64-pc-linux-gnu-library/4.0
## [2] /usr/local/lib/R/site-library
## [3] /usr/lib/R/site-library
## [4] /usr/lib/R/library
0 Answers
Related