Save R Cairo graphics on windows - r

I've just found out that I can get antialiased R graphics on Windows via Cairo so I've installed the package right away. Unfortunately, though, I'm stuck trying to save the output.
I can't seem to copy it and right click doesn't do anything.
Ideas?

This should work (it does for me using R 2.14 under linux):
cairo_pdf("spam.pdf")
plot(1:10)
dev.off()
and the resulting pdf is in the current work directory. Alternatively, when having the cairo package installed, you can use:
CairoPDF("spam.pdf")
plot(1:10)
dev.off()
to get the same effect.
Information on my system:
> sessionInfo()
R version 2.14.1 (2011-12-22)
Platform: i686-pc-linux-gnu (32-bit)
locale:
[1] LC_CTYPE=en_US.utf8 LC_NUMERIC=C
[3] LC_TIME=en_US.utf8 LC_COLLATE=en_US.utf8
[5] LC_MONETARY=en_US.utf8 LC_MESSAGES=en_US.utf8
[7] LC_PAPER=C LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.utf8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] fortunes_1.4-2
loaded via a namespace (and not attached):
[1] tools_2.14.1

The Cairo functions are now included in most R distributions. All you need to do is just specify type="cairo":
png(filename="fn.png",
type="cairo",
units="in",
width=5,
height=4,
pointsize=12,
res=96)
...
dev.off()
I wrote a blog-post about the Cairo packages and found out about the new feature through a comment :-)

Related

Show Chinese character in ggplot2

I just setup a Ubuntu 14.04 web server and change locale to zh_CN.UTF-8. However ggplot2 cannot show the Chinese characters in labels.
library(ggplot2)
x_lab <- enc2utf8('中国')
p <- ggplot(cars) + geom_point(aes(speed, dist)) +
xlab(x_lab)
png('a.png')
print(p)
dev.off()
The results look like this
Any idea to fix this problem?
PS: I have another web server with the same locale, but work fine to me.
This is my sessionInfo
> sessionInfo()
R version 3.3.1 (2016-06-21)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS
locale:
[1] LC_CTYPE=zh_CN.UTF-8 LC_NUMERIC=C
[3] LC_TIME=zh_CN.UTF-8 LC_COLLATE=zh_CN.UTF-8
[5] LC_MONETARY=zh_CN.UTF-8 LC_MESSAGES=zh_CN.UTF-8
[7] LC_PAPER=zh_CN.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=zh_CN.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_2.1.0
loaded via a namespace (and not attached):
[1] colorspace_1.2-6 scales_0.4.0 plyr_1.8.4 gtable_0.2.0
[5] Rcpp_0.12.6 grid_3.3.1 munsell_0.4.3
Figure out this problem. I need to install Chinese fonts in the new server, then roboot:
sudo apt-get install fonts-wqy-zenhei
sudo apt-get install fonts-arphic-bkai00mp fonts-arphic-bsmi00lp fonts-arphic-gbsn00lp fonts-arphic-gkai00mp fonts-arphic-ukai fonts-arphic-uming fonts-cns11643-kai fonts-cns11643-sung fonts-cwtex-fs fonts-cwtex-heib fonts-cwtex-kai fonts-cwtex-ming fonts-cwtex-yen
See this page: https://askubuntu.com/questions/490829/how-can-i-install-chinese-fonts-on-kubuntu-14-04

Error while using rSymPy

I recently installed rSymPy to perform some symbolic calculations in R. This step also installed some dependencies (“rJava”, “rjson” and “rJython”). After the installation was complete, I loaded the library and ran a very simple command which gave me an error:
> library("rSymPy")
Loading required package: rJython
Loading required package: rJava
Loading required package: rjson
> x <- Var("x")
Error in .jcheck() : No running JVM detected. Maybe .jinit() would help.
Here is my sessionInfo(), just in case you need it:
> sessionInfo()
R version 3.2.2 (2015-08-14)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS release 6.7 (Final)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rSymPy_0.2-1.1 rJython_0.0-4 rjson_0.2.15 rJava_0.9-8
loaded via a namespace (and not attached):
[1] tools_3.2.2 tcltk_3.2.2
I have tried to install different versions of required packages, but I always get this error. I would really appreciate any help.
Thanks,
Aashish
Thanks to Gabor Grothendieck (developer of rSymPy) for pointing out that sometimes one needs to manually initialize JVM. One can simply use "sympyStart()" at the beginning of the code, and that fixes the error.

ggplot: object 'DiscreteRange' not found

