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

Executing tests freezes R process #216

Open
pawelru opened this issue Jan 22, 2024 · 8 comments
Open

Executing tests freezes R process #216

pawelru opened this issue Jan 22, 2024 · 8 comments
Labels

Comments

@pawelru
Copy link
Contributor

pawelru commented Jan 22, 2024

r$> devtools::test()
ℹ Testing teal.widgets
Loading required package: withr
✔ | F W  S  OK | Context
✔ |         33 | ggplot2_args                                                
(...)
⠏ |          0 | plot_with_settings                                     

The issue is here. Below line freezes R

is_draw(\() print_plot(\() ggplot2::ggplot(mtcars), \() "ANYTHING"))

This does work in CI (and CRAN checks) but it does not work locally which is very frustrating. It might be due to the fact that I am using VSCode which has its own engine for graphics

r$> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.5

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Europe/Zurich
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] teal.widgets_0.4.2.9000 testthat_3.2.1         

loaded via a namespace (and not attached):
 [1] gtable_0.3.4       bslib_0.6.1        ggplot2_3.4.4     
 [4] shinyjs_2.1.0      htmlwidgets_1.6.4  formatters_0.5.5  
 [7] devtools_2.4.5     remotes_2.4.2.1    vctrs_0.6.5       
[10] tools_4.3.2        generics_0.1.3     tibble_3.2.1      
[13] fansi_1.0.6        pkgconfig_2.0.3    R.oo_1.25.0       
[16] checkmate_2.3.1    desc_1.4.3         lifecycle_1.0.4   
[19] R.cache_0.16.0     compiler_4.3.2     stringr_1.5.1     
[22] brio_1.1.4         munsell_0.5.0      httpuv_1.6.13     
[25] shinyWidgets_0.8.1 htmltools_0.5.7    usethis_2.2.2     
[28] sass_0.4.8         later_1.3.2        pillar_1.9.0      
[31] jquerylib_0.1.4    urlchecker_1.0.1   ellipsis_0.3.2    
[34] R.utils_2.12.3     cachem_1.0.8       rtables_0.6.6     
[37] sessioninfo_1.2.2  mime_0.12          styler_1.10.2     
[40] tidyselect_1.2.0   digest_0.6.34      stringi_1.8.3     
[43] dplyr_1.1.4        purrr_1.0.2        rprojroot_2.0.4   
[46] fastmap_1.1.1      grid_4.3.2         colorspace_2.1-0  
[49] cli_3.6.2          magrittr_2.0.3     pkgbuild_1.4.3    
[52] utf8_1.2.4         withr_3.0.0        scales_1.3.0      
[55] promises_1.2.1     backports_1.4.1    R.methodsS3_1.8.2 
[58] memoise_2.0.1      shiny_1.8.0        miniUI_0.1.1.1    
[61] profvis_0.3.8      rlang_1.1.3        Rcpp_1.0.12       
[64] xtable_1.8-4       glue_1.7.0         pkgload_1.3.4     
[67] rstudioapi_0.15.0  jsonlite_1.8.8     R6_2.5.1          
[70] fs_1.6.3
@chlebowa
Copy link
Contributor

It might ba an app with if (interactive()). You are running the tests interactively.

@pawelru
Copy link
Contributor Author

pawelru commented Jan 22, 2024

That was also my guess but no. These are the erroneous lines and no shiny in there:

plot_funs <- list(
function() {
print(ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) +
ggplot2::geom_point())
},
function() lattice::densityplot(1),
function() {
ggplot2::ggplotGrob(
ggplot2::ggplot(mtcars, ggplot2::aes(mpg, wt)) +
ggplot2::geom_point()
)
},
function() plot(1),
function() boxplot(2),
function() 2
)
testthat::test_that("print_plot is able to plot different types of graphics", {
plot_types <- list(
function() "ANYTHING",
function() "trel",
function() "grob",
function() "base",
function() "base",
function() "other"
)
for (p in seq_along(plot_funs)) {
testthat::expect_true(
is_draw(function() print_plot(plot_funs[[p]], plot_types[[p]]))
)
}
})

@pawelru
Copy link
Contributor Author

pawelru commented Jan 22, 2024

Using debugger I managed to find that it's grDevices::dev.off() executed on exit from is_draw():

#' Function to check if a function has a side effect of drawing something
#' @param `function` function which possibly draws something.
#' @return `logical(1)` whether the function has a side effect of drawing a plot.
#' @note reference to https://stackoverflow.com/questions/74615694/check-if-a-function-draw-plot-something
#' @keywords internal
is_draw <- function(plot_fun) {
checkmate::assert_function(plot_fun)
grDevices::graphics.off() # close any current graphics devices
cdev <- grDevices::dev.cur()
plot_fun()
if (cdev != grDevices::dev.cur()) {
on.exit(grDevices::dev.off())
return(TRUE)
}
return(FALSE)
}

After temp removing this line, the second call to grDevices::graphics.off() also freezes the session

@pawelru
Copy link
Contributor Author

pawelru commented Jan 22, 2024

Found a workaround but this is still an issue. In VSCode, turn off r.plot.useHttpgd setting

@averissimo
Copy link
Contributor

It doesn't freeze on rstudio nor on the console

@averissimo
Copy link
Contributor

Is the r.plot.useHttpgd defined via R's options() or via vscode's GUI?

We could force change that option for a set of affected tests or globally if it makes sense

@kartikeyakirar
Copy link
Contributor

for me also it does not freeze on Rstudio
image

@chlebowa
Copy link
Contributor

It doesn't freeze on rstudio nor on the console

Sounds like an IDE issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants