diff --git a/R/sanitize_type.R b/R/sanitize_type.R index 6a13516c..dc50fd57 100644 --- a/R/sanitize_type.R +++ b/R/sanitize_type.R @@ -43,10 +43,23 @@ sanitize_type = function(settings) { assert_choice(type, known_types, null.ok = TRUE) if (is.null(type)) { - if (!is.null(x) && (is.factor(x) || is.character(x)) && !(is.factor(y) || is.character(y))) { + if (is.null(x) && !(is.factor(y) || is.character(y))) { + # enforce histogram type for y ~ 1 + settings$x = y + settings$y = NULL + type = type_hist + } else if (is.null(x) && (is.factor(y) || is.character(y))) { + # enforce barplot type for factor(y) ~ 1 + settings$x = y + settings$y = NULL + type = type_barplot + } else if ((is.factor(x) || is.character(x)) && is.null(y)) { + # enforce barplot type for ~ factor(y) + type = type_barplot + } else if (!is.null(x) && (is.factor(x) || is.character(x)) && !(is.factor(y) || is.character(y))) { # enforce boxplot type for y ~ factor(x) type = type_boxplot - } else if (is.factor(y) || is.character(y)) { + } else if (!is.null(x) && (is.factor(y) || is.character(y))) { # enforce spineplot type for factor(y) ~ x type = type_spineplot } else { diff --git a/R/tinyformula.R b/R/tinyformula.R index 56fd546e..29189061 100644 --- a/R/tinyformula.R +++ b/R/tinyformula.R @@ -74,6 +74,8 @@ tinyframe = function(formula, data, drop = FALSE) { ## - formula: (sub-)formula ## - data: model.frame from full formula if (is.null(formula)) return(NULL) - names = sapply(attr(terms(formula), "variables")[-1L], deparse, width.cutoff = 500L) + vars = attr(terms(formula), "variables")[-1L] + if (is.null(vars)) return(NULL) + names = sapply(vars, deparse, width.cutoff = 500L) data[, names, drop = drop] } diff --git a/R/tinyplot.R b/R/tinyplot.R index 1073a4c9..22bc1135 100644 --- a/R/tinyplot.R +++ b/R/tinyplot.R @@ -1429,13 +1429,16 @@ tinyplot.formula = function( m[[1L]] = quote(stats::model.frame) mf = eval.parent(m) - ## extract x + ## extract x (if any) x = tinyframe(tf$x, mf) - xnam = names(x)[[1L]] - if (length(names(x)) != 1L) warning( - paste("formula should specify exactly one x-variable, using:", xnam), + if (!is.null(x)) { + xnam = names(x)[[1L]] + if (length(names(x)) > 1L) warning(paste("formula should specify at most one x-variable, using:", xnam), "\nif you want to use arithmetic operators, make sure to wrap them inside I()") - x = x[[xnam]] + x = x[[xnam]] + } else { + xnam = NULL + } ## extract y (if any) y = tinyframe(tf$y, mf) @@ -1444,6 +1447,8 @@ tinyplot.formula = function( if (length(names(y)) > 1L) warning(paste("formula should specify at most one y-variable, using:", ynam), "\nif you want to use arithmetic operators, make sure to wrap them inside I()") y = y[[ynam]] + } else { + ynam = NULL } ## extract by (if any)