Rstudio crushing when calling rgl and knitr in ioslides presentation - r

My RStudio session is crushing when trying to draw an rgl 3d plot using knitr in ioslides html presentation. Any ideas how to prevent it?
My chunk:
```{r setup, results='asis'}
library(knitr)
library(rgl)
knit_hooks$set(webgl = hook_webgl)
```
```{R, testgl, webgl=TRUE, cache=FALSE}
x <- sort(rnorm(1000))
y <- rnorm(1000)
z <- rnorm(1000) + atan2(x,y)
plot3d(x, y, z, col=rainbow(1000))
```
Edit
This error occurs for:
> sessionInfo()
R version 3.2.1 (2015-06-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] 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] rsconnect_0.4.1.4 htmltools_0.2.6 tools_3.2.1 yaml_2.1.13 rmarkdown_0.7 digest_0.6.8
>
Source code for the presentation can be found here:
https://github.com/TARF/SMM/tree/master/Presentation
Please note that in order to run the code one needs to connect to Human Mortality Database via Demography package with own credentials.

You probably need to update RStudio. I had it crash for a while, I think because their WebGL implementation wasn't working; now it runs fine (though it doesn't display WebGL, I have to view the output in my browser for that).
Edited a couple of years later: I haven't seen a crash in a long time, and rgl (displayed by calling rglwidget()) now displays fine in RStudio.

Related

Package-specific utils::news() not working in RStudio

I'm trying to bring up news for a package while in RStudio. While the default utils::news() works for generating the base R changelog in the built-in viewer, I can't get it to work for a specific package; it throws an error. The function works fine for specific packages in RGui.
Fresh R session in RStudio 1.2.1335:
news() # this works
news(package = "ggplot2") # this doesn't
Error that I get in viewer: Error in UseMethod("toHTML") : no applicable method for 'toHTML' applied to an object of class "NULL"
Fresh R session in RGui:
news()
news(package = "ggplot2") # both work perfectly
Session info:
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17134)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.6.0 tools_3.6.0 Rcpp_1.0.1 xml2_1.2.0 commonmark_1.7
How can I get the function to output without error in RStudio? If this is not possible, how can I run the function in RStudio but tell it to view the HTML outside of the viewer, e.g., in the browser like RGui?
This looks like an RStudio bug, so probably the best action is to report it to them. As a workaround, you can avoid using their built-in browser by changing the setting for options("browser").
For example, on a Mac outside of RStudio I see
options("browser")
# $browser
# [1] "/usr/bin/open"
and in RStudio running
options(browser = "/usr/bin/open")
disables the built-in browser. I don't know what it defaults to in RGui on Windows, but setting it to the same in RStudio as it is in RGui should get it to work.
Unfortunately, this disables it for everything, not just for news(), so you probably want something like this instead:
save <- options(browser = "/usr/bin/open")
news(package = "ggplot2")
options(save)

Running code one line at a time breaks inline chunk output

When I run a complete chunk of code, inline output appears as expected. However if I run the same chunk one line at a time it breaks if I use more than one plot call. So:
set.seed(129485)
x=1:10
y=rnorm(length(x), x, 1)
plot(x,y)
lines(x,y)
works fine in an R script. If I insert it into a code chunk in a brand new R markdown file, like this:
---
title: "wtf"
author: "Drew Tyre"
date: "December 22, 2016"
output: html_document
---
```{r}
set.seed(129485)
x=1:10
y=rnorm(length(x), x, 1)
plot(x,y)
lines(x,y)
```
This chunk runs if I run the entire chunk using control-shift-enter, the play button, or by selecting all the lines and using control-enter. However, if I run the lines one at a time using control-enter, it breaks on the call to lines(x,y):
Error in plot.xy(xy.coords(x, y), type = type, ...) : plot.new has not been called yet
It goes away if I set the options to chunk output in console. I guess it could be the intended behavior, but then maybe control-enter should do the same thing as control-shift-enter when using inline output?
I'm using RStudio 1.0.44 and
R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets
[6] methods base
loaded via a namespace (and not attached):
[1] rsconnect_0.4.3 tools_3.3.2 yaml_2.1.14
[4] knitr_1.15.1
I can also reproduce this behavior on this machine:
R version 3.2.3 (2015-12-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] tools_3.2.3 knitr_1.12.3
Kevin Ushey over at the RStudio community forum had this answer:
This is unfortunately a consequence of the Notebook execution model. The graphics device state is reset after each execution, so code execution that is done line-by-line (e.g. to fill a plot) will not work as expected.
There are some workarounds for this:
Prefer executing a whole chunk at a time, using e.g. Cmd + Shift + Enter,
Wrap your plot generating code in brackets {},
Define a function that generates the entire plot,
Disable inline chunk output for that document. (Do this by clicking the Gear icon in the source toolbar and choosing 'Chunk Output in Console')

