@@ -145,6 +145,13 @@ parse_fun <- function() {
145145
146146 done <- FALSE
147147 call <- standardize(.p $ calls $ fun )
148+
149+ if (length(.p $ var ) > 1 ) {
150+ # str <- paste0(deparse(.p$calls[[grep(.p$caller, .p$calls)[1]]]), collapse = "")
151+ done <- parse_data_str(deparse(call ))
152+ return (done )
153+ }
154+
148155 obj <- .p $ sf [[.p $ pos $ fun ]][[.p $ var ]]
149156
150157 # Extract names from x argument
@@ -390,13 +397,14 @@ parse_pipe <- function() {
390397 envir = .p $ sf [[.p $ pos $ pipe ]]$ parent )
391398
392399 if (is.data.frame(obj )) {
393- if (" var_name" %in% names(.p $ output ) && ncol(obj ) == 1 ) {
394- done <- upd_output(" var_name" , names(obj ))
395- done <- upd_output(" var_label" , label(obj [[1 ]]))
400+ obj_df <- obj
401+ if (" var_name" %in% names(.p $ output ) && ncol(obj_df ) == 1 ) {
402+ done <- upd_output(" var_name" , colnames(obj_df ))
403+ done <- upd_output(" var_label" , label(obj_df [[1 ]]))
396404 if (done ) return (TRUE )
397405 }
398406
399- done <- upd_output(" df_label" , as.character(label(obj )))
407+ done <- upd_output(" df_label" , as.character(label(obj_df )))
400408 if (length(obj_str ) == 1 ) {
401409 done <- upd_output(" df_name" , obj_name )
402410 if (done ) return (TRUE )
@@ -409,23 +417,32 @@ parse_pipe <- function() {
409417 }
410418 } else {
411419 done <- parse_data_str(obj_name )
420+ obj_df <- NULL
412421 }
413422 if (done ) return (TRUE )
414423
415424 # Move focus to rhs
416425 if (" var_name" %in% names(.p $ output )) {
417426 rhs <- call $ rhs
418-
419427 if (is.call(rhs ))
420428 rhs <- standardize(rhs )
421429
422430 rhs_nms <- all.names(rhs )
423431 if (.p $ caller %in% rhs_nms && length(rhs_nms ) > 1 ) {
424432 rhs_args <- setdiff(rhs_nms , .p $ caller )
425- if (length(rhs_args ) == 1 && rhs_args %in% colnames(obj )) {
426- done <- upd_output(" var_name" , rhs_args )
427- done <- upd_output(" var_label" , label(obj [[rhs_args ]]))
428- if (done ) return (TRUE )
433+ if (length(rhs_args ) == 1 ) {
434+ if (rhs_args %in% colnames(obj_df )) {
435+ done <- upd_output(" var_name" , rhs_args )
436+ done <- upd_output(" var_label" , label(obj_df [[rhs_args ]]))
437+ if (done ) return (TRUE )
438+ }
439+ } else {
440+ if (length(var_ind <- which(rhs_args %in% colnames(obj_df ))) == 1 ) {
441+ var_name <- rhs_args [[var_ind ]]
442+ done <- upd_output(" var_name" , var_name )
443+ done <- upd_output(" var_label" , label(obj_df [[var_name ]]))
444+ if (done ) return (TRUE )
445+ }
429446 }
430447 }
431448
@@ -478,7 +495,7 @@ parse_piper <- function() {
478495 obj_str <- setdiff(obj_str , c(.p $ caller , .st_env $ oper , " " ))
479496 if (length(obj_str ) == 1 ) {
480497 done <- upd_output(" var_name" , obj_str )
481- done <- upd_output(" var_label" , label(obj ))
498+ done <- try( upd_output(" var_label" , label(obj )), silent = TRUE )
482499 } else if (length(obj_str ) == 2 ) {
483500 obj_df <- try(get_object(obj_str [1 ], " data.frame" ),
484501 silent = TRUE )
@@ -548,8 +565,11 @@ deduce_names <- function() {
548565 # - if there is a df, hope there is only one other object left
549566 nms <- setdiff(all.names(sys.calls()[[1 ]]), .p $ caller )
550567 call <- standardize(sys.calls()[[1 ]])
551- nms <- unique(c(nms , as.character(call [[.p $ var ]])))
552-
568+
569+ if (length(.p $ var ) == 1 ) {
570+ nms <- unique(c(nms , as.character(call [[.p $ var ]])))
571+ }
572+
553573 nnames <- length(nms )
554574 df_found <- ! empty_na(.p $ output $ df_name )
555575
@@ -571,8 +591,6 @@ deduce_names <- function() {
571591 }
572592 } else candidates %+ = % c(untested = nm )
573593 } else if (is.data.frame(obj_ )) {
574- cand_class %+ = % " data.frame"
575- names(cand_class )[length(cand_class )] <- nm
576594 if (isFALSE(df_found )) {
577595 df_found <- TRUE
578596 obj_df <- obj_
@@ -581,8 +599,7 @@ deduce_names <- function() {
581599 if (done ) return (TRUE )
582600 nnames <- nnames - 1
583601 } else {
584- # We have a 2nd data frame; we'll simply ignore it, trusting
585- # previous stages
602+ # We had already found df_name, so we'll simply ignore it
586603 nnames <- nnames - 1
587604 }
588605 } else if (inherits(obj_ , " function" )) {
@@ -616,17 +633,27 @@ deduce_names <- function() {
616633 }
617634
618635 # If there is only 1 tested, we keep it
619- if ( table(candidates [ ' tested' ])[[ 1 ]] == 1 ) {
620- done <- upd_output( " var_name " , candidates [[ ' tested ' ]])
621- if ( done ) return ( TRUE )
622- if (isTRUE( df_found ) )
623- done <- upd_output(" var_label" , label(obj_df [[candidates [[ ' tested ' ]] ]]))
624- if ( done ) return (TRUE )
636+ n_tested <- table(names( candidates ))[[ ' tested' ]]
637+ if ( n_tested == 1 ) {
638+ var_name <- candidates [[ ' tested ' ]]
639+ done <- upd_output( " var_name " , var_name )
640+ done <- upd_output(" var_label" , label(obj_df [[var_name ]]))
641+ return (TRUE )
625642 } else {
626- # At this stage we can't determine which variable is the right one
627- message(" Unable to determine variable and/or df name" )
643+ # More than one variable -- hopefully ctable
644+ if (n_tested == 2 && length(.p $ var ) == 2 ) {
645+ done <- upd_output(
646+ " var_name" ,
647+ unname(candidates [names(candidates ) == " tested" ]),
648+ force = TRUE
649+ )
650+ }
628651 }
629652 }
653+ if (done ) return (TRUE )
654+
655+ # Set .p$do_return to TRUE to avoid warning (although there will be a msg)
656+ .p $ do_return <- TRUE
630657 return (FALSE )
631658}
632659
@@ -759,6 +786,31 @@ parse_data_str <- function(str) {
759786 done <- upd_output(" var_name" , obj_name )
760787 done <- upd_output(" var_label" , label(obj ))
761788 if (done ) return (TRUE )
789+ } else {
790+ if (is.function(obj )) {
791+ # Most probably something like descr(rnorm(10))
792+ # First, confirm that function is a summarytools fn
793+ if (! grepl(" summarytools" ,
794+ capture.output(pryr :: where(obj_name ))[1 ])) {
795+ # See if only one of var_name & df_name is required, and
796+ # use that slot and return
797+ name_slots <- grep(" _name" , names(.p $ output ), value = TRUE )
798+ if (length(name_slots ) == 1 ) {
799+ upd_output(name_slots , str , force = TRUE )
800+ .p $ do_return <- TRUE
801+ return (TRUE )
802+ } else {
803+ # Get first element of evaluated str to determine which
804+ # slot to use
805+ if (is.data.frame(eval(str2expression(str ))[1 ]))
806+ upd_output(" df_name" , str , force = TRUE )
807+ else
808+ upd_output(" var_name" , str , force = TRUE )
809+ .p $ do_return <- TRUE
810+ return (TRUE )
811+ }
812+ }
813+ }
762814 }
763815 }
764816 }
0 commit comments