I have a ggplot2 plotting function as part of my code.
The function works fine when the file is sourced as R code, however when I include this function in an R package (and of course I include ggplot2 and scales both in the DESCRIPTION and in the NAMESPACE files of the package) I am getting the following error:
Error in initFields(scales = scales) :
could not find function "initRefFields"
The respective call of scales in the ggplot2 object is the following:
+ facet_wrap(~PV_Type, ncol = 1, scales = "free") +
I run the latest R (3.2.0) and the latest ggplot2 (1.0.1) installed from source.
A possible solution/explanation will be highly appreciated!
EDIT: The output of sessionInfo() is the following:
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)
locale:
[1] C/UTF-8/C/C/C/C
attached base packages:
[1] stats graphics grDevices utils datasets base
other attached packages:
[1] argparse_1.0.1 proto_0.3-10 greater_1.0
loaded via a namespace (and not attached):
[1] Rcpp_0.11.6 DEoptimR_1.0-2 RColorBrewer_1.1-2 plyr_1.8.2
[5] methods_3.2.0 class_7.3-12 prabclus_2.2-6 tools_3.2.0
[9] digest_0.6.8 mclust_5.0.1 gtable_0.1.2 lattice_0.20-31
[13] mvtnorm_1.0-2 findpython_1.0.1 gridExtra_0.9.1 trimcluster_0.1-2
[17] stringr_1.0.0 cluster_2.0.1 RGraphics_2.0-12 fpc_2.1-9
[21] stats4_3.2.0 diptest_0.75-6 grid_3.2.0 nnet_7.3-9
[25] getopt_1.20.0 robustbase_0.92-3 flexmix_2.3-13 pander_0.5.1
[29] ggplot2_1.0.1 reshape2_1.4.1 kernlab_0.9-20 magrittr_1.5
[33] scales_0.2.4 modeltools_0.2-21 MASS_7.3-40 colorspace_1.2-6
[37] stringi_0.4-1 munsell_0.4.2 rjson_0.2.15
The .libPaths() is also rather common for my installation:
>.libPaths()
[1] "/Library/Frameworks/R.framework/Versions/3.2/Resources/library"
Perhaps I should also add that I call the function which requires the scales package from a command line R script.
(This is not a duplicate of ggplot2 Error in initFields as I tried all their recommendations (apart from reverting back to R 2.14 of course) and none worked).
I think I have found a solution/workaround.
As I noticed from sessionInfo() the methods package was not attached but rather loaded in the namespace. Therefore after attaching methods (as well as grid which was needed for a consequent function) my script work fine by oading the functions from the package.
My working sessionInfo() looks like this now:
R version 3.2.0 (2015-04-16)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.3 (Yosemite)
locale:
[1] C/UTF-8/C/C/C/C
attached base packages:
[1] grid methods stats graphics grDevices utils datasets
[8] base
other attached packages:
[1] argparse_1.0.1 proto_0.3-10 greater_1.0
loaded via a namespace (and not attached):
[1] Rcpp_0.11.6 DEoptimR_1.0-2 RColorBrewer_1.1-2 plyr_1.8.2
[5] class_7.3-12 prabclus_2.2-6 tools_3.2.0 digest_0.6.8
[9] mclust_5.0.1 gtable_0.1.2 lattice_0.20-31 mvtnorm_1.0-2
[13] findpython_1.0.1 gridExtra_0.9.1 trimcluster_0.1-2 stringr_1.0.0
[17] cluster_2.0.1 RGraphics_2.0-12 fpc_2.1-9 stats4_3.2.0
[21] diptest_0.75-6 nnet_7.3-9 getopt_1.20.0 robustbase_0.92-3
[25] flexmix_2.3-13 pander_0.5.1 ggplot2_1.0.1 reshape2_1.4.1
[29] kernlab_0.9-20 magrittr_1.5 scales_0.2.4 modeltools_0.2-21
[33] MASS_7.3-40 colorspace_1.2-6 stringi_0.4-1 munsell_0.4.2
[37] rjson_0.2.15
Thanks very much for the contribution, however I would appreciate an explanation on firstly why there is the need to explicitly attach these two packages (methods and grid) when the functions are loaded as a package, and it is not required when the function are sourced by source().
Related
I have this pdf image of the Netherlands with dots:
pdf image of the netherlands with dots
When I try to convert this pdf to a png image, using pdftools and png like this:
library(png)
library(pdftools)
bitmap <- pdf_render_page("netherlands.pdf", page = 1, dpi = 300)
png::writePNG(bitmap, "netherlands.png"))
The result is this:
An image without the dots that were shown in the pdf version.
How can I solve this problem?
Here is my sessioninfo:
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2008 R2 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Dutch_Netherlands.1252 LC_CTYPE=Dutch_Netherlands.1252 LC_MONETARY=Dutch_Netherlands.1252
[4] LC_NUMERIC=C LC_TIME=Dutch_Netherlands.1252
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] png_0.1-7 pdftools_1.4 psych_1.6.9 psy_1.1 maptools_0.8-41
[6] rgeos_0.3-21 rgdal_1.2-5 sp_1.2-4 tagcloud_0.6 Rcpp_0.12.12
[11] wordcloud_2.5 directlabels_2015.12.16 extrafont_0.17 haven_1.0.0 tm_0.6-2
[16] NLP_0.1-9 qdap_2.2.5 RColorBrewer_1.1-2 qdapTools_1.3.1 qdapRegex_0.6.0
[21] qdapDictionaries_1.0.6 tidyr_0.6.1 dplyr_0.5.0 readxl_1.0.0 reshape2_1.4.2
[26] Hmisc_4.0-2 Formula_1.2-1 survival_2.40-1 lattice_0.20-34 knitr_1.15.1
[31] ggthemes_3.4.0 ggplot2_2.2.1 rmarkdown_1.4
loaded via a namespace (and not attached):
[1] splines_3.3.2 gender_0.5.1 gtools_3.5.0 assertthat_0.1 latticeExtra_0.6-28 xlsxjars_0.6.1
[7] cellranger_1.1.0 yaml_2.1.14 slam_0.1-40 Rttf2pt1_1.3.4 backports_1.0.4 quadprog_1.5-5
[13] extrafontdb_1.0 chron_2.3-48 digest_0.6.12 colorspace_1.3-2 htmltools_0.3.5 Matrix_1.2-7.1
[19] plyr_1.8.4 XML_3.98-1.5 scales_0.4.1 gdata_2.17.0 htmlTable_1.7 tibble_1.2
[25] openNLP_0.2-6 reports_0.1.4 nnet_7.3-12 lazyeval_0.2.0 mnormt_1.5-5 magrittr_1.5
[31] evaluate_0.10 foreign_0.8-67 tools_3.3.2 data.table_1.10.0 stringr_1.1.0 xlsx_0.5.7
[37] munsell_0.4.3 cluster_2.0.5 plotrix_3.6-4 RCurl_1.95-4.8 igraph_1.0.1 labeling_0.3
[43] bitops_1.0-6 base64enc_0.1-3 venneuler_1.1-0 gtable_0.2.0 DBI_0.5-1 R6_2.2.0
[49] gridExtra_2.2.1 openNLPdata_1.5.3-2 rprojroot_1.1 rJava_0.9-8 stringi_1.1.2 parallel_3.3.2
[55] rpart_4.1-10 acepack_1.4.1
update: The pdf renders fine on MacOS (see below). I think the problem is that the pdf file uses a font which not available on Windows. On Windows I see:
> pdf_fonts("~/../Downloads/netherlands_pdf.pdf")
Warning: error: Couldn't find a font for 'ZapfDingbats', subst is 'Helvetica'
name type embedded file
1 ZapfDingbats type1 FALSE C:\\Windows\\Fonts\\arial.ttf
When rendering the PDF I see:
> bitmap <- pdf_render_page("~/../Downloads/netherlands_pdf.pdf")
Warning: error: No display font for 'ArialNarrow'
Warning: error: No display font for 'ArialNarrow,Bold'
Warning: error: No display font for 'ArialNarrow,Italic'
Warning: error: No display font for 'ArialNarrow,BoldItalic'
Warning: error: No display font for 'ArialNarrow-Bold'
Warning: error: No display font for 'ArialNarrow-Italic'
Warning: error: No display font for 'ArialNarrow-BoldItalic'
Warning: error: No display font for 'HelveticaNarrow'
Warning: error: No display font for 'HelveticaNarrow,Bold'
Warning: error: No display font for 'HelveticaNarrow,Italic'
Warning: error: No display font for 'HelveticaNarrow,BoldItalic'
Warning: error: No display font for 'HelveticaNarrow-Bold'
Warning: error: No display font for 'HelveticaNarrow-Italic'
Warning: error: No display font for 'HelveticaNarrow-BoldItalic'
Warning: error: No display font for 'BookAntiqua'
Warning: error: No display font for 'BookAntiqua,Bold'
Warning: error: No display font for 'BookAntiqua,Italic'
Warning: error: No display font for 'BookAntiqua,BoldItalic'
Warning: error: No display font for 'BookAntiqua-Bold'
Warning: error: No display font for 'BookAntiqua-Italic'
Warning: error: No display font for 'BookAntiqua-BoldItalic'
Warning: error: No display font for 'ArialUnicode'
So I suspect that the dots are actually printed using a font (probably a dingbat) but they cannot be rendered if this font is not available.
On OSX there are no such warnings and the output seems correct:
However I am not sure why the pdf shows correctly in Chrome on Windows. Perhaps chromium includes extra fonts?
I'am trying to use package RODBCto access a mdb-file.
The mdb-file is created with ESRI GIS.
I want to change some entries in the attribute table.
If i try
sqlQuery(channnel = rddall, "SELECT* FROM abt_F;")
sqlFetch(channel=rddall,"txt_L ")
R crashes.
Screenshot
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=German_Austria.1252 LC_CTYPE=German_Austria.1252 LC_MONETARY=German_Austria.1252
[4] LC_NUMERIC=C LC_TIME=German_Austria.1252
attached base packages:
[1] splines grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] RODBC_1.3-12 brew_1.0-6 Hmisc_3.15-0 ggplot2_1.0.0 Formula_1.2-1 survival_2.37-7
[7] lattice_0.20-29 rgdal_1.1-8 sp_1.2-2
loaded via a namespace (and not attached):
[1] acepack_1.3-3.3 cluster_1.15.3 colorspace_1.2-5 digest_0.6.8 foreign_0.8-61
[6] gtable_0.1.2 latticeExtra_0.6-26 MASS_7.3-35 munsell_0.4.2 nnet_7.3-8
[11] plyr_1.8.3 proto_0.3-10 RColorBrewer_1.1-2 Rcpp_0.11.5 reshape2_1.4.1
[16] rpart_4.1-10 scales_0.2.4 stringr_0.6.2 tcltk_3.1.2 tools_3.1.2
Likely too late, but I just came across this question after encountering a similar problem. R was crashing for me when I tried to sqlFetch a table directly from my ArcGIS-created .mdb, and when I tried to fetch the table via a different non-GIS .mdb (I added the table to that database as an External Linked table, from the GIS .mdb). I finally got R to load the table correctly when I created a query in my non-GIS .mdb that basically replicated the initial table (although I dropped some of the unneeded columns in that query). Now, I just left the query as-is in my non-GIS .mdb, and sqlFetch the query, and it's working fine. Not really an R solution, but a potential workaround that may work for some folks.
I'm trying to use the bind_rows() function from dplyr (ver 0.4.1), but R command line can't find the function. It can find the help for the function, but any efforts to use the function are met with
> bind_rows()
Error: could not find function "bind_rows"
Am I doing something wrong? I understand bind_rows() was only introduced in ver 0.4.0, but I have a newer version than that.
Here is my sessionInfo()
> R version 3.1.1 (2014-07-10)
> Platform: x86_64-apple-darwin13.1.0 (64-bit)
>
> locale:
> [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
>
> attached base packages:
> [1] utils datasets graphics grDevices stats methods base
>
> other attached packages:
> [1] lubridate_1.3.3 tidyr_0.2.0 dplyr_0.4.1 swirl_2.2.16 mvoutlier_2.0.5 sgeostat_1.0-25
>
>loaded via a namespace (and not attached):
> [1] assertthat_0.1 colorspace_1.2-4 DEoptimR_1.0-1 digest_0.6.4 GGally_0.4.8
> [6] ggplot2_1.0.0 grid_3.1.1 gtable_0.1.2 httr_0.5 magrittr_1.0.1
>[11] MASS_7.3-34 memoise_0.2.1 munsell_0.4.2 mvtnorm_1.0-0 parallel_3.1.1
>[16] pcaPP_1.9-49 pls_2.4-3 plyr_1.8.1 proto_0.3-10 Rcpp_0.11.2
>[21] RCurl_1.95-4.3 reshape_0.8.5 reshape2_1.4 robCompositions_1.9.0 robustbase_0.91-1
>[26] rrcov_1.3-4 scales_0.2.4 stats4_3.1.1 stringr_0.6.2 tcltk_3.1.1
>[31] testthat_0.8.1 tools_3.1.1 yaml_2.1.13
Did you try to call it with the package prefix, e.g.
dplyr::bind_rows()
This should work even if there is a conflict with one of the other packages.
Closing all R sessions and re-installing dplyr worked in this situation. Thanks to David Arenburg for the prompt.
I am trying to generate a proper dendrogram in R with ggplot2 (more specifically ggdendro::ggdendrogram) because I would like to share it over plotly with my collaborators and currently I do not think it is possible with the api to generate a dendrogram with plotly.
I am mainly having troubles with the "axis" labeling when I rotate the dendrogram:
require(ggplot2)
require(ggdendro)
hc<-hclust(dist(iris[,1:4]),method="ward.D2")
ggd<-ggdendrogram(hc,rotate=TRUE)
ggd+scale_y_reverse()
This results in the following plot, where the tick labels are on the wrong side:
I would like to obtain a similar result as from R base graphics when executing plot(as.dendrogram(hc),horiz=TRUE)
As a bonus, I would like to be able to color the leaf (tick) labels separately as well as draw rect.hclust-like boxes, but this is not strictly necessary. I do know my way a little bit around ggplot2 syntax so no need to explain every possible solution in great length.
If relevant please find my sessionInfo() below, I am using R through Rstudio server on an Ubuntu 12.04 LTS server:
sessionInfo()
R version 3.1.0 (2014-04-10)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=C LC_COLLATE=C LC_MONETARY=C LC_MESSAGES=C LC_PAPER=C
[8] LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=C LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggdendro_0.1-14 cluster_1.15.2 plotly_0.4 RJSONIO_1.2-0.2 RCurl_1.95-4.1 bitops_1.0-6 devtools_1.5
[8] extrafont_0.16 RColorBrewer_1.0-5 scales_0.2.4 vegan_2.0-10 lattice_0.20-29 permute_0.8-3 splitstackshape_1.2.0
[15] data.table_1.9.2 BurStMisc_1.00 reshape2_1.4 xlsx_0.5.5 xlsxjars_0.6.0 rJava_0.9-6 ggplot2_0.9.3.1
[22] plyr_1.8.1
loaded via a namespace (and not attached):
[1] MASS_7.3-32 Rcpp_0.11.1 Rttf2pt1_1.3 colorspace_1.2-4 digest_0.6.4 evaluate_0.5.5 extrafontdb_1.0 formatR_0.10 grid_3.1.0
[10] gtable_0.1.2 httr_0.3 knitr_1.6 labeling_0.2 memoise_0.2.1 munsell_0.4.2 parallel_3.1.0 proto_0.3-10 stringr_0.6.2
[19] tools_3.1.0 whisker_0.3-2
If I include \usepackage{Sweavel} in my .rnw file, I get an X11 popup error "It seems you are using the Sweave-specific syntax; you may need Sweave2knitr("IPT-baseline-test.rnw") to convert it to knitr" when I compile in RStudio (Version 0.98.484). The document compiles, but I have to dismiss the error.
(1) Any ideas why \usepackage{Sweavel} triggers the error?
(2) Is there a way to turn off the popup since the document compiles anyway?
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
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] knitr_1.5
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 dichromat_2.0-0 digest_0.6.3 evaluate_0.5.1
[5] formatR_0.10 ggplot2_0.9.3.1 grid_3.0.2 gtable_0.1.2
[9] labeling_0.2 MASS_7.3-29 munsell_0.4.2 plyr_1.8
[13] proto_0.3-10 RColorBrewer_1.0-5 reshape2_1.2.2 scales_0.2.3
[17] stringr_0.6.2 tools_3.0.2
You shouldn't need to \usepackage{Sweavel} explicitly, I think -- knitr should handle that automatically. If you really want to suppress this false positive , you can rename Sweavel.sty to a file name that doesn't start with Sweave ... the which_sweave() function at https://github.com/yihui/knitr/blob/de7c65c58acfb1f3f5c0ac2f00b92cd2546be943/R/utils-sweave.R shows you what patterns knitr is looking for to detect "old Sweave syntax", specifically in this case the regular expression
regexp <-
'^\\s*\\\\(usepackage(\\[.*\\])?\\{Sweave|SweaveInput\\{|SweaveOpts\\{)'
So changing to mySweavel.sty should work ...
grepl(regexp,"\\usepackage{Sweave}") ## TRUE
grepl(regexp,"\\usepackage{Sweavel}") ## TRUE
grepl(regexp,"\\usepackage{mySweavel}") ## FALSE
My guess is that you have a newer version of knitr on your new than on your old machine, and it's trying harder to detect old Sweave syntax.
Removing the Sweave tag in the latex document prevented the warnings. It did not prevent the rendering of the file. Thus, the suggestion made in one of the comments above worked... knit2pdf (in my case) figures it out. - E