@@ -792,60 +792,62 @@ get_datagrid.comparisons <- get_datagrid.slopes
792
792
if (all(grepl(' \\ ".*\\ "' , parts ))) parts <- gsub(' "' , " " , parts , fixed = TRUE )
793
793
794
794
# Make expression ----------
795
- if ((is.factor(x ) && parts %in% levels(x )) || (is.character(x ) && parts %in% x )) {
796
- # Factor
797
- # Add quotes around them
798
- parts <- paste0(" '" , parts , " '" )
799
- # Convert to character
800
- by_expression <- paste0(" as.factor(c(" , toString(parts ), " ))" )
801
- } else if (length(parts ) == 1 ) {
802
- # Numeric
803
- # If one, might be a shortcut. or a sampling request
804
- shortcuts <- c(
805
- " meansd" , " sd" , " mad" , " quartiles" , " quartiles2" , " zeromax" ,
806
- " minmax" , " terciles" , " terciles2" , " fivenum" , " pretty"
807
- )
808
- if (grepl(" sample" , parts , fixed = TRUE )) {
809
- n_to_sample <- .safe(as.numeric(trim_ws(gsub(" sample" , " " , parts , fixed = TRUE ))))
810
- # do we have a proper definition of the sample size? If not, error
811
- if (is.null(n_to_sample ) || is.na(n_to_sample ) || ! length(n_to_sample )) {
812
- format_error(" The token `sample` must be followed by the number of samples to be drawn, e.g. `[sample 15]`." ) # nolint
813
- }
814
- by_expression <- paste0(" c(" , paste(sample(x , n_to_sample ), collapse = " ," ), " )" )
815
- } else if (parts %in% shortcuts ) {
816
- if (parts %in% c(" meansd" , " sd" )) {
817
- center <- mean(x , na.rm = TRUE )
818
- spread <- stats :: sd(x , na.rm = TRUE )
819
- by_expression <- paste0(" c(" , center - spread , " ," , center , " ," , center + spread , " )" )
820
- } else if (parts == " mad" ) {
821
- center <- stats :: median(x , na.rm = TRUE )
822
- spread <- stats :: mad(x , na.rm = TRUE )
823
- by_expression <- paste0(" c(" , center - spread , " ," , center , " ," , center + spread , " )" )
824
- } else if (parts %in% c(" fivenum" , " quartiles" )) {
825
- by_expression <- paste0(" c(" , paste(as.vector(stats :: fivenum(x , na.rm = TRUE )), collapse = " ," ), " )" )
826
- } else if (parts == " quartiles2" ) {
827
- by_expression <- paste0(" c(" , paste(as.vector(stats :: quantile(x , na.rm = TRUE ))[2 : 4 ], collapse = " ," ), " )" )
828
- } else if (parts == " terciles" ) {
829
- by_expression <- paste0(" c(" , paste(as.vector(stats :: quantile(x , probs = (0 : 3 ) / 3 , na.rm = TRUE )), collapse = " ," ), " )" ) # nolint
830
- } else if (parts == " terciles2" ) {
831
- by_expression <- paste0(" c(" , paste(as.vector(stats :: quantile(x , probs = (1 : 2 ) / 3 , na.rm = TRUE )), collapse = " ," ), " )" ) # nolint
832
- } else if (parts == " pretty" ) {
833
- by_expression <- paste0(" c(" , paste(as.vector(pretty(x , na.rm = TRUE )), collapse = " ," ), " )" )
834
- } else if (parts == " zeromax" ) {
835
- by_expression <- paste0(" c(0," , max(x , na.rm = TRUE ), " )" )
836
- } else if (parts == " minmax" ) {
837
- by_expression <- paste0(" c(" , min(x , na.rm = TRUE ), " ," , max(x , na.rm = TRUE ), " )" )
838
- }
839
- } else if (is.numeric(parts )) {
840
- by_expression <- parts
795
+ if (length(parts ) == 1 ) {
796
+ if ((is.factor(x ) && parts %in% levels(x )) || (is.character(x ) && parts %in% x )) {
797
+ # Factor
798
+ # Add quotes around them
799
+ parts <- paste0(" '" , parts , " '" )
800
+ # Convert to character
801
+ by_expression <- paste0(" as.factor(c(" , toString(parts ), " ))" )
841
802
} else {
842
- format_error(
843
- paste0(
844
- " The `by` argument (" , by , " ) should either indicate a valid factor level, the minimum and the maximum value of a vector, or one of the following options: " , # nolint
845
- toString(shortcuts ),
846
- " ."
847
- )
803
+ # Numeric
804
+ # If one, might be a shortcut. or a sampling request
805
+ shortcuts <- c(
806
+ " meansd" , " sd" , " mad" , " quartiles" , " quartiles2" , " zeromax" ,
807
+ " minmax" , " terciles" , " terciles2" , " fivenum" , " pretty"
848
808
)
809
+ if (grepl(" sample" , parts , fixed = TRUE )) {
810
+ n_to_sample <- .safe(as.numeric(trim_ws(gsub(" sample" , " " , parts , fixed = TRUE ))))
811
+ # do we have a proper definition of the sample size? If not, error
812
+ if (is.null(n_to_sample ) || is.na(n_to_sample ) || ! length(n_to_sample )) {
813
+ format_error(" The token `sample` must be followed by the number of samples to be drawn, e.g. `[sample 15]`." ) # nolint
814
+ }
815
+ by_expression <- paste0(" c(" , paste(sample(x , n_to_sample ), collapse = " ," ), " )" )
816
+ } else if (parts %in% shortcuts ) {
817
+ if (parts %in% c(" meansd" , " sd" )) {
818
+ center <- mean(x , na.rm = TRUE )
819
+ spread <- stats :: sd(x , na.rm = TRUE )
820
+ by_expression <- paste0(" c(" , center - spread , " ," , center , " ," , center + spread , " )" )
821
+ } else if (parts == " mad" ) {
822
+ center <- stats :: median(x , na.rm = TRUE )
823
+ spread <- stats :: mad(x , na.rm = TRUE )
824
+ by_expression <- paste0(" c(" , center - spread , " ," , center , " ," , center + spread , " )" )
825
+ } else if (parts %in% c(" fivenum" , " quartiles" )) {
826
+ by_expression <- paste0(" c(" , paste(as.vector(stats :: fivenum(x , na.rm = TRUE )), collapse = " ," ), " )" )
827
+ } else if (parts == " quartiles2" ) {
828
+ by_expression <- paste0(" c(" , paste(as.vector(stats :: quantile(x , na.rm = TRUE ))[2 : 4 ], collapse = " ," ), " )" ) # nolint
829
+ } else if (parts == " terciles" ) {
830
+ by_expression <- paste0(" c(" , paste(as.vector(stats :: quantile(x , probs = (0 : 3 ) / 3 , na.rm = TRUE )), collapse = " ," ), " )" ) # nolint
831
+ } else if (parts == " terciles2" ) {
832
+ by_expression <- paste0(" c(" , paste(as.vector(stats :: quantile(x , probs = (1 : 2 ) / 3 , na.rm = TRUE )), collapse = " ," ), " )" ) # nolint
833
+ } else if (parts == " pretty" ) {
834
+ by_expression <- paste0(" c(" , paste(as.vector(pretty(x , na.rm = TRUE )), collapse = " ," ), " )" )
835
+ } else if (parts == " zeromax" ) {
836
+ by_expression <- paste0(" c(0," , max(x , na.rm = TRUE ), " )" )
837
+ } else if (parts == " minmax" ) {
838
+ by_expression <- paste0(" c(" , min(x , na.rm = TRUE ), " ," , max(x , na.rm = TRUE ), " )" )
839
+ }
840
+ } else if (is.numeric(parts )) {
841
+ by_expression <- parts
842
+ } else {
843
+ format_error(
844
+ paste0(
845
+ " The `by` argument (" , by , " ) should either indicate a valid factor level, the minimum and the maximum value of a vector, or one of the following options: " , # nolint
846
+ toString(shortcuts ),
847
+ " ."
848
+ )
849
+ )
850
+ }
849
851
}
850
852
# If only two, it's probably the range
851
853
} else if (length(parts ) == 2 ) {
0 commit comments