extendr / rextendr

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

Error in myextendr::hello_world() : object 'wrap__hello_world' not found #255

Closed junjunlab closed 1 year ago

junjunlab commented 1 year ago

Hi, I am creating an R package with example according to rextendr. But I got this error "Error in myextendr::hello_world() : object 'wrap__hello_world' not found" at last. How can I solve this error. Thanks!

Ilia-Kosenkov commented 1 year ago

Hi! It is hard to diagnose the problem based on what you wrote. Can you share the full example with us? E.g. the sequence of commands you executed.

junjunlab commented 1 year ago

Problem has been solved, thanks! By the way, how can I change rust hashmap into R list object with extendr, the following is my example incorrect code:

#[extendr]
fn get_hashmap() -> HashMap<String, f64> {
    let mut map = HashMap::new();
    map.insert("a".to_string(), 1.0);
    map.insert("b".to_string(), 2.0);
    map.insert("c".to_string(), 3.0);
    map
} 
Ilia-Kosenkov commented 1 year ago

It would be nice if you could still explain what was the original problem before we close the issue so in the future other developers can check it out before opening a new discussion.

JosiahParry commented 1 year ago

Here is how to accomplish turning a hashmap into a list.

rust_function(
'
 fn get_hashmap() -> List {
    let mut map = std::collections::HashMap::new();
    map.insert("a".to_string(), r!(1.0));
    map.insert("b".to_string(), r!(2.0));
    map.insert("c".to_string(), r!(3.0));
    List::from_hashmap(map).unwrap()
} 
'
)

There are two interesting point here.

  1. The output of this returns b, a, then c which isn't the same order as it's inserted in. Not sure that matters at all.
  2. The hashmap values must be Robjs and cannot be Rfloat for example.

The following will not compile.

  fn get_hashmap() -> List {
    let mut map = std::collections::HashMap::new();
    map.insert("a".to_string(), Rfloat::from(1.0));
    map.insert("b".to_string(), Rfloat::from(2.0));
    map.insert("c".to_string(), Rfloat::from(3.0));
    List::from_hashmap(map).unwrap()
} 
Ilia-Kosenkov commented 1 year ago

Well, hashmaps do not preserve the order of the items. In fact, hashmaps can return items in a different order every time you re-start the application, so that is expected. Speaking of requirement to store Robj there, we need to check our trait bounds, I suspect you should be able to store any T : IntoRobj there.

Qile0317 commented 1 year ago

Hi! It is hard to diagnose the problem based on what you wrote. Can you share the full example with us? E.g. the sequence of commands you executed.

I'm also a new user, and I just simply followed the tutorial and very simply ran the following:

library(rextendr)
rextendr::use_extendr()
rextendr::document()
hello_world()

Everything printed in the R terminal before running hello_world() seemed normal, but running the function outputs the following error: Error in hello_world() : object 'wrap__hello_world' not found

JosiahParry commented 1 year ago

Can you try the following? I think you may need to load_all()

rextendr::use_extendr()
rextendr::document()
devtools::load_all()
hello_world()
Qile0317 commented 1 year ago

Can you try the following? I think you may need to load_all()

rextendr::use_extendr()
rextendr::document()
devtools::load_all()
hello_world()

ah, worked perfectly, thank you!

CGMossa commented 1 year ago

First, rextendr::use_extendr() is to be used only once per package. My experience rexendr::document() is all you need, but maybe I'm misunderstanding something.

Since everything is solved and everyone is happy, I'll close this.