Skip to content

Commit 939ede6

Browse files
authored
Merge pull request #614 from yjunechoe/create_agent-base-pipe
Truncate long auto-generated `tbl_name`s
2 parents 0457249 + dccd3f9 commit 939ede6

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# pointblank (development version)
22

3+
- Bugfix agents auto-generating a table label that was too long. They now get truncated (#614)
4+
35
- Bugfix agents not searching the formula environment when materializing `~ tbl` (#599)
46

57
- `info_columns()` warn more informatively when no columns are selected (#589).

R/create_agent.R

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -507,13 +507,16 @@ create_agent <- function(
507507

508508
# Try to infer the table name if one isn't
509509
# explicitly given in `tbl_name`
510-
if (!is.null(tbl) && is.null(tbl_name)) {
511-
tbl_name <- deparse(match.call()$tbl)
512-
if (tbl_name[1] == ".") {
513-
tbl_name <- NA_character_
510+
if (is.null(tbl_name) && !missing(tbl)) {
511+
tbl_expr <- rlang::enexpr(tbl)
512+
tbl_name <- paste0(deparse(tbl_expr), collapse = " ")
513+
# truncate tbl expr that exceeds `deparse()` width (#613)
514+
if (nchar(tbl_name) > 60) {
515+
tbl_name <- truncate_expr(tbl_expr)
514516
}
515517
}
516-
if (is.null(tbl_name)) {
518+
# ignore empty or magrittr pipe
519+
if (is.null(tbl) || tbl_name == ".") {
517520
tbl_name <- NA_character_
518521
}
519522

R/utils.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,3 +1740,12 @@ string_is_valid_symbol <- function(x) {
17401740
rlang::is_scalar_character(x) &&
17411741
identical(make.names(x), x)
17421742
}
1743+
1744+
truncate_expr <- function(x) {
1745+
if (rlang::is_symbol(x)) {
1746+
rlang::expr_deparse(x)
1747+
} else {
1748+
truncated <- rlang::call2(x[[1]], quote(...))
1749+
rlang::expr_deparse(truncated)
1750+
}
1751+
}

tests/testthat/test-create_agent.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,35 @@ test_that("`agent` can materialize table from formula environment", {
169169
expect_identical(agent$tbl, data.frame(x = 1))
170170

171171
})
172+
173+
test_that("truncate long auto-generated `tbl_name`s", {
174+
175+
# Isue #613 (can't use base pipe in test but this is equivalent)
176+
expect_identical(
177+
create_agent(data.frame(
178+
a = c(1, 2, 3), b = c(1, 2, 3), c = c(1, 2, 3), d = c(1, 2, 3)
179+
)) %>%
180+
el("tbl_name"),
181+
"data.frame(...)"
182+
)
183+
expect_identical(
184+
create_agent(data.frame(a = c(1, 2, 3), b = 4:6)) %>%
185+
el("tbl_name"),
186+
"data.frame(a = c(1, 2, 3), b = 4:6)"
187+
)
188+
189+
# Same treatment for formulas
190+
expect_identical(
191+
create_agent(~ data.frame(
192+
a = c(1, 2, 3), b = c(1, 2, 3), c = c(1, 2, 3), d = c(1, 2, 3)
193+
)) %>%
194+
el("tbl_name"),
195+
"~..."
196+
)
197+
expect_identical(
198+
create_agent(~ data.frame(a = c(1, 2, 3), b = 4:6)) %>%
199+
el("tbl_name"),
200+
"~data.frame(a = c(1, 2, 3), b = 4:6)"
201+
)
202+
203+
})

0 commit comments

Comments
 (0)