Running code one line at a time breaks inline chunk output - r

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')

Related

Rstudio snippet editor glitching, making it extremely difficult to edit snippets

I'm having issues with Rstudio, where I'm trying to edit my code snippets, but when I go to Tools -> Global Options -> Code -> Edit Snippets.., the snippet editor opens up with the window behind the global options pop-up, while the text for the editor shows up in front of the global options pop up. I'm using R version 4.0.2, and I've tried deleting and re-downloading R and R studio, as well as restarting my session. I'll include a picture of my issue:
also, here's my session info
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)
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
loaded via a namespace (and not attached):
[1] compiler_4.0.2 rsconnect_0.8.16 tools_4.0.2 yaml_2.2.1
Setting rendering engine to 'Desktop OpenGL' solved this for me.

Can't get the colon separator between figure number and caption in bookdown

I have this in my _bookdown.yml for a word document I'm trying to render with bookdown. Even if don't create a _bookdown.yml file, I still don't get a ":" separator.
delete_merged_file: true
language:
label:
fig: 'Figuee '
tab: 'TABLE '
ui:
chapter_name: "Chapter "
but it shows up in the word document as:
Figuee 1 blah-blah
with no colon between the figure number and the caption. For the life of me, I don't see where that is specified nor where I can change it. I'm sure I'm missing something simple somewhere.
sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server 2012 R2 x64 (build 9600)
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] bookdown_0.7
loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1 knitr_1.20 xfun_0.3
This is a bug of bookdown, which I just fixed on Github. Please try the development version:
remotes::install_github('rstudio/bookdown')

Printing a foreign text in a variable in a html generated by R markdown

In Rstudio interactive console, I can print store foreign characters in a variable and correctly print it the way I want.
> (korean_hello <- "안녕")
[1] "안녕"
But in the html file generated by rmarkdown::render, I get following.
(korean_hello <- "안녕")
## [1] "<U+C548><U+B155>"
That is, instead of the native characters, Unicode representation of the string is printed. How do you make it so that the generated html correctly prints what I want?
Below is my sessionInfo()
> sessionInfo()
R version 3.3.1 (2016-06-21)
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 methods
[7] base
loaded via a namespace (and not attached):
[1] rsconnect_0.4.3 tools_3.3.1

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.

Rstudio crushing when calling rgl and knitr in ioslides presentation

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.

Resources