How can I get webshot to work with bookdown? - r

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.

Related

R Markdown struggling with read_xlsx, Warning: Expecting logical

When running read_xlsx() in my normal .R script, I'm able to read in the data. But when running the .R script with source() in R Markdown, it suddenly takes a long time (> 20+++ mins I always terminate before the end) and I keep getting these warning messages where it is evaluating every single column and expecting it to be a logical:
Warning: Expecting logical in DE5073 / R5073C109: got 'HOSPITAL/CLINIC'
Warning: Expecting logical in DG5073 / R5073C111: got 'YES'
Warning: Expecting logical in CQ5074 / R5074C95: got '0'
Warning: Expecting logical in CR5074 / R5074C96: got 'MARKET/GROCERY STORE'
Warning: Expecting logical in CT5074 / R5074C98: got 'NO'
Warning: Expecting logical in CU5074 / R5074C99: got 'YES'
Warning: Expecting logical in CV5074 / R5074C100: got 'Less than one week'
Warning: Expecting logical in CW5074 / R5074C101: got 'NEXT'
Warning: Expecting logical in CX5074 / R5074C102: got '0'
.. etc
I can't share the data here, but it is just a normal xlsx file (30k obs, 110 vars). The data has responses in all capitals like YES and NO. The raw data has filters applied, some additional sheets, and some mild formatting in Excel (no borders, white fill) but I don't think these are affecting it.
An example of my workflow setup is like this:
Dataprep.R:
setwd()
pacman::p_load() # all my packages
df <- read_xlsx("./data/Data.xlsx") %>% type_convert()
## blabla more cleaning stuff
Report.Rmd:
setwd()
pacman::p_load() # all my packages again
source("Dataprep.R")
When I run Dataprep.R, everything works in < 1 min. But when I try to source("Dataprep.R") from Report.Rmd, then it starts being slow at read_xlsx() and giving me those warnings.
I've tried also taking df <- read_xlsx() from Dataprep.R and moving it to Report.Rmd, and it is still as slow as running source(). I've also removed type_convert() and tried other things like removing the extra sheets in the Excel. source() was also in the setup chunk in Report.Rmd, but I took it out and still the same thing.
So I think it is something to do with R Markdown and readxl/read_xlsx(). The exact same code and data is evaluating so differently in R vs Rmd and it's very puzzling.
Would appreciate any insight on this. Is there a fix? Or is this something I will just have to live with (i.e. convert to csv)?
> sessionInfo()
R version 4.2.0 (2022-04-22 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)
Matrix products: default
locale:
[1] LC_COLLATE=English_United Kingdom.utf8 LC_CTYPE=English_United Kingdom.utf8 LC_MONETARY=English_United Kingdom.utf8
[4] LC_NUMERIC=C LC_TIME=English_United Kingdom.utf8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] digest_0.6.29 R6_2.5.1 lifecycle_1.0.1 pacman_0.5.1 evaluate_0.15 scales_1.2.0 rlang_1.0.2 cli_3.3.0 rstudioapi_0.13
[10] rmarkdown_2.14 tools_4.2.0 munsell_0.5.0 xfun_0.30 yaml_2.3.5 fastmap_1.1.0 compiler_4.2.0 colorspace_2.0-3 htmltools_0.5.2
[19] knitr_1.39
UPDATE:
So in Markdown, I can use the more generic read_excel() and that works in my setup chunk. But I still get the same Warning messages if I try to source() it, even if the R script sourced is also using read_excel() instead of read_xlsx(). Very puzzling all around.
When you run that code on a .R (and probably other kinds of codes that generate warnings), you will get a summary of warnings. Something like "There were 50 or more warnings (use warning() to see the first 50)".
While if you run that same code on a standard Rmarkdown code chunk, you will actually get the whole 50+ warnings. That could mean you are printing thousands, millions, or more warnings.
If your question is WHY does that happen on Rmarkdown and not on R, I'm not sure.
But if your question is how to solve it, it's simple. Just make sure to add the options message=FALSE and warning=FALSE to your code chunk.
It should look something like this:
{r chunk_name, message=FALSE, warning=FALSE}
setwd()
pacman::p_load() # all my packages again
source("Dataprep.R")
Now, about the "setwd()", I would advise against using anything that changes the state of your system (avoid "side effect" functions). They can create problems if you are not very careful. But that is another topic for another day.

