Can't insert plots in bookdown - r

I'm using bookdown to generate notes for an R. A minimal bookdown example that exhibits the error:
https://github.com/DavisBrian/bookdown_error
Everything worked great until I tried to add a plot (dataviz.Rmd). When building the book I get the following error:
! LaTeX Error: File `dataviz_files/figure-latex/plotxyx-1' not found.
Error: Failed to compile rclassnotes.tex. See rclassnotes.log for more info.
In addition: Warning message:
In parse_packages(logfile, quiet = c(TRUE, FALSE, FALSE)) :
Failed to find a package that contains dataviz_files/figure-latex/plotxyx-1
Execution halted
Exited with status 1.
I've run this under:
> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS High Sierra 10.13.4
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
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
loaded via a namespace (and not attached):
[1] compiler_3.4.4 backports_1.1.2 bookdown_0.7 magrittr_1.5 rprojroot_1.3-2 htmltools_0.3.6 tools_3.4.4
[8] yaml_2.1.18 Rcpp_0.12.16 stringi_1.1.7 rmarkdown_1.9 knitr_1.20 xfun_0.1 stringr_1.3.0
[15] digest_0.6.15 evaluate_0.10.1
Any ideas on where to go from here?
Edit:
It seems very similar to:
https://github.com/rstudio/bookdown-demo/issues/3
and
https://github.com/rstudio/bookdown/issues/342
Note I have this issue with both Windows and Mac OSX. I don't think it's a simple software installation error of one of the components.
The files seem to be created in _bookdown_files/dataviz_files/figure-latex/ and temporarily in _dataviz_files/figure-latex/

The problem was due to the fact that you set the graphical device to svg in _common.R: https://github.com/DavisBrian/bookdown_error/blob/9f4078a/_common.R#L13
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
cache = FALSE,
out.width = "70%",
fig.align = 'center',
fig.width = 6,
fig.asp = 0.618, # 1 / phi
fig.show = "hold",
dev = "svg"
)
LaTeX does not support SVG images natively (at least for now). You have to either use special LaTeX packages, or use an image format that LaTeX can recognize, such as pdf or png.

Related

Size of PDF files generated by knitr

I am using the following chunk in an Rnw file.
<<scatter,dev='pdf',include=TRUE>>=
a <- rnorm(1e4)
b <- rnorm(1e4)
plot(a,b)
#
I knitted the same Rnw file under R3.6.3 and R4.0.2, respectively. However, the PDF figure generated under R4.0.2 is much larger (~8 times) than the one from R3.6.3.
I am wondering if there is a way (eg. a chunk option) to reduce the PDF size under R4.0.2.
The session information of two different versions of R is shown below.
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)
Matrix products: default
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.29
loaded via a namespace (and not attached):
[1] compiler_4.0.2 magrittr_1.5 tools_4.0.2 stringi_1.4.6 highr_0.8 stringr_1.4.0
[7] xfun_0.16 evaluate_0.14
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 16299)
Matrix products: default
Random number generation:
RNG: Mersenne-Twister
Normal: Inversion
Sample: Rounding
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252
[3] LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
[5] LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.28
loaded via a namespace (and not attached):
[1] compiler_3.6.3 magrittr_1.5 tools_3.6.3 stringi_1.4.6 highr_0.8 stringr_1.4.0
[7] xfun_0.12 evaluate_0.14
The only reference to pdf in the release notes lately has been in relation to the dingbats font. Looking at the reference, it says (emphasis mine):
useDingbats logical. Should small circles be rendered via the
Dingbats font? Defaults to TRUE, which produces smaller and better
output. Setting this to FALSE can work around font display problems in
broken PDF viewers: although this font is one of the 14 guaranteed to
be available in all PDF viewers, that guarantee is not always
honoured.
You can change the default via
pdf.options(useDingbats = TRUE)
in the first code chunk of your Rnw document. This change is global to your document. If you'd rather change it only for a specific code chunk, you may use the chunk option dev.args, e.g.,
<<scatter, dev='pdf', dev.args=list(useDingbats = TRUE)>>=
a <- rnorm(1e4)
b <- rnorm(1e4)
plot(a,b)
#

How can I get webshot to work with bookdown?

I am using bookdown to create a "book", in both PDF and HTML.
In places where I have a video for the HTML, I get this message when I render:
Error: Functions that produce HTML output found in document targeting latex output.
Please change the output type of this document to HTML. Alternatively, you can allow
HTML output in non-HTML formats by adding this option to the YAML front-matter of
your rmarkdown file:
always_allow_html: true
Note however that the HTML output will not be visible in non-HTML formats.
Following these instructions about always_allow_html seems to be discouraged by yihui (https://github.com/rstudio/bookdown/issues/440):
You should not add always_allow_html: yes to YAML.
So, a search yields a solution (e.g. https://bookdown.org/yihui/bookdown/html-widgets.html):
Since knitr v1.13, HTML widgets will be rendered automatically as screenshots taken via the webshot package (Chang 2019). Of course, you need to install the webshot package. Additionally, you have to install PhantomJS (http://phantomjs.org), since it is what webshot uses to capture screenshots. Both webshot and PhantomJS can be installed automatically from R:
...and some instructions given, which I have followed.
So now, I have webshot and phantomjs download and installed by following those instructions, which is easily confirmed:
> webshot::is_phantomjs_installed()
[1] TRUE
> webshot:install_phantomjs()
It seems that the version of `phantomjs` installed is greater than or equal to the requested version.To install the requested version or downgrade to another version, use `force = TRUE`.
Error in webshot:install_phantomjs() : argument of length 0
Whether I use webshot:install_phantomjs() or webshot:install_phantomjs(force=TRUE), nothing changes.
Indeed, this fails:
> webshot:install_phantomjs(version="2.1.1", force=TRUE)
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 637 100 637 0 0 0 1585 0 --:--:-- --:--:-- --:---:-- --:--:-- 1588
0 16.3M 100 16.3M 100 16.3M 0 0 3244k 0 0:00:05 0:00:05 --:--:-- 3926k
0:01 0:10:39 49930phantomjs has been installed to /Users/pdunn2/Library/Application Support/PhantomJS
Error in webshot:install_phantomjs(version = "2.1.1", force = TRUE) :
argument of length 0
So... if webshot and phantomjs appear to be installed... what am I doing wrong?
Thanks for direction...
P.
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Mojave 10.14.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib
locale:
[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] webshot_0.5.2 bookdown_0.20
loaded via a namespace (and not attached):
[1] packrat_0.5.0 digest_0.6.25 jsonlite_1.7.0 magrittr_1.5 evaluate_0.14 rlang_0.4.7 stringi_1.4.6
[8] curl_4.3 rstudioapi_0.11 rmarkdown_2.3 webshot_0.5.2 tools_4.0.2 stringr_1.4.0 xfun_0.15
[15] yaml_2.2.1 rsconnect_0.8.16 compiler_4.0.2 askpass_1.1 htmltools_0.5.0 openssl_1.4.2 knitr_1.29
Adding a webshot library solve my problem.
for example:
{r}
library(webshot)
DT::datatable(iris)
Funnily enough, I tried this solution after reading your reference. And it works like a charm. Thanks to you.

problem with svg device, expression(), and unicode characters

I am having difficulty using the svg device with math expressions and unicode (in this case, left- and right-quotes). This only occurs on my Windows 10 computer; my osx computer seems unaffected. Consider the following R code:
output = tempfile(fileext = ".svg")
svg(file = output)
plot(1, axes=FALSE, ty='n')
## Line A:
mtext(side = 2, text = expression(paste("A")))
## Line B:
mtext(side = 3, text = expression(paste("“B")))
## Line C:
mtext(side = 4, text = expression(paste("C")))
axis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)
dev.off()
## Uncomment and run to open the SVG file to look at it
# browseURL(output)
(Note the left-quote in Line B). Upon running Line C, I get
Error in mtext(side = 4, text = expression(paste("C"))) :
metric information not available for this device
If I change the device to png I get the output I expect.
If I comment out lines B and C, I get the output I expect (eg, the output from lines A and the axis)
If I comment out only line C, I do not get the output I expect. Neither line B works, nor the axis. There appears to be something wrong with line B that causes everything later in the chunk to fail too.
If I remove the expression() from line B, everything works as expected (including the unicode left-quote)
If I change the font using showtext (see the issue for details: https://github.com/yihui/knitr/issues/1831) I have no problem.
This appears, then, to be an interaction between svg, expression, the default font, and unicode. But only on Windows 10. My OSX computer (see details below) does not have the issue.
sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
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] reprex_0.3.0
loaded via a namespace (and not attached):
[1] Rcpp_1.0.4.6 ps_1.3.2 digest_0.6.25 R6_2.4.1 evaluate_0.14 rlang_0.4.5 fs_1.4.1
[8] callr_3.4.3 whisker_0.4 rmarkdown_2.1 Cairo_1.5-12 tools_3.6.2 xfun_0.13 compiler_3.6.2
[15] processx_3.4.2 clipr_0.7.0 htmltools_0.4.0 knitr_1.28.3
OSX session info (no problem on this computer)
> xfun::session_info('knitr')
R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.4, RStudio 1.2.5031
Locale: en_GB.UTF-8 / en_GB.UTF-8 / en_GB.UTF-8 / C / en_GB.UTF-8 / en_GB.UTF-8
Package version:
evaluate_0.14 glue_1.4.0 graphics_3.6.1 grDevices_3.6.1
highr_0.8 knitr_1.28.3 magrittr_1.5 markdown_1.1
methods_3.6.1 mime_0.9 stats_3.6.1 stringi_1.4.6
stringr_1.4.0 tools_3.6.1 utils_3.6.1 xfun_0.12
yaml_2.2.1
Any idea what is causing this?
I get the same problem on my 32-bit Windows work PC. It's a tricky error to track down, but I eventually found it is being thrown here in the GMMathText function of graphics.c. As you describe, it only seems to be a problem with expressions containing certain characters on some graphics devices. In particular, anything using Cairo on Windows seems to stub its toe at the same point.
After attempting several hacks to get around this, I found that the best solution is to simply use bquote instead of expression. This allows you to keep all of the plotmath functionality you get with expression without generating the error:
svg(output)
plot(1, axes=FALSE, ty='n')
mtext(side = 2, text = bquote("A"))
mtext(side = 3, text = bquote("“B"))
mtext(side = 4, text = bquote("C"))
axis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)
dev.off()
Which gives the following SVG:
There is a nice overview of how to use bquote to generate your labels, including all the maths expressions you need here
I'm taking something of a guess here because I know nothing about R. However the local for your windows box is cp1252 which does not contain Unicode quotes but uses Microsoft's smart quotes.
You say it works in OS-X which looking at your session info is set up to use UTF-8.
I'm assuming that your program file is saved as UTF-8.

dev='png' is not working for pdf output in knitr

In the default setting, knitr will use "'pdf' for LaTeX output and 'png' for HTML/markdown". However, I can specify the dev = "png" in the chunk options for the LaTeX output. This feature is very useful to reduce the file size for big vector figure (e.g. maps).
In the previous version of knitr (could be 1.8.*, but not sure), I can specify the dev = "png" (the example below is working for earlier version).
Today I installed the latest version of knitr from github (just now). But the dev='png' is not working for pdf output. I have to use png device for pdf output as I have some big vector figure in pdf format.
The error message is:
Error in (function (filename = "Rplot%03d.png", width = 480, height = 480, :
unused argument (pdf = list(useDingbats = FALSE) Call: <Anonymous> ...
block_exec -> chunk_device -> dev_new -> do.call -> <Anonymous> Execution halted))
How could I fix this problem?
This is my example:
---
output: pdf_document
---
```{r, echo=FALSE, dev='png'}
plot(cars)
```
Thanks for any advice. Please let me know if my question is still not clear.
This is my session information.
R version 3.1.3 (2015-03-09)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252 LC_MONETARY=English_Australia.1252
[4] LC_NUMERIC=C LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] knitr_1.9.4
loaded via a namespace (and not attached):
[1] digest_0.6.4 evaluate_0.5.5 formatR_1.0 htmltools_0.2.6 rmarkdown_0.5.1 stringr_0.6.2
[7] tools_3.1.3 yaml_2.1.13
This is a bug in knitr. I just fixed it in the development version (v1.9.5). The reason for the failure was that the options for the pdf device were passed to the png device.

Function `Boot` from `R` package `car` can not find .carEnv

While using the Boot function from the car package I get the error message
Error in get(".y.boot", envir = .carEnv) : object '.carEnv' not found
I suspect I have inadvertently changed/set something in my OS and have no idea what it might be. Running the code below returns an error on my desktop but runs without error on a laptop running the same OS (Yosemite) as well as a desktop running Windows 7 (all using R-3.1.2). The code that triggers the message is
library(car)
swiss.lm <- lm(Fertility ~ Education, data = swiss)
BC <- Boot(swiss.lm, R = 999, method = "case") # No Problems
BR <- Boot(swiss.lm, R = 999, method = "residual") # Problems now
Error in get(".y.boot", envir = .carEnv) : object '.carEnv' not found
I have reinstalled R but the error still appears when running the above code. Any suggestions as to what I have done and how to get the code to run and find the environment would be most appreciated. TIA!
> sessionInfo()
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.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] car_2.0-24
loaded via a namespace (and not attached):
[1] boot_1.3-15 grid_3.1.2 lattice_0.20-29 lme4_1.1-7 MASS_7.3- 37 Matrix_1.1-5
[7] mgcv_1.8-4 minqa_1.2.4 nlme_3.1-119 nloptr_1.0.4 nnet_7.3-9 parallel_3.1.2
[13] pbkrtest_0.4-2 quantreg_5.11 Rcpp_0.11.4 SparseM_1.6 splines_3.1.2 tools_3.1.2
Looks like a reproducible bug in a car package. According to package news the changes in latest version (2.0-24) are related to .carEnv handling.
I tried to get around the issue by simply assigning the .carEnv before call to Boot with
.carEnv <- car:::.carEnv
This makes the Boot function execute without errors, but I am not sure of any other effects.
The package maintainer emailed me and indicated a bug had been introduced in 2.0-24 and that he would attempt to fix the bug.

Resources