-
Notifications
You must be signed in to change notification settings - Fork 333
WIP: explore auto-labelling with values #2142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
36d0a8b
4414dda
0f24281
3444a77
118cf20
60d0eb7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,11 +41,10 @@ quasi_label <- function(quo, label = NULL, arg = "quo") { | |
} | ||
|
||
expr <- quo_get_expr(quo) | ||
value <- eval_bare(expr, quo_get_env(quo)) | ||
label <- label %||% auto_label(expr, value) | ||
|
||
labelled_value( | ||
eval_bare(expr, quo_get_env(quo)), | ||
label %||% expr_label(expr) | ||
) | ||
labelled_value(value, label) | ||
} | ||
|
||
labelled_value <- function(value, label) { | ||
|
@@ -66,6 +65,39 @@ quasi_capture <- function(.quo, .label, .capture, ...) { | |
act | ||
} | ||
|
||
auto_label <- function(expr, value) { | ||
if (is.call(expr) || is.name(expr)) { | ||
label <- expr_label(expr) | ||
if (can_inline(value)) { | ||
paste0(label, " (", as_label(value), ")") | ||
} else { | ||
label | ||
} | ||
} else { | ||
expr_label(expr) | ||
} | ||
} | ||
|
||
can_inline <- function(x) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (is.null(x)) { | ||
return(TRUE) | ||
} | ||
if (!is.atomic(x) || !is.vector(x)) { | ||
return(FALSE) | ||
} | ||
if (length(x) != 1) { | ||
return(FALSE) | ||
} | ||
|
||
if (is.character(x)) { | ||
is.na(x) || (!grepl("\n", x) && nchar(x) < 100) | ||
} else if (is.logical(x) || is.numeric(x)) { | ||
TRUE | ||
} else { | ||
FALSE | ||
} | ||
} | ||
|
||
expr_label <- function(x) { | ||
if (is_syntactic_literal(x)) { | ||
deparse1(x) | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,34 +1,34 @@ | ||||||
# logical tests act as expected | ||||||
|
||||||
FALSE (`actual`) is not equal to TRUE (`expected`). | ||||||
FALSE is not equal to TRUE. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we quote the expressions?
Suggested change
I guess this becomes too heavy in the parenthetised/string-quoted cases
|
||||||
|
||||||
`actual`: FALSE | ||||||
`expected`: TRUE | ||||||
|
||||||
--- | ||||||
|
||||||
TRUE (`actual`) is not equal to FALSE (`expected`). | ||||||
TRUE is not equal to FALSE. | ||||||
|
||||||
`actual`: TRUE | ||||||
`expected`: FALSE | ||||||
|
||||||
# can compare non-vectors | ||||||
|
||||||
quote(x) (`actual`) is not equal to TRUE (`expected`). | ||||||
quote(x) is not equal to TRUE. | ||||||
|
||||||
`actual` is a symbol | ||||||
`expected` is a logical vector (TRUE) | ||||||
|
||||||
# expect_null works | ||||||
|
||||||
1L (`actual`) is not equal to FALSE (`expected`). | ||||||
1L is not equal to FALSE. | ||||||
|
||||||
`actual` is an integer vector (1) | ||||||
`expected` is NULL | ||||||
|
||||||
--- | ||||||
|
||||||
environment() (`actual`) is not equal to FALSE (`expected`). | ||||||
environment() is not equal to FALSE. | ||||||
|
||||||
`actual` is an environment | ||||||
`expected` is NULL | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,24 @@ | ||
# provide useful feedback on failure | ||
|
||
1 (`actual`) is not identical to "a" (`expected`). | ||
`x` (1) is not identical to "a". | ||
|
||
`actual` is a double vector (1) | ||
`expected` is a character vector ('a') | ||
|
||
--- | ||
|
||
1 (`actual`) is not equal to "a" (`expected`). | ||
`x` (1) is not equal to "a". | ||
|
||
`actual` is a double vector (1) | ||
`expected` is a character vector ('a') | ||
|
||
--- | ||
|
||
1 not identical to "a". | ||
`x` (1) not identical to "a". | ||
Types not compatible: double is not character | ||
|
||
--- | ||
|
||
1 not equal to "a". | ||
`x` (1) not equal to "a". | ||
Types not compatible: double is not character | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,73 @@ | ||
# useful message on failure | ||
|
||
"actual" (`actual`) and "expected" (`expected`) don't have the same values. | ||
"actual" doesn't have the same values as "expected". | ||
* Only in `actual`: "actual" | ||
* Only in `expected`: "expected" | ||
|
||
|
||
--- | ||
|
||
1:2 (`actual`) and 2 (`expected`) don't have the same values. | ||
`x` doesn't have the same values as `y` (2). | ||
* Only in `actual`: 1 | ||
|
||
|
||
--- | ||
|
||
2 (`actual`) and 2:3 (`expected`) don't have the same values. | ||
`x` (2) doesn't have the same values as `y`. | ||
* Only in `expected`: 3 | ||
|
||
|
||
--- | ||
|
||
1:2 (`actual`) and 2:3 (`expected`) don't have the same values. | ||
`x` doesn't have the same values as `y`. | ||
* Only in `actual`: 1 | ||
* Only in `expected`: 3 | ||
|
||
|
||
--- | ||
|
||
c("a", "a") (`actual`) and c("b", "b", "b") (`expected`) don't have the same values. | ||
`x` doesn't have the same values as `y`. | ||
* Only in `actual`: "a" | ||
* Only in `expected`: "b" | ||
|
||
|
||
--- | ||
|
||
`x` doesn't have the same values as c("a", "b", "c", "d"). | ||
* Only in `expected`: "d" | ||
|
||
|
||
# truncates long vectors | ||
|
||
1:2 (`actual`) and 1:50 (`expected`) don't have the same values. | ||
`x` doesn't have the same values as `y`. | ||
* Only in `expected`: 3, 4, 5, 6, 7, 8, 9, 10, 11, ... | ||
|
||
|
||
# expect_contains() gives useful message on failure | ||
|
||
`x1` (`actual`) doesn't fully contain all the values in `x2` (`expected`). | ||
* Missing from `actual`: "d" | ||
* Present in `actual`: "a", "b", "c" | ||
Comment on lines
-42
to
-44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's actually a case where the parameter names are useful and add context (as opposed to e.g. |
||
`x1` doesn't fully contain all the values in `x2`. | ||
* Missing from `x1`: "d" | ||
* Present in `x1`: "a", "b", "c" | ||
|
||
|
||
--- | ||
|
||
`x1` (`actual`) doesn't fully contain all the values in `x3` (`expected`). | ||
* Missing from `actual`: "d", "e" | ||
* Present in `actual`: "a", "b", "c" | ||
`x1` doesn't fully contain all the values in `x3`. | ||
* Missing from `x1`: "d", "e" | ||
* Present in `x1`: "a", "b", "c" | ||
|
||
|
||
# expect_in() gives useful message on failure | ||
|
||
`x1` (`actual`) isn't fully contained within `x2` (`expected`). | ||
* Missing from `expected`: "a" | ||
* Present in `expected`: "b", "c" | ||
`x1` isn't fully contained within `x2`. | ||
* Missing from `x1`: "a" | ||
* Present in `x1`: "b", "c" | ||
|
||
|
||
--- | ||
|
||
`x1` (`actual`) isn't fully contained within `x3` (`expected`). | ||
* Missing from `expected`: "a", "b" | ||
* Present in `expected`: "d", "e" | ||
`x1` isn't fully contained within `x3`. | ||
* Missing from `x1`: "a", "b" | ||
* Present in `x1`: "d", "e" | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd avoid
expr_label()
as it's a remnant from lazyeval. Do you get good results if you useas_label()
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried that and it introduced a bunch of small changes that were largely for the worse. I think this must have deviated from
as_label
at some time in the past.