This is embarrassing. I've been using the ggplot2 package on a regular basis over the past few months, and now suddenly things seem to go seriously wrong.
Apparently, whenever I try to plot a bar graph I obtain the error:
Error in structure(list(call = match.call(), aesthetics = aesthetics, :
object 'DiscreteRange' not found
This is the error that results, e.g., when I try to run a code as simple as the following:
library(ggplot2)
ggplot(diamonds, aes(x=cut)) + geom_bar()
There is no graphics output, the code stops with the above error message.
If I recall correctly this used to work up to a few days ago, so I suspect that there is some error in an (update of) another package that interferes. Of course, I may also be doing a very stupid mistake that I don't see.
I'm using RStudio version 0.99.467; more information concerning the system and the relevant packages is provided below:
> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.3 LTS
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggplot2_1.0.1
loaded via a namespace (and not attached):
[1] Rcpp_0.12.0 lattice_0.20-33 zoo_1.7-11 digest_0.6.8 MASS_7.3-43 grid_3.2.1 plyr_1.8.3 gtable_0.1.2
[9] magrittr_1.5 scales_0.2.5.9003 stringi_0.5-5 reshape2_1.4.1 proto_0.3-10 blotter_0.9.1666 tools_3.2.1 stringr_1.0.0
[17] munsell_0.4.2 colorspace_1.2-6 quantstrat_0.9.1669
I tried to restart a fresh R session and even rebooted the computer, to no avail.
Thank you very much for your help!
I'm going to take a guess that the problem is with the scales package, which is at version: 0.2.5.9003. The packages with "four-part" version numbers x.y.z.wwww look like development versions to me. The current CRAN version is 0.2.5:
a1 <- available.packages()
a1[a1[,"Package"]=="scales","Version"] ## 0.2.5
I don't know how you ended up with a development version (devtools::install_github("hadley/scales") would probably do it) but I would install.packages("scales") (or the equivalent through the RStudio GUI) to re-install the CRAN version and see if that resolves the problem.
Alternatively, you could try keeping the development version of scales and moving to the development version of ggplot2 (devtools::install_github("hadley/ggplot2")).
I'm using the development version of ggplot2 (1.0.1.9003) with the development version of scales (0.2.5.9003) and your example works fine.

reading large file into R

I am trying to read a large space-delimited file (14Gb) of 49,376 rows and 73,625 columns into R for analysis.
I have tried using fread from the data.table package, as suggested here.
I receive the error
Error: segfault from C stack overflow
Is there another approach that could be used here? Any other packages or some kind of work around for this error? My R session info is below.
R version 3.0.2 (2013-09-25)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US LC_NUMERIC=C LC_TIME=en_US
[4] LC_COLLATE=en_US LC_MONETARY=en_US LC_MESSAGES=en_US
[7] LC_PAPER=en_US LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.9.4
loaded via a namespace (and not attached):
[1] chron_2.3-45 tools_3.0.2
The error was occurring due to insufficient memory. Once I increased the memory limit, fread worked as expected.

difficulties with installing/using Cairo / R 2.12.1

I have compiled R version 2.12.1 (2010-12-16) for my system (CentOS), and afterwards I installed Cairo_1.4-5.
I'd like use Cairo to produce PNG (and maybe PDF) output of my graphs when I batch-invoke my scripts, but this gives me difficulties when I am using X (I am tunneling X through ssh and it often disconnects while the R scripts are running...), so this is why I want to use Cairo.
(I do not have root access btw).
Although it's installed according to sessionInfo
> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: x86_64-unknown-linux-gnu (64-bit)
locale:
[1] C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
I can not call CairoPDF:
> CairoPDF()
Error: could not find function "CairoPDF"
Weirdly, I can invoke a function called cairo_pdf
> cairo_pdf()
>
I can not call CairoPNG, cairo_PNG or cairo_png:
> CairoPNG()
Error: could not find function "CairoPNG"
> cairo_PNG()
Error: could not find function "cairo_PNG"
> cairo_png()
Error: could not find function "cairo_png"
Is something wrong withmy installation? Frankly I have no idea how to proceed from here, why can't I even call CairoPNG()?
On Ubuntu I have:
> sessionInfo()
R version 2.12.1 (2010-12-16)
Platform: i486-pc-linux-gnu (32-bit)
locale:
[1] LC_CTYPE=af_ZA.utf8 LC_NUMERIC=C
[3] LC_TIME=af_ZA.utf8 LC_COLLATE=af_ZA.utf8
[5] LC_MONETARY=C LC_MESSAGES=af_ZA.utf8
[7] LC_PAPER=af_ZA.utf8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=af_ZA.utf8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
Then run library(Cairo), if this is not installed
library(Cairo)
Error in library(Cairo) : there is no package called 'Cairo'
This means you will have to install the Cairo R package, which interfaces with the Cairo graphics system
> install.packages('Cairo')
It will download, build and install the package - you don't need root for this
If it was successful, you can run
>library(Cairo)
>sessionInfo()
R version 2.12.1 (2010-12-16)<br>
Platform: i486-pc-linux-gnu (32-bit)
locale:
[1] LC_CTYPE=af_ZA.utf8 LC_NUMERIC=C
[3] LC_TIME=af_ZA.utf8 LC_COLLATE=af_ZA.utf8
[5] LC_MONETARY=C LC_MESSAGES=af_ZA.utf8
[7] LC_PAPER=af_ZA.utf8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=af_ZA.utf8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] Cairo_1.4-5
HTH
This should do:
install.packages("Cairo")
library(Cairo)

Resources