Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 15 additions & 2 deletions R/sanitize_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion R/tinyformula.R
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
15 changes: 10 additions & 5 deletions R/tinyplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down