Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rust Engine #1823

Closed
wants to merge 10 commits into from
Closed

Add Rust Engine #1823

wants to merge 10 commits into from

Conversation

TianyiShi2001
Copy link

@TianyiShi2001 TianyiShi2001 commented Mar 23, 2020

Adding support for running Rust code in ```{rust} and ```{cargo} chunks.

```{rust} chunks call rustc, while ```{cargo} chunks call cargo run. The main difference is that, ```{cargo} chunks allow using external crates, but this also comes at a cost of additional configuration (see below).

```{rust} implementation details:

This is achieved by copying code into a temporary file foo.rs and then run rustc foo.rs && ./foo.

You may need to specify the path to rustc in ~/.Renviron because Sys.getenv('PATH') in R is different from echo $PATH in shell.

Demo:

图片

You can even omit fn main() in simple cases:

图片

(this is achieved by first grepping 'fn main', and if it's not found, wrapping your code with fn main(){ and }

```{cargo} implementation details:

  1. the user sets options(knitr.cargo.path= "PATH_TO_CARGO"), where PATH_TO_CARGO is the directory that contains Cargo.toml Please suggest a better name for knitr.cargo.path!
  2. eng_rust_cargo() extracts the cargo directory using getOption("knitr.cargo.path")
  3. the content of each ```{cargo}block is copied to CARGO_DIR/src/main.rs (fn main() {} added if absent)
  4. setwd() to the cargo directory, and cargo run -q is executed by system2(), then wd is reset

Demo:

Screenshot 2020-04-05 at 16 18 03

Copy link
Owner

@yihui yihui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! It'll be great if you could use <- or = consistently (personally I prefer the latter). Thank you!

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@cderv cderv mentioned this pull request Jan 9, 2021
@cderv
Copy link
Collaborator

cderv commented Mar 6, 2021

FWIW I just saw there is a rust knitr engine in the R package extender

https://github.com/extendr/rextendr

Not yet on CRAN but part of a rust for R ecosystem. A good home for such an engine.

@yutannihilation
Copy link
Sponsor Collaborator

Feedback is welcome! R Markdown-related usages can be found here:

https://extendr.github.io/rextendr/articles/rmarkdown.html

@yihui
Copy link
Owner

yihui commented Apr 8, 2021

The extendr package looks much more promising than calling the command directly via system2() in R, so I'll close this PR and recommend users to try extendr instead. Thanks!

@yihui yihui closed this Apr 8, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 6, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants