JuliaInterop / JuliaCall

Embed Julia in R
https://non-contradiction.github.io/JuliaCall/index.html
Other
267 stars 36 forks source link

Plotting error in Rstudio #133

Open donskerclass opened 4 years ago

donskerclass commented 4 years ago

Attempting to call plot from the console or a code block in an Rmarkdown document is failing with an R error.

If I run from an .Rmd file the block

```{julia}
using Plots
gr()
dd = [1, 2, 3, 4]
ee = [3, 7, 2, 1]
plot(dd,ee)
```.

I get result

Error in if (suffix != "" && !grepl("[.]", suffix)) suffix = paste0(".", : missing value where TRUE/FALSE needed

It seems like something is getting cut off when it tries to plot. Aside from plotting from Julia, I haven't had other issues running Julia and R in alternating blocks.

I'm running Julia 1.4.2 and have included my R session info and Julia project list below, in case there's something weird just with my setup.

Session Info R version 3.5.0 (2018-04-23) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS 10.15.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/3.5/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] gridExtra_2.3 zoo_1.8-6 lubridate_1.7.4 forcats_0.4.0 stringr_1.4.0 [6] dplyr_0.8.3 purrr_0.3.2 readr_1.3.1 tidyr_0.8.3 tibble_3.0.1 [11] ggplot2_3.3.0 tidyverse_1.2.1 forecast_8.8 readstata13_0.9.2 foreign_0.8-72 [16] JuliaCall_0.17.1 loaded via a namespace (and not attached): [1] tseries_0.10-47 httr_1.4.1 jsonlite_1.6.1 splines_3.5.0 [5] modelr_0.1.5 StanHeaders_2.21.0-1 assertthat_0.2.1 TTR_0.23-4 [9] stats4_3.5.0 cellranger_1.1.0 pillar_1.4.4 backports_1.1.6 [13] lattice_0.20-38 glue_1.4.1 quadprog_1.5-7 digest_0.6.25 [17] rvest_0.3.4 colorspace_1.4-1 Matrix_1.2-17 timeDate_3043.102 [21] pkgconfig_2.0.3 rstan_2.19.3 broom_0.5.2 haven_2.1.1 [25] scales_1.1.0 processx_3.4.2 mgcv_1.8-28 generics_0.0.2 [29] farver_2.0.3 ellipsis_0.3.1 withr_2.1.2 urca_1.3-0 [33] nnet_7.3-12 cli_2.0.2 quantmod_0.4-15 magrittr_1.5 [37] crayon_1.3.4 readxl_1.3.1 ps_1.3.2 fansi_0.4.1 [41] nlme_3.1-141 xts_0.11-2 xml2_1.2.2 pkgbuild_1.0.6 [45] loo_2.2.0 prettyunits_1.1.1 tools_3.5.0 hms_0.5.0 [49] matrixStats_0.56.0 lifecycle_0.2.0 munsell_0.5.0 callr_3.4.3 [53] packrat_0.5.0 compiler_3.5.0 rlang_0.4.6 grid_3.5.0 [57] rstudioapi_0.11 labeling_0.3 gtable_0.3.0 fracdiff_1.4-2 [61] inline_0.3.15 curl_4.3 R6_2.4.1 knitr_1.28 [65] stringi_1.4.6 parallel_3.5.0 Rcpp_1.0.4.6 vctrs_0.3.1 [69] tidyselect_0.2.5 xfun_0.8 lmtest_0.9-37 Status `~/.julia/environments/v1.4/Project.toml` [c52e3926] Atom v0.12.14 [6cf23378] DSGE v0.8.1 [a93c6f00] DataFrames v0.21.4 [aae7a2af] DiffEqFlux v1.17.0 [41bf760c] DiffEqSensitivity v6.26.0 [0c46a032] DifferentialEquations v6.15.0 [31c24e10] Distributions v0.23.4 [634d3b9d] DrWatson v1.13.1 [587475ba] Flux v0.10.4 [6e85b7ee] FredData v0.4.0 [7073ff75] IJulia v1.21.2 [c601a237] Interact v0.10.3 [e5e0dc1b] Juno v0.8.2 [c7f686f2] MCMCChains v3.0.12 [e47e5152] ModelConstructors v0.1.4 [4d1e1d77] Nullables v1.0.0 [429524aa] Optim v0.22.0 [bac558e1] OrderedCollections v1.3.0 [91a5bcdd] Plots v1.4.2 [d330b81b] PyPlot v2.9.0 [6f49c342] RCall v0.13.7 [30cb0354] SciMLTutorials v0.8.0 #master (https://github.com/SciML/SciMLTutorials.jl) [f3b207a7] StatsPlots v0.14.6 [fd094767] Suppressor v0.2.0 [fce5fe82] Turing v0.13.0

Thanks!

Non-Contradiction commented 4 years ago

Thank you very much for the feedback! After some investigation, I thought the RMarkdown document problem might be related to some changes in knitr since version 1.28, and you are just using that version in R. I just pushed some possible fix to the master of JuliaCall on Github. You could install the master temporarily to see if the problem in RMarkdown document is solved. I will later prepare a new release of JuliaCall on CRAN with the fix.

donskerclass commented 4 years ago

After reinstalling from master branch (which also updated knitr and Rcpp, and one or two other libraries), the error goes away. Images do not display when code chunks are run interactively like when running R, but produce saved .png files. When the document is knit, it displays beautifully.

Thanks for attending to this so quickly!

Session Info R version 3.5.0 (2018-04-23) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS 10.15.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/3.5/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] gridExtra_2.3 zoo_1.8-6 lubridate_1.7.4 forcats_0.4.0 [5] stringr_1.4.0 dplyr_0.8.3 purrr_0.3.2 readr_1.3.1 [9] tidyr_0.8.3 tibble_3.0.1 ggplot2_3.3.0 tidyverse_1.2.1 [13] forecast_8.8 readstata13_0.9.2 foreign_0.8-72 JuliaCall_0.17.1.9000 loaded via a namespace (and not attached): [1] tseries_0.10-47 httr_1.4.1 splines_3.5.0 jsonlite_1.6.1 [5] modelr_0.1.5 StanHeaders_2.21.0-1 assertthat_0.2.1 TTR_0.23-4 [9] stats4_3.5.0 cellranger_1.1.0 yaml_2.2.1 pillar_1.4.4 [13] backports_1.1.6 lattice_0.20-38 glue_1.4.1 quadprog_1.5-7 [17] digest_0.6.25 rvest_0.3.4 colorspace_1.4-1 htmltools_0.3.6 [21] Matrix_1.2-17 timeDate_3043.102 pkgconfig_2.0.3 rstan_2.19.3 [25] broom_0.5.2 haven_2.1.1 scales_1.1.0 processx_3.4.2 [29] mgcv_1.8-28 generics_0.0.2 farver_2.0.3 ellipsis_0.3.1 [33] withr_2.1.2 urca_1.3-0 nnet_7.3-12 cli_2.0.2 [37] quantmod_0.4-15 magrittr_1.5 crayon_1.3.4 readxl_1.3.1 [41] evaluate_0.14 ps_1.3.2 fansi_0.4.1 nlme_3.1-141 [45] xts_0.11-2 xml2_1.2.2 pkgbuild_1.0.6 rsconnect_0.8.15 [49] tools_3.5.0 loo_2.2.0 prettyunits_1.1.1 hms_0.5.0 [53] lifecycle_0.2.0 matrixStats_0.56.0 munsell_0.5.0 callr_3.4.3 [57] packrat_0.5.0 compiler_3.5.0 rlang_0.4.6 grid_3.5.0 [61] rstudioapi_0.11 rmarkdown_2.1 labeling_0.3 gtable_0.3.0 [65] fracdiff_1.4-2 inline_0.3.15 curl_4.3 R6_2.4.1 [69] knitr_1.29 stringi_1.4.6 parallel_3.5.0 Rcpp_1.0.5 [73] vctrs_0.3.1 tidyselect_0.2.5 xfun_0.16 lmtest_0.9-37
Non-Contradiction commented 4 years ago

Thanks for the feedback! I just pushed more commits to master to fix the RStudio notebook display. You could check the master again to see if it works for the RStudio notebook. It works for me with RStudio 1.3.

donskerclass commented 4 years ago

I dowloaded and installed the new version, restarted R, and reran a file with the example above and now it works fine, displaying interactively in an .Rmd file in Rstudio with no problems. If I run

julia_command("plot(dd,ee)")

in the Console, it produces output

$stdout
NULL

$out
[1] "figure/-J13.png"
attr(,"class")
[1] "knit_image_paths" "knit_asis"       

attr(,"class")
[1] "JuliaOutput"

and the image is saved as a file (here -J13.png) but not displayed.

My version of RStudio is 1.2.5033; I should probably upgrade.

Session Info R version 3.5.0 (2018-04-23) Platform: x86_64-apple-darwin15.6.0 (64-bit) Running under: macOS 10.15.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/3.5/Resources/lib/libRlapack.dylib locale: [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8 attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] BMR_0.11.0 Rcpp_1.0.5 gridExtra_2.3 zoo_1.8-6 lubridate_1.7.4 [6] forcats_0.4.0 stringr_1.4.0 dplyr_0.8.3 purrr_0.3.2 readr_1.3.1 [11] tidyr_0.8.3 tibble_3.0.1 ggplot2_3.3.0 tidyverse_1.2.1 forecast_8.8 [16] readstata13_0.9.2 foreign_0.8-72 JuliaCall_0.17.1.9000 loaded via a namespace (and not attached): [1] tseries_0.10-47 httr_1.4.1 jsonlite_1.6.1 modelr_0.1.5 StanHeaders_2.21.0-1 [6] assertthat_0.2.1 TTR_0.23-4 stats4_3.5.0 cellranger_1.1.0 pillar_1.4.4 [11] backports_1.1.6 lattice_0.20-38 glue_1.4.1 quadprog_1.5-7 digest_0.6.25 [16] rvest_0.3.4 colorspace_1.4-1 timeDate_3043.102 pkgconfig_2.0.3 rstan_2.19.3 [21] broom_0.5.2 haven_2.1.1 scales_1.1.0 processx_3.4.2 generics_0.0.2 [26] farver_2.0.3 ellipsis_0.3.1 withr_2.1.2 urca_1.3-0 nnet_7.3-12 [31] cli_2.0.2 quantmod_0.4-15 magrittr_1.5 crayon_1.3.4 readxl_1.3.1 [36] ps_1.3.2 fansi_0.4.1 MASS_7.3-51.4 nlme_3.1-141 xts_0.11-2 [41] xml2_1.2.2 pkgbuild_1.0.6 tools_3.5.0 loo_2.2.0 prettyunits_1.1.1 [46] hms_0.5.0 lifecycle_0.2.0 matrixStats_0.56.0 munsell_0.5.0 callr_3.4.3 [51] packrat_0.5.0 isoband_0.2.1 compiler_3.5.0 rlang_0.4.6 grid_3.5.0 [56] rstudioapi_0.11 labeling_0.3 codetools_0.2-16 gtable_0.3.0 fracdiff_1.4-2 [61] inline_0.3.15 curl_4.3 R6_2.4.1 knitr_1.29 stringi_1.4.6 [66] parallel_3.5.0 vctrs_0.3.1 tidyselect_0.2.5 xfun_0.16 lmtest_0.9-37
Non-Contradiction commented 4 years ago

A function plotsViewer is to deal with the issue of plotting in the RStudio console. You could try

plotsViewer()
julia_command("plot(dd,ee)")

The solution is not perfect but may satisfy the need.

donskerclass commented 4 years ago

I hadn't seen the plotsViewer function, but I can confirm that in the version before your latest PR, it led to the same behavior (image is saved rather than displayed), and after the update it displays as intended, with no problems at all.

Thank you!