Is assignInNamespace() the correct solution for errors rendering distill with data.table?

Using the distill package and Rmarkdown for writing a blog, when rendering the .Rmd file I get errors when code chunks include data.table := and ., but not functions such as data.table(). The errors occur when the YAML header states draft: false but not when draft: true.
The R code chunk in the .Rmd file:
# create a data.table
library(data.table)
DT <- data.table(p = 1:5, q = 6:10)
# operate with ":="
DT[, r := p + q][]
# operate with "."
DT[, .(p)]
With draft: true in the YAML header, the .Rmd file knits with no problem.
With draft: false, I can still run the R code chunks with no error but knitting the .Rmd file produces this error:
Error: Check that is.data.table(DT) == TRUE. Otherwise, :=, `:=`(...)
and let(...) are defined for use in j, once only and in particular
ways. See help(":=").
Execution halted
If I comment out the line with the := operation, knitting the document produces a similar error on the . operation:
Error in .(p) : could not find function "."
Calls: <Anonymous> ... withVisible -> eval -> eval -> [ ->
[.data.table -> [.data.frame
Execution halted
On Stackoverflow I found a similar issue 3 years ago with the rtvs package (RTVS: Unable to Knit Document with data.table) that suggested a workaround using assignInNamespace(). I copied the suggestion to my code chunk and changed “rtvs” to “distill”, as follows:
# workaround
assignInNamespace("cedta.pkgEvalsUserCode",
c(data.table:::cedta.pkgEvalsUserCode, "distill"),
"data.table")
The help page for assignInNamespace() says the function is intended for use within a package, which my blog is not, but it does solve my problem. Adding this workaround to the first code chunk eliminates the errors and the .Rmd file renders correctly.
My questions are:
Does using assignInNamespace() in this way produce any problematic side effects?
Is there another solution or is this possibly a case where data.table or distill might need a patch?
Session information
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)
Matrix products: default
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
other attached packages:
[1] data.table_1.14.3
loaded via a namespace (and not attached):
[1] fansi_1.0.2 digest_0.6.29 R6_2.5.1 jsonlite_1.7.3
[5] magrittr_2.0.2 evaluate_0.14 stringi_1.7.6 rlang_1.0.1
[9] cachem_1.0.6 cli_3.2.0 rstudioapi_0.13 jquerylib_0.1.4
[13] bslib_0.3.1 vctrs_0.3.8 rmarkdown_2.11 distill_1.3
[17] tools_4.1.2 stringr_1.4.0 xfun_0.29 yaml_2.2.2
[21] fastmap_1.1.0 compiler_4.1.2 memoise_2.0.1 htmltools_0.5.2
[25] knitr_1.37 downlit_0.4.0 sass_0.4.0
2022-06-06 Update.
I've switched blog-authoring software from distill to quarto. The problem described above does not occur with quarto and porting older posts to quarto has been fairly straightforward.

Can't insert plots in bookdown

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.

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.

ggmap missing background map (except when using pdf device)

I am using the ggmap library in R to produce a map of the Atlanta area. When running the code, I'm able to get points plotted on the map as well as other layers, but am not able to get the map itself to print in any graphics device other than PDF. I suspect a configuration issue as I'm able to use this code on a different computer without issue, but two other machines simply display blank plots.
Here is a scaled down version of the code. I am assuming that my blank plots won't be reproducible, but I'm hoping to get some guidance on where the configuration problem might be.
library('ggplot2')
library('ggmap')
library('mapproj')
# ggmapTemp.png gets saved to the working directory correctly
atlanta <- get_map(location=c(lon=-84.26039,
lat=33.8751),
zoom=9,maptype='roadmap')
atlantaMap <- ggmap(atlanta, extent = 'device', legend = 'topleft')
# this produces a blank plot from both RStudio as well as R
atlantaMap
# this produces the actual map correctly
pdf("plot.pdf")
atlantaMap
dev.off()
# this produces a png file with only a white background
png("plot.png")
atlantaMap
dev.off()
Session Info:
R version 2.15.3 (2013-03-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
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] mapproj_1.2-1 ggmap_2.3 ggplot2_0.9.3.1 maps_2.3-6 maptools_0.8-27
[6] sp_1.0-14 car_2.0-19 reshape2_1.2.2 plyr_1.8 Revobase_6.2.0
[11] RevoMods_6.2.0 RevoScaleR_6.2.0 lattice_0.20-13 rpart_4.1-0
loaded via a namespace (and not attached):
[1] codetools_0.2-8 colorspace_1.2-4 dichromat_2.0-0 digest_0.6.3 foreach_1.4.0
[6] foreign_0.8-52 grid_2.15.3 gtable_0.1.2 iterators_1.0.6 labeling_0.2
[11] MASS_7.3-23 munsell_0.4.2 nnet_7.3-5 png_0.1-6 proto_0.3-10
[16] RColorBrewer_1.0-5 RgoogleMaps_1.2.0.5 rjson_0.2.13 RJSONIO_1.0-3 scales_0.2.3
[21] stringr_0.6.2 tools_2.15.3
And capabilities:
jpeg png tiff tcltk X11 aqua http/ftp sockets libxml fifo cledit
TRUE TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE
iconv NLS profmem cairo
TRUE TRUE TRUE TRUE
The problem has to do with the server limiting the bit depth that gets passed through the connection. Oracle documents the problem and solution on their site:
In a Remote Desktop session, all environment variables, including display variables determining Color Depth, are determined by the RCP-Tcp connection settings. For example, users can reduce the Color Depth when connecting over a slow connection. The different settings are 15 bits, 16 bits, 24 bits, or 32 bits per pixel. To raise the Remote Desktop color depth:
On the Windows server, launch Remote Desktop Session Host Configuration from the Accessories menu.
Under Connections, right click on RDP-Tcp and select Properties.
On the Client Settings tab either uncheck LimitMaximum Color Depth or set it to 32 bits per pixel.
Upon unchecking the "Limit Maximum Color Depth" check box and reconnecting to the server, the raster map backgrounds now show up as expected. Note that they also provide option 2 which is to output to an alternate device.
It seems jrshrenk haven't updated R and the packages in a while. Although in theory your versions of R, ggmap, png and ggplot2 seems sufficient:
Package: ggmap
Version: 2.4
Depends: R (>= 2.14.0), ggplot2 (>= 0.9.2)
Imports: proto, scales, RgoogleMaps, png, plyr, reshape2, grid, rjson,
mapproj, jpeg, geosphere, digest
Suggests: MASS, stringr
License: GPL-2
NeedsCompilation: no
Package: png
Version: 0.1-7
Depends: R (>= 2.9.0)
License: GPL-2 | GPL-3
NeedsCompilation: yes
I would just try to update R and the packages to see if it solves the issue:
#Save your current packages but not the base ones:
savepackages <- rownames(installed.packages(priority='NA'))
write(savepackages, file="listpackages.txt")
#Check for Updates of R
library(installr)
check.for.updates.R(notify_user = TRUE, use_GUI = TRUE,
page_with_download_url = "http://cran.rstudio.com/bin/windows/base/",
pat = "R-[0-9.]+-win")
#Download and Install New Version
install.R(page_with_download_url = "http://cran.rstudio.com/bin/windows/base/",
pat = "R-[0-9.]+-win.exe", to_checkMD5sums = TRUE,
keep_install_file = TRUE, download_dir = tempdir(), silent = FALSE)
#Then reinstall packages
pkg.ls <- read.table("listpackages.txt")
install.packages(pkg.ls)

Resources