Skip to content

Commit c271ac8

Browse files
committed
Set variable length as number of columns for dfs and matrices
1 parent 818e158 commit c271ac8

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

crates/ark/src/variables/variable.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -547,29 +547,25 @@ impl PositronVariable {
547547
}
548548

549549
fn variable_length(x: SEXP) -> usize {
550+
// Check for tabular data
551+
if let Some(info) = harp::table_info(x) {
552+
return info.dims.num_cols as usize;
553+
}
554+
555+
// Otherwise treat as vector
550556
let rtype = r_typeof(x);
551557
match rtype {
552-
LGLSXP | RAWSXP | INTSXP | REALSXP | CPLXSXP | STRSXP => unsafe {
558+
LGLSXP | RAWSXP | INTSXP | REALSXP | CPLXSXP | STRSXP | LISTSXP => unsafe {
553559
Rf_xlength(x) as usize
554560
},
555561
VECSXP => unsafe {
556-
if r_inherits(x, "POSIXlt") {
562+
// TODO: Support vctrs types like record vectors
563+
if r_inherits(x, "POSIXlt") && r_typeof(x) == VECSXP && r_length(x) > 0 {
557564
Rf_xlength(VECTOR_ELT(x, 0)) as usize
558-
} else if r_is_data_frame(x) {
559-
let dim = RFunction::new("base", "dim.data.frame")
560-
.add(x)
561-
.call()
562-
.unwrap();
563-
564-
INTEGER_ELT(*dim, 0) as usize
565565
} else {
566566
Rf_xlength(x) as usize
567567
}
568568
},
569-
LISTSXP => match pairlist_size(x) {
570-
Ok(n) => n as usize,
571-
Err(_) => 0,
572-
},
573569
_ => 0,
574570
}
575571
}

crates/harp/src/table.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ pub struct TableInfo {
2121
}
2222

2323
// TODO: Might want to encode as types with methods so that we can make
24-
// assumptions about memory layout more safely
24+
// assumptions about memory layout more safely. Also makes it possible
25+
// to compute properties more lazily.
2526
pub fn table_info(x: SEXP) -> Option<TableInfo> {
2627
if r_is_data_frame(x) {
2728
return df_info(x).ok();

0 commit comments

Comments
 (0)