R 3.3.1 crashing with dplyr

I have recently updated RStudio to 3.3.1, and since then, when I run the following code using dplyr, R tells me "This application has requested the Runtime to terminate it in an unusual way".
max_trop <- df %>% group_by(record_id) %>% summarise(max= max(troponin, na.rm=TRUE))
Would anyone be able to tell me if I'm doing something particularly stupid? It used to work fine...
Many thanks
sessionInfo()
R version 3.3.1 (2016-06-21)Platform: i386-w64-mingw32/i386 (32-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:[1] LC_COLLATE=English_United Kingdom.1252
LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dplyr_0.5.0
loaded via a namespace (and not attached):
[1] magrittr_1.5 R6_2.1.2 assertthat_0.1 DBI_0.4-1
tools_3.3.1 tibble_1.1 Rcpp_0.12.6
This can be caused by getting the wrong variable names in a dplyr chain. The solution is to check what you're providing in the variables exists in your data set.
data("mtcars")
library(dplyr)
# Runs
mtcars %>% summarise(mean(mpg))
# crashes with message 'This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.'
mtcars %>% summarise(mean(mag))

Exporting graphics with Unicode in R using png() or Cairo(), on Windows

I am able to display Unicode characters on a plot within my R session, but when I try to export the figure using e.g. png(), CairoPNG(), cairo_pdf(), etc., some (although not all) of these fail to appear. For example, the following simple plot shows the correct characters for me in the R window:
plot( 1:9,
c(280, 540, 750, 1434, 1078, 728, 662, 606, 785),
pch=-as.hexmode(c("4E00", "4E8C", "4E09", "56DB", "4E94", "516D", "4E03", "516B", "4E5D")),
xlab="Digit",
ylab="pixels"
)
However, when I use png() first to try to save this image to disk, the characters are replaced with boxes. The previous discussions on this I managed to find (e.g. here and here) suggested using cairo_pdf. But I get basically the same result with cairo_pdf(), CairoPNG(), cairo_ps(), etc. Changing the locale, as suggested here, also still got the same result.
Some session info:
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 8 x64 (build 9200)
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] yarrr_0.1 lme4_1.1-8 Matrix_1.2-2 boot_1.3-17 Cairo_1.5-9
loaded via a namespace (and not attached):
[1] minqa_1.2.4 MASS_7.3-43 tools_3.2.2 Rcpp_0.12.4 splines_3.2.2
[6] nlme_3.1-121 grid_3.2.2 nloptr_1.0.4 lattice_0.20-33
Any suggestions would be greatly appreciated!
EDIT: Fixed a mistake in the code I had posted, sorry about that; you should hopefully be able to replicate the plot now.

Newest raster package won't plot a raster object in Windows

I'm trying to plot a raster using the "raster" package but I'm running against an error
Here's my code
map = raster("A055E.tif")
plot(map)
Error in `colnames<-`(`*tmp*`, value = "A055E") :
length of 'dimnames' [2] not equal to array extent
sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: i386-w64-mingw32/i386 (32-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] rgdal_0.9-1 raster_2.3-0 sp_1.0-15
loaded via a namespace (and not attached):
[1] grid_3.1.1 lattice_0.20-29 tools_3.1.1
However, when I run it on a linux machine with the following versions:
R version 3.1.1 (2014-07-10)
Platform: i686-pc-linux-gnu (32-bit),
raster_2.2-31 sp_1.0-15)
it doesn't give me an issue
I think that if you update the raster package things will work again.
On windows computer I had similar problem - I couldn't plot 3-band raster map using plotRGB(). The solution was to handly remove package 'raster' and just install it again.

Resources