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

前端 未结 2 922
终归单人心
终归单人心 2020-12-29 00:10

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 in

相关标签:
2条回答
  • 2020-12-29 00:45

    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")
    ```
    
    0 讨论(0)
  • 2020-12-29 01:04

    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")
    ```
    
    0 讨论(0)
提交回复
热议问题