extendr / rextendr

An R package that helps scaffolding extendr-enabled packages or compiling Rust code dynamically
https://extendr.github.io/rextendr/
Other
184 stars 27 forks source link

Examples not working in MacOS #151

Closed dkgaraujo closed 2 years ago

dkgaraujo commented 2 years ago

Hi all, first off thanks for binding these two very complementary languages, I'm looking forward to harnessing their respective powers together. But unfortunately so far I haven't been able to make it work even with simple examples. I already had well-functioning R (4.0.2) and rust (1.55.0) installations.

Here's the sequential steps I undertook, all successful, and with no hint of errors or incomplete installations:

1) in R: install.packages("rextendr") 2) in the terminal: git clone https://www.github.com/extendr/libR-sys then, in the libR-sys folder, cargo build. (It did return warnings, but no errors; the warnings were about the unsafe part of the code.)

But, here are the results of the basic examples provided in the extendr page:

'''{extendr}
rprintln!("Hello from Rust!");

let x = 5;
let y = 7;
let z = x*y;

z
'''

Warning message:
In get_engine(options$engine) :
  Unknown language engine 'extendr' (must be registered via knit_engines$set()).

(The output slide was created but that code chunk above didn't produced any output whereas other chunks did as expected. I didn't want to register in knit_engines without first knowing what was going on.)

> rust_function("fn add(a:f64, b:f64) -> f64 { a + b }")

ℹ build directory: /var/folders/b9/p8z57lqd55xfk68xz34dg0s40000gn/T//RtmpaMAFZh/filef3b4ac06619
sh: cargo: command not found
Error: Rust code could not be compiled successfully. Aborting.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning message:
In system2(command = "cargo", args = c(sprintf("+%s", toolchain),  :
  error in running command

Any pointer into how to solve the above is greatly appreciated! Thanks very much in advance!

yutannihilation commented 2 years ago

Thanks for reporting! I think I've seen the same error of "Unknown language engine 'extendr'" on my Linux, but didn't have time to file an issue at that time (sorry).

Regarding this error,

sh: cargo: command not found

this indicates you need to add ~/.cargo/bin to your PATH. Typically, including PATH="~/.cargo/bin:${PATH}" in your ~/.Renviron will fix this. (See https://github.com/extendr/libR-sys/issues/54 for the related discussions. I don't know why this tends to happen on macOS...)

yutannihilation commented 2 years ago

@clauswilke Do extendr chunks work on your environment? I'm suspecting something changed on knitr's side after we implemented extendr engine.

dkgaraujo commented 2 years ago

Thanks for reporting! I think I've seen the same error of "Unknown language engine 'extendr'" on my Linux, but didn't have time to file an issue at that time (sorry).

Regarding this error,

sh: cargo: command not found

this indicates you need to add ~/.cargo/bin to your PATH. Typically, including PATH="~/.cargo/bin:${PATH}" in your ~/.Renviron will fix this. (See extendr/libR-sys#54 for the related discussions. I don't know why this tends to happen on macOS...)

Many thanks @yutannihilation! The code rust_function() code now works, although the code chunk in knitr is still not working successfully (same msg as above).

clauswilke commented 2 years ago

Before making any comments, I'd like to see a fully reproducible example. The {rextendr} package needs to be loaded before the first extendr chunk. If that isn't done I'd expect knitr to fail.

clauswilke commented 2 years ago

Our regression suite still runs without problems, so knitr chunks work as far as I can tell. https://github.com/extendr/rextendr/runs/3984897049?check_suite_focus=true

dkgaraujo commented 2 years ago

Thanks very much @clauswilke for jumping in and @yutannihilation again for the helpful pointers. As I was preparing a reprex of the knittr situation, I noted it did work now. Maybe it somehow needed me to close the R session to reflect the addition of ~/.cargo/bin to PATH, I don' know. It is working perfectly now. Thanks again!

Separately, do you think it would be necessary to slightly adjust the installation instructions for macOS users that they might need to add ~/.cargo/bin to PATH?

yutannihilation commented 2 years ago

Our regression suite still runs without problems, so knitr chunks work as far as I can tell.

Oh, true, thanks. I think I struggled to make extendr chunk work when I was writing a blog post about extendr, but it seems it was just my misunderstanding of something. Sorry for the noise.

Separately, do you think it would be necessary to slightly adjust the installation instructions for macOS users that they might need to add ~/.cargo/bin to PATH?

We are discussing about the better setup here: https://github.com/extendr/rextendr/issues/100.