Including a 3D interactive figure in html and static in word/pdf using knitr and rgl
Asked Answered
V

2

8

Following this question (including a interactive 3D figure with knitr) and this example by Yihui (https://dl.dropboxusercontent.com/u/15335397/misc/webgl-rmd.html), I can include a 3D interactive figure in html output using knitr and Rmarkdown. But I would like to include a static figure in word/pdf output.

Is it possible to do this? Thanks for any suggestion.

My sessionInfo

> sessionInfo()
R version 3.1.1 (2014-07-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

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] rgl_0.95.1158 knitr_1.8    

loaded via a namespace (and not attached):
[1] evaluate_0.5.5 formatR_1.0    stringr_0.6.2  tools_3.1.1 
Vibration answered 17/11, 2014 at 5:12 Comment(2)
I'm guessing you could use knitr::opts_knit$get("rmarkdown.pandoc.to") to query the output format, and feed that info to an optional cat.Halstead
Also relevant: #25528567Oleum
H
8

You could use the following setup to switch according to the output format

```{r, echo=FALSE}
out_type <- knitr::opts_knit$get("rmarkdown.pandoc.to")
keep <- if(out_type == "html") 'none' else 'last'
```


```{r chunk, echo=FALSE, fig.keep=keep}
plot(cars)
if(out_type == "html")
  cat("there goes fancy js code")
```
Halstead answered 17/11, 2014 at 12:46 Comment(2)
the out_type function does not work in my .Rmd file; the object is empty ("NULL") if I run that chunk. Can you help me fix this?Southworth
I don't know what the problem is; it works for me. You won't see the result in an interactive run of this particular chunk, though, the whole document presumably has to be run via knitr/rmarkdown for this option to be set.Halstead
O
0

As pointed out in an answer to a related question, knitr 1.18 introduced the function

knitr::is_html_output()

which checks if the output is HTML. Adapting @baptiste's excellent answer to use this function:

```{r, echo=FALSE}
keep <- if(knitr::is_html_output()) 'none' else 'last'
```


```{r chunk, echo=FALSE, fig.keep=keep}
plot(cars)
if(knitr::is_html_output())
  cat("there goes fancy js code")
```
Oleum answered 10/10, 2018 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.