Skip to content
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

Blank Rplots.pdf generated when running snapshot tests interactively from RStudio #227

Open
nanxstats opened this issue May 30, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@nanxstats
Copy link
Collaborator

nanxstats commented May 30, 2024

To reproduce the issue:

usethis::create_package("zzz/")
usethis::use_testthat()

Run usethis::use_r("myfun") and put this function into it:

#' @export
myfun <- function(path) {
  head(datasets::iris) |>
    r2rtf::rtf_body() |>
    r2rtf::rtf_encode() |>
    r2rtf::write_rtf(file = path)
}

Run usethis::use_test("myfun") to create this test:

test_that("myfun snapshot", {
  path <- tempfile(fileext = ".rtf")
  myfun(path)

  local_edition(3)
  expect_snapshot_file(path, "myfun.rtf")
})

Run devtools::document() and devtools::load_all().

Clicking the "Run Tests" button or the "Test" button in the Build panel in RStudio will generate a blank, redundant tests/testthat/Rplots.pdf, while no such Rplots.pdf is generated when running devtools::test() or devtools::test_active_file() in the R console.

Reproduced under macOS, Windows, and Posit Cloud.

@nanxstats nanxstats added the bug Something isn't working label May 30, 2024
@nanxstats
Copy link
Collaborator Author

The culprit is in these two places where graphics::par() and graphics::strwidth() were called:

text_cex <- attr(tbl, "text_font_size") / graphics::par("ps")

r2rtf/R/rtf_strwidth.R

Lines 110 to 115 in 307c112

x$width <- graphics::strwidth(x$text,
units = "inches",
cex = x$cex[1],
font = x$font[1],
family = as.character(x$family[1])
)

By replacing them both with a constant number, the problem goes away. Looks like by calling these functions in graphics, a PDF device is somehow triggered when running the tests in RStudio interactively.

@nanxstats
Copy link
Collaborator Author

This unit test using graphics::strwidth() also needs side effect isolation, otherwise, Rplots.pdf will be generated:

size8italic <- round(graphics::strwidth("This is a long sentence",
units = "inches",
cex = 2 / 3,
font = 3,
family = "Times"
), 5)
bold <- round(graphics::strwidth("third",
units = "inches",
cex = 2 / 3,
font = 2,
family = "Times"
), 5)

@elong0527
Copy link
Collaborator

It seems it only happens while running unit test. I did not see the RPlots.pdf is created by running the code above in an interactive R sessions.

Guess a graphic device is initiated when devtools::test() detected graphics package is used. As I do see a RPlots.pdf file is created if we call pdf().

I am OK to live with it.

@nanxstats
Copy link
Collaborator Author

I should have been clearer - this only happens when you click the "Run Tests" button above the code editor or the "Test" button in the Build panel in RStudio, but not when you send the code into the R console.

Regardless, I'd say it will be productive to get this fixed, because every package that have tests with underlying code using r2rtf will get this unavoidable side effect - and it can be confusing and annoying. For example, see previous discussions:

Merck/metalite.ae#75
Merck/metalite.ae#73

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants