Problem using knitter with reticulate package in rstudio with matplotlib - r

To elaborate on the title,
I am using rstudio making an rmarkdown file that contains both r and python code. My configuration is below:
Running under: Windows 10 x64 (build 18363)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 rstudioapi_0.11 knitr_1.28 magrittr_1.5 rappdirs_0.3.1 tidyselect_1.0.0
[7] munsell_0.5.0 lattice_0.20-38 colorspace_1.4-1 R6_2.4.1 rlang_0.4.5 dplyr_0.8.5
[13] tools_3.6.3 grid_3.6.3 gtable_0.3.0 xfun_0.12 htmltools_0.4.0 yaml_2.2.1
[19] assertthat_0.2.1 digest_0.6.25 tibble_2.1.3 lifecycle_0.2.0 crayon_1.3.4 Matrix_1.2-18
[25] purrr_0.3.3 ggplot2_3.3.0 rsconnect_0.8.16 glue_1.3.2 evaluate_0.14 rmarkdown_2.1
[31] compiler_3.6.3 pillar_1.4.3 scales_1.1.0 jsonlite_1.6.1 reticulate_1.15-9000 pkgconfig_2.0.3
The python chunk I try running is:
import scipy as sp
import numpy as np
import pandas as pd
df = pd.read_csv("D:/03 PhD Edinburgh related/OneDrive/OneDrive - University of Edinburgh/00 PhD/000 PhD Data/01 Project I Chr Hansen/20200421_analysis_pp16013/20200420_pp16013_analysis.csv")
plt.scatter(df['OD'].values, df['osmolarity.mOSM'].values,
c=df['hydrophobicity'].values,cmap='magma')
plt.xlabel('OD')
plt.ylabel('osmolarity')
plt.title('osmolarity as function of OD and Hydrophobicity')
cbar = plt.colorbar()
cbar.set_label('Hydrophobicity', rotation=270)
plt.show()
I can run the python chunk fine in rStudio and display the graph as expect.
When I try using knitter to make the document it gets to the python chunk and gives the error:
"This application failed to start because it could not find or load
the Qt platform plugin "windows" in "",
reinstalling the application may fix this problem."
This is specifically related to the part using matplotlib. I can create and display the Pandas data frame.
I have tried installing the latest version of reticulate of github.
Thank you for any help in advance.

In my rmarkdown file, it works fine after setting the plugin path of the conda environment being used. You can add the below r code chunk before using matplotlib for python code chunk.
library(reticulate)
py_run_string("import os as os")
py_run_string("os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = 'C:/Users/[UserID]/Anaconda3/envs/[EnvironmentName]/Library/plugins/platforms'")

I have managed to find the issue in another stack overflow answer this is a duplicate.
For answer please look here.
https://stackoverflow.com/a/50711837/11076186
I basically just save my python plot and then load the saved python plot in r.

Related

shiny runs undesirable scripts on R folder

This issue shook me. Lets a minimal reproducible example:
On RStudio, create a New Project... Choose Shiny Web App. Check that sample app runs well.
Create inside the standard R folder, to put all your *.R scripts.
Create a dummy script inside the R folder that fails, but you do not want run yet. For example
str(do_not_exist) ## foo.R
4.Run the App again. Now, the App fails
> runApp()
Error in str(do_not_exist) : objeto 'do_not_exist' no encontrado
Why Shiny runs undesirable scripts? They are OUT of the root directory.
I am not finding ways to save scripts that are not functions within the project.
This is my R Session
> sessionInfo()
R version 4.0.5 (2021-03-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:
[1] LC_COLLATE=Spanish_Argentina.1252 LC_CTYPE=Spanish_Argentina.1252
[3] LC_MONETARY=Spanish_Argentina.1252 LC_NUMERIC=C
[5] LC_TIME=Spanish_Argentina.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] tidyr_1.1.3 dplyr_1.0.5 openxlsx_4.2.3.9000 shiny_1.6.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 magrittr_2.0.1 tidyselect_1.1.0 xtable_1.8-4
[5] R6_2.5.0 rlang_0.4.10 fastmap_1.1.0 fansi_0.4.2
[9] tools_4.0.5 utf8_1.2.1 DBI_1.1.1 withr_2.4.2
[13] htmltools_0.5.1.1 ellipsis_0.3.1 assertthat_0.2.1 digest_0.6.27
[17] tibble_3.1.1 lifecycle_1.0.0 crayon_1.4.1 zip_2.1.1
[21] purrr_0.3.4 later_1.1.0.1 vctrs_0.3.7 promises_1.2.0.1
[25] glue_1.4.2 mime_0.10 stringi_1.5.3 compiler_4.0.5
[29] pillar_1.6.0 generics_0.1.0 httpuv_1.5.5 pkgconfig_2.0.3
See here. One option is options(shiny.autoload.r = FALSE).

Error in if (num_colors <256) when importing csv files?

I recently updated to R 4.03 and since doing so I have had difficulty uploading csv files in R studio. This is true whether trying to import with code or when manually importing using the tab Environment -> "Import Dataset." The error message I receive is "Error in if (num_colors < 256) { : missing value where TRUE/FALSE needed."
Here is the code I am using:
library(readr)
df <- read_csv("df.csv")
Error in if (num_colors < 256) { : missing value where TRUE/FALSE needed`
I thought this may have something to do with the package "crayon" but loading this library does not seem to make a difference.
Furthermore, when I save "df.csv" as an excel file "df.xlsx" I am able to load the dataset without issue using the following code
library(readxl)
df <- read_excel(df.xlsx)
I don't encounter this problem when using R 4.0.2 on a different machine, so I suspect this issue may be related to the upgrade.
Any insights are appreciated.
Update:
Newish R user and I appreciate everyone's interest in this question. I believe the session is clean. Per Ben's request:
find("read_csv")
[1] "package:readr"
sessionInfo()
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_1.0.3 crayon_1.4.1 readxl_1.3.1 plyr_1.8.6 readr_1.4.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.6 rstudioapi_0.13 magrittr_2.0.1 hms_1.0.0 tidyselect_1.1.0 lattice_0.20-41 R6_2.5.0 rlang_0.4.10 fansi_0.4.2
[10] tools_4.0.3 grid_4.0.3 utf8_1.1.4 DBI_1.1.1 cli_2.3.0 ellipsis_0.3.1 assertthat_0.2.1 tibble_3.0.5 lifecycle_0.2.0
[19] Matrix_1.2-18 purrr_0.3.4 vctrs_0.3.6 glue_1.4.2 compiler_4.0.3 pillar_1.4.7 cellranger_1.1.0 generics_0.1.0 pkgconfig_2.0.3
I'd suggest updating RStudio to the latest version as well as R.
That did the trick for me.
Edit
In view of Ben's request below.
R version: 4.0.4;
RStudio version: 1.4.1103

save_kable does not work after installing R v4.0.2

I have successfully used save_kable() from kableExtra to export latex tables into *.png before. However, after updating R to the version 4.0.2, save_kable always ends with R fatal error (no specific error message is shown, the session is just aborted). I installed a clean version o R 3.6.3 and everything works fine again.
Next, I re-installed MikTEX, but this does not seem to be the problem because R Markdown can knit a .pdf file without an issue (in the 4.0.2 version). Also, using save_kable with "html" options works fine and exports the table into .png. The problem seems to be specific for latex.
Looking at sessionInfo(), the only difference is that R 3.6.3 automatically loads the 'magick' package, while R 4.0.2 does not. However, loading the magick package manually in the latter R version does not solve the problem. Then, of course, the sessions differ in the "compiler" package versions. Maybe this is the source of the problem?
Any help would be much appreciated. The problem can be reproduced with minimal code such as
kable(mtcars, "latex") %>% save_kable("test.png")
Here is the sessionInfo
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
Random number generation:
RNG: Mersenne-Twister
Normal: Inversion
Sample: Rounding
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] magick_2.4.0 kableExtra_1.1.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.5 rstudioapi_0.11 knitr_1.29 xml2_1.3.2 magrittr_1.5
[6] hms_0.5.3 rvest_0.3.5 munsell_0.5.0 viridisLite_0.3.0 colorspace_1.4-1
[11] R6_2.4.1 rlang_0.4.7 stringr_1.4.0 httr_1.4.1 tools_4.0.2
[16] webshot_0.5.2 xfun_0.15 htmltools_0.5.0 ellipsis_0.3.1 digest_0.6.25
[21] tibble_3.0.3 lifecycle_0.2.0 crayon_1.3.4 readr_1.3.1 vctrs_0.3.2
[26] glue_1.4.1 evaluate_0.14 rmarkdown_2.3 stringi_1.4.6 compiler_4.0.2
[31] pillar_1.4.6 scales_1.1.1 pkgconfig_2.0.3
This seems to be a windows specific problem. I actually just asked a recent question here
kableExtra HTML styling in Rmarkdown and kable_save()
Some of the things I have seen mentioned as potential fixes are listed there such as updating ghostscript, not using tinytext, updating some latex packages including 'standalone', and editing the policy.xls document in ImageMagick ImageMagick security policy 'PDF' blocking conversion.
None of these worked for me and I get the same crashing issues the second I try to kable_save a latex format into png. However, some other Windows users have found success with the steps above so maybe try that out and see how it goes.

Problem in importing .biom file from the Earth Microbiome Project (release1) in R

I want to import a .biom file in R that I've dowloaded from the ftp server - the Earth Microbiome Project (release 1).
The file comes from the following link: ftp://ftp.microbio.me/emp/release1/otu_tables/closed_ref_silva/
I tried with several of these files but the one I want to import in R (studio) is the first one: 'emp_cr_silva_16S_123.qc_filtered.biom' (293MB)
I've tried several things:
I tried to open it with both the phyloseq::import_biome and the bioformat::read_biom functions :
emp<-import_biom(BIOMfilename = biom.file)
I got the following Error message:
Both attempts to read input file:
E:/Path/to/my/data/EMP_data/emp_cr_silva_16S_123.qc_filtered.biom
either as JSON (BIOM-v1) or HDF5 (BIOM-v2).
Check file path, file name, file itself, then try again.
I then checked the file path and name with the is_file function of the 'fs' package.
Then I checked the .biom file I want to import, openning this .biom file with Notepad shows strange characters (sorry but I am not familiar with developing) such as:
ƒOB§ß;}]0v(ÿQ<ãï8 #OÅ+q8‚´'Ž;º‹ë®Ü-¯§-‡ùP
I had a look at other biom files I have but none of them look like this. I try to open these other files with the same function and it works.
I tried to obtain this file from other repositories (https://zenodo.org/record/890000) but have a similar problem.
The problem most likely comes from the file format but I don't know how to deal with this.
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] biomformat_1.8.0 phyloseq_1.24.2 fs_1.3.1
loaded via a namespace (and not attached):
[1] Rcpp_1.0.2 bindr_0.1.1 compiler_3.5.0 pillar_1.4.2
[5] plyr_1.8.4 XVector_0.20.0 iterators_1.0.9 tools_3.5.0
[9] zlibbioc_1.26.0 jsonlite_1.6 tibble_2.1.3 nlme_3.1-137
[13] rhdf5_2.24.0 gtable_0.2.0 lattice_0.20-35 mgcv_1.8-24
[17] pkgconfig_2.0.2 rlang_0.4.0 igraph_1.2.4.1 Matrix_1.2-14
[21] foreach_1.4.4 rstudioapi_0.10 yaml_2.1.19 parallel_3.5.0
[25] bindrcpp_0.2.2 dplyr_0.7.6 stringr_1.3.1 cluster_2.0.7-1
[29] Biostrings_2.48.0 S4Vectors_0.20.1 IRanges_2.14.10 multtest_2.36.0
[33] tidyselect_0.2.5 stats4_3.5.0 ade4_1.7-11 grid_3.5.0
[37] glue_1.3.1 Biobase_2.40.0 data.table_1.12.2 R6_2.4.0
[41] survival_2.42-3 purrr_0.2.5 reshape2_1.4.3 Rhdf5lib_1.2.1
[45] ggplot2_3.2.0 magrittr_1.5 splines_3.5.0 scales_1.0.0
[49] codetools_0.2-15 MASS_7.3-50 BiocGenerics_0.26.0 assertthat_0.2.0
[53] permute_0.9-4 ape_5.1 colorspace_1.4-1 stringi_1.1.7
[57] lazyeval_0.2.1 munsell_0.5.0 vegan_2.5-5 crayon_1.3.4 ```
The .biom file is encrypted, complessed or mislabled, so I don't think you will be able to use it this way.
A usable version should look like examples given here:
http://biom-format.org/documentation/format_versions/biom-1.0.html
You could ask the owners of the file if they could provide you with a correct version.

Autoplot error when using ggcyto (Bioconductor) package: 'ggplot.data.frame' not found

I am running R-3.5.0 and RStudio 1.1.423 on Windows 7. I had to use our corporate installer for both programs, so I did not download the installer .exe files and am also limited in having the most recent version of the software.
I am trying to use the Bioconductor project FlowCore and ggcyto package to plot flow cytometry events. In the beginning this worked quite well, but recently (and I have no idea if I even changed anything) I cannot plot my flowframes using autoplot. Here's the example from the ggcyto documentation:
library(flowCore)
library(ggcyto)
data(GvHD)
fs <- GvHD[subset(pData(GvHD), Patient %in%5:7 & Visit %in% c(5:6))[["name"]]]
autoplot(fs, x = "SSC-H")
which results in
Error in get(name, envir = asNamespace(pkg), inherits = FALSE) :
object 'ggplot.data.frame' not found
I noticed one strange thing: I cannot update the stringi package to the latest (I believe 1.2.3) version, only to 1.1.7.
It is suggested in this thread: R gives strange error with ggplot2 expression: object 'rversion' not found
that it is an RStudio problem. However, when running the same code in the R console without RStudio, I get the same error message.
Any help is greatly appreciated!
I found the source of the problem. Apparently I must have updated my packages and the latest ggplot2 release does not interact well with the ggcyto package.
A downgrade of ggplot2 to version 2.2.1 fixed it.
I ran into the exact same problem today running the most up-to-date R binaries and Rstudio version. I ended up using the package "flowViz" and the function: flowplot() to receive essentially the same result (just without the binning) as the autoplot() function of the "ggcyto" package utilized in the FlowCore Vignette. I hope this helps.
I am happy to report that this no longer fails in more recent R/BioC environments such as:
sessionInfo()
R version 3.5.2 (2018-12-20)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)
Matrix products: default
BLAS: /n/apps/CentOS7/install/r-3.5.2/lib64/R/lib/libRblas.so
LAPACK: /n/apps/CentOS7/install/r-3.5.2/lib64/R/lib/libRlapack.so
locale:
[1] C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggcyto_1.10.2 flowWorkspace_3.30.2 ncdfFlow_2.28.1 BH_1.69.0-1 RcppArmadillo_0.9.400.3.0 ggplot2_3.1.1 flowCore_1.48.1 usethis_1.5.0 devtools_2.0.2
loaded via a namespace (and not attached):
[1] Rcpp_1.0.1 mvtnorm_1.0-10 lattice_0.20-38 corpcor_1.6.9 prettyunits_1.0.2 ps_1.3.0 assertthat_0.2.1 rprojroot_1.3-2 digest_0.6.18 R6_2.4.0 plyr_1.8.4 backports_1.1.4 stats4_3.5.2 pcaPP_1.9-73 pillar_1.4.0 zlibbioc_1.28.0 rlang_0.3.4 lazyeval_0.2.2 data.table_1.12.2 Rgraphviz_2.26.0 callr_3.2.0 hexbin_1.27.3 labeling_0.3 desc_1.2.0 stringr_1.4.0 munsell_0.5.0 compiler_3.5.2 pkgconfig_2.0.2 BiocGenerics_0.28.0
[30] pkgbuild_1.0.3 IDPmisc_1.1.19 tidyselect_0.2.5 gridExtra_2.3 tibble_2.1.1 matrixStats_0.54.0 XML_3.98-1.19 flowViz_1.46.1 rrcov_1.4-7 crayon_1.3.4 dplyr_0.8.1 withr_2.1.2 MASS_7.3-51.4 grid_3.5.2 gtable_0.3.0 magrittr_1.5 scales_1.0.0 graph_1.60.0 KernSmooth_2.23-15 stringi_1.4.3 cli_1.1.0 fs_1.3.1 remotes_2.0.4 testthat_2.1.1 latticeExtra_0.6-28 robustbase_0.93-5 RColorBrewer_1.1-2 tools_3.5.2 Biobase_2.42.0
[59] glue_1.3.1 DEoptimR_1.0-8 purrr_0.3.2 processx_3.3.1 pkgload_1.0.2 parallel_3.5.2 colorspace_1.4-1 cluster_2.0.9 sessioninfo_1.1.1 memoise_1.1.0

Resources