Skip to content

Commit

Permalink
handle NAs.
Browse files Browse the repository at this point in the history
  • Loading branch information
clauswilke committed Feb 3, 2020
1 parent 6746a9e commit 5cc4b40
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# gridtext devel

- `richtext_grob()` and `textbox_grob()` now gracefully handle empty strings.
- `richtext_grob()` and `textbox_grob()` now gracefully handle empty strings
and NAs.

# gridtext 0.1.0

Expand Down
2 changes: 2 additions & 0 deletions R/richtext-grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ richtext_grob <- function(text, x = unit(0.5, "npc"), y = unit(0.5, "npc"),

# make sure we can handle input text even if provided as factor
text <- as.character(text)
# convert NAs to empty strings
text <- ifelse(is.na(text), "", text)

# make sure margin and padding are of length 4
margin <- rep(margin, length.out = 4)
Expand Down
2 changes: 2 additions & 0 deletions R/textbox-grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ textbox_grob <- function(text, x = NULL, y = NULL,

# make sure we can handle input text even if provided as factor
text <- as.character(text)
# convert NAs to empty strings
text <- ifelse(is.na(text), "", text)

# determine orientation and adjust accordingly
orientation <- match.arg(orientation)
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-richtext-grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ test_that("misc. tests", {
# empty strings work
expect_silent(richtext_grob(""))
expect_silent(richtext_grob(" "))

# NAs work
expect_silent(richtext_grob(c(" ", "abc", NA)))
})

test_that("visual tests", {
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-textbox-grob.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ test_that("misc. tests", {
# empty strings work
expect_silent(textbox_grob(""))
expect_silent(textbox_grob(" "))

# NAs work
expect_silent(textbox_grob(NA))
})

test_that("visual tests", {
Expand Down

0 comments on commit 5cc4b40

Please sign in to comment.