vscode-R icon indicating copy to clipboard operation
vscode-R copied to clipboard

Run other languages in code blocks

Open jtrakk opened this issue 5 years ago • 12 comments

Sometimes I have multiple languages in the same notebook.

```{python}
# This is python code.
```

```{r}
# This is R code.
```

```{julia}
# This is julia code.
```

Is it possible to run julia's repl in {julia} blocks and python's repl in {python} blocks?

jtrakk avatar Feb 20 '21 22:02 jtrakk

There's a feature request on the VS Code backlog to make editorLangId recognise embedded languages: https://github.com/microsoft/vscode/issues/107989

In R Markdown files, those R/Python/Julia chunks are embedded languages. So if that VS Code feature gets implemented, this would work automatically: the editorLangId keybinding clause would use the language in the chunk, and the appropriate command to send the text to the appropriate REPL would be called.

andycraig avatar Feb 21 '21 02:02 andycraig

I need this feature badly. But I guess you don't need to request VS Code to recognize it, do you? I think the python codes can be called and executed from within R like what it actually does when you run knitr for the Rmd document. In this way, you can further access the python functions and variables from R. Can this be implemented here? I guess we just need the extension to automatically wrap up the python code whenever we click run code block. Ref: https://rstudio.github.io/reticulate/articles/r_markdown.html#engine-setup

MatrixRanger98 avatar May 14 '21 14:05 MatrixRanger98

Note that if you directly recognize those chunks as embedded languages from VS Code, you can't get the interactive features between R and other languages.

MatrixRanger98 avatar May 14 '21 14:05 MatrixRanger98

Another discussion on this: https://stackoverflow.com/questions/49503195/reticulate-running-python-chunks-in-rmarkdown

MatrixRanger98 avatar May 14 '21 14:05 MatrixRanger98

Hi @matrix-ranger, for now maybe you could use something like this? You would need a separate keybinding from Ctrl+Enter and would need to select the text to send, so it's a workaround rather than a full solution.

    {
        "key": "ctrl+shift+;",
        "command": "r.runCommandWithSelectionOrWord",
        "when": "editorTextFocus",
        "args": "py_run_string('$$')"
    }

andycraig avatar May 15 '21 02:05 andycraig

Thanks a lot. It works well except when there are already quote marks inside the code. Maybe a more robust way is to enter the reticulate python console inside R whenever a python chunk is executed.

reticulate::repl_python()

Then next time if you want to execute an R chunk, exit the python console first. It looks fine to manually do this but still I would really appreciated it if there could be support from the extension.

MatrixRanger98 avatar May 15 '21 03:05 MatrixRanger98

It would be great to recognize alternative knitr engines in addition to the usual languages. For example, the targets package defines a special language engine for Target Markdown: https://books.ropensci.org/targets/markdown.html.

wlandau avatar Jul 31 '21 15:07 wlandau

It would be great to recognize alternative knitr engines in addition to the usual languages. For example, the targets package defines a special language engine for Target Markdown: books.ropensci.org/targets/markdown.html.

Sorry if this is a dumb question, but is the interactive part of the "engine" defined by the engine spec, or is it something the IDE should handle? From the docs you linked, it looks like the engine defers to tar_option_get("envir") in the presence of the targets engine, but how does knitr or the IDE know this? My guess would be the former, but thought I would ask!

ElianHugh avatar Aug 01 '21 09:08 ElianHugh

There is an overlap between engine-aware chunk handling and vscode notebook.

renkun-ken avatar Aug 01 '21 10:08 renkun-ken

@ElianHugh, the workings of custom knitr engines are described at https://bookdown.org/yihui/rmarkdown-cookbook/custom-engine.html. A knitr engine is just an R function that controls how to run and display the code in the chunk. In the case of targets, the tar_engine_knitr() function (called from knitr::knit_engines$get("targets")) is responsible for what happens in both interactive and non-interactive modes, and tar_option_get("envir") is an R environment that targets uses to do its job regardless of whether R Markdown is involved.

My request for VSCode is just to put the "Run chunk" button right above {targets} code chunks (and any {xyz} code chunks) in addition to the {r} code chunks. knitr engines can handle the rest.

wlandau avatar Aug 01 '21 12:08 wlandau

@wlandau Thank you for the information!

At the moment, run chunk is fairly primitive - it just sends the chunk's text to the current active REPL. For processing knitr engines cleanly, I think there would be a couple steps required:

  1. Obtain a list of knitr engines via names(knitr::knit_engines$get())
  2. If the chunk is not an R-chunk, pass the chunk to an R process and invoke the relevant knit_engine method
  3. Update the list of knitr engines any time a custom engine is loaded (e.g., loading targets)

I think doing this would be easiest once we have a working 'back-end' for VSC-R, as renkun-ken points out.


Alternatively, a super rudimentary way of doing this would be to send the chunk to the REPL, and wrap the code in the knitr engine. I hsven't tested this, but theeoretically the following should work:

using Plots
x = 1:10; y = rand(10);
plot(x, y)

and running the chunk would call the following in the REPL:

# options would realistically be taken from the chunk
call_chunk <- function(code) {
	ops <- list(
      code = strsplit(code, "\n", fixed = TRUE)[[1]],
      eval = TRUE
    ) 
   knitr::knit_engines$get()[["julia"]](ops)
}

ElianHugh avatar Aug 03 '21 05:08 ElianHugh

This issue is stale because it has been open for 365 days with no activity.

github-actions[bot] avatar Aug 04 '22 02:08 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar Aug 18 '22 02:08 github-actions[bot]