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 support for computing summary stats #342

Merged
merged 4 commits into from
May 15, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions crates/harp/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ impl TryFrom<&Vec<i32>> for RObject {
}
}

// Converts an R named character vector to a HashMap<String, String>
// Note: Duplicated names are silently ignored, and only the first occurence is kept.
impl TryFrom<RObject> for HashMap<String, String> {
type Error = crate::error::Error;
fn try_from(value: RObject) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -936,6 +938,8 @@ impl TryFrom<RObject> for HashMap<String, String> {
}
}

// Converts an R named integer vector to a HashMap<String, i32>
// Note: Duplicated names are silently ignored, and only the first occurence is kept.
impl TryFrom<RObject> for HashMap<String, i32> {
type Error = crate::error::Error;
fn try_from(value: RObject) -> Result<Self, Self::Error> {
Expand Down Expand Up @@ -1409,6 +1413,34 @@ mod tests {
assert_eq!(out.get("pepperoni").unwrap(), "OK");
assert_eq!(out.get("sausage").unwrap(), "OK");
assert_eq!(out.get("pineapple").unwrap(), "NOT OK");


let v = r_parse_eval0("c(x = 'a', y = 'b', z = 'c')", R_ENVS.global).unwrap();
let out: HashMap<String, String> = v.try_into().unwrap();
assert_eq!(out["x"], "a"); // duplicated name is ignored and first is kept
assert_eq!(out["y"], "b");
}
}

#[test]
#[allow(non_snake_case)]
fn test_tryfrom_RObject_hashmap_i32() {
r_test! {
// Create a map of pizza toppings to their acceptability.
Copy link
Contributor

Choose a reason for hiding this comment

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

wat 😋

let v = r_parse_eval0("list(x = 1L, y = 2L, x = 3L)", R_ENVS.global).unwrap();
assert_eq!(v.length(), 3 as isize);

// Ensure we created an object of the same size as the map.
let out: HashMap<String, i32> = v.try_into().unwrap();

// Ensure we can convert the object back into a map with the same values.
assert_eq!(out["x"], 1); // duplicated name is ignored and first is kept
assert_eq!(out["y"], 2);

let v = r_parse_eval0("c(x = 1L, y = 2L, x = 3L)", R_ENVS.global).unwrap();
let out: HashMap<String, i32> = v.try_into().unwrap();
assert_eq!(out["x"], 1); // duplicated name is ignored and first is kept
assert_eq!(out["y"], 2);
}
}

Expand Down
Loading