ggiraph htmlwidgets with bookdown and new_session = yes

耗尽温柔 提交于 2021-01-28 09:17:25

问题


This seems related to to rstudio/bookdown#15 but I haven't been able to figure pin it down. The example is here but the offending code is

p2 <- ggplot(mtcars, aes(x = disp, y = mpg)) + 
  geom_point_interactive(aes(tooltip = car))
ggiraph(code = print(p2))

and my bookdown.yml has new_session: yes. Other htmlwidgets do work.

  ```r
  p2 <- ggplot(mtcars, aes(x = disp, y = mpg)) + 
    geom_point_interactive(aes(tooltip = car))
  ggiraph(code = print(p2))
  ```

Session info:

  $ R

  R version 3.3.3 (2017-03-06) -- "Another Canoe"
  Copyright (C) 2017 The R Foundation for Statistical Computing
  Platform: x86_64-apple-darwin13.4.0 (64-bit)

  R is free software and comes with ABSOLUTELY NO WARRANTY.
  You are welcome to redistribute it under certain conditions.
  Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

  R is a collaborative project with many contributors.
  Type 'contributors()' for more information and
  'citation()' on how to cite R or R packages in publications.

  Type 'demo()' for some demos, 'help()' for on-line help, or
  'help.start()' for an HTML browser interface to help.
  Type 'q()' to quit R.

  > library(bookdown)
  > library(rmarkdown)
  > render_book("index.Rmd", "bookdown::gitbook")


  processing file: index.Rmd
  |................                                                 |  25%
  inline R code fragments

  |................................                                 |  50%
  label: unnamed-chunk-1 (with options) 
  List of 1
  $ eval: logi FALSE

  |.................................................                |  75%
  ordinary text without R code

  |.................................................................| 100%
  label: unnamed-chunk-2 (with options) 
  List of 1
  $ include: logi FALSE


  output file: index.knit.md



  processing file: 01-intro.Rmd
  |......                                                           |   9%
  ordinary text without R code

  |............                                                     |  18%
  label: nice-fig (with options) 
  List of 4
  $ fig.cap  : chr "Here is a nice figure!"
  $ out.width: chr "80%"
  $ fig.asp  : num 0.75
  $ fig.align: chr "center"

  |..................                                               |  27%
  ordinary text without R code

  |........................                                         |  36%
  label: nice-tab (with options) 
  List of 1
  $ tidy: logi FALSE

  |..............................                                   |  45%
  ordinary text without R code

  |...................................                              |  55%
  label: load-libs
  |.........................................                        |  64%
  ordinary text without R code

  |...............................................                  |  73%
  label: ggplot
  |.....................................................            |  82%
  ordinary text without R code

  |...........................................................      |  91%
  label: ggiraph
  |.................................................................| 100%
  ordinary text without R code


  output file: 01-intro.knit.md



  processing file: 02-literature.Rmd
  |.................................................................| 100%
  ordinary text without R code


  output file: 02-literature.knit.md



  processing file: 03-method.Rmd
  |.................................................................| 100%
  ordinary text without R code


  output file: 03-method.knit.md



  processing file: 04-application.Rmd
  |.................................................................| 100%
  ordinary text without R code


  output file: 04-application.knit.md



  processing file: 05-summary.Rmd
  |.................................................................| 100%
  ordinary text without R code


  output file: 05-summary.knit.md



  processing file: 06-references.Rmd
  |.................................................................| 100%
  inline R code fragments


  output file: 06-references.knit.md

  Error: path for html_dependency not found: /var/folders/8r/bytzpgb50dx3159xd49zgtxw0000gn/T//RtmpXcXTft/file142585251ceaa
  Please delete bookdown-demo.md after you finish debugging the error.
  > library(sessioninfo)
  > session_info()
  ─ Session info ───────────────────────────────────────────────────────────────
  setting  value                       
  version  R version 3.3.3 (2017-03-06)
  os       macOS Sierra 10.12.6        
  system   x86_64, darwin13.4.0        
  ui       X11                         
  language (EN)                        
  collate  en_US.UTF-8                 
  tz       America/New_York            
  date     2017-09-29                  

  ─ Packages ───────────────────────────────────────────────────────────────────
  package     * version    date       source                               
  assertthat    0.2.0      2017-04-11 CRAN (R 3.3.2)                       
  backports     1.1.0      2017-05-22 CRAN (R 3.3.2)                       
  base64enc     0.1-3      2015-07-28 CRAN (R 3.3.0)                       
  bindr         0.1        2016-11-13 CRAN (R 3.3.2)                       
  bindrcpp      0.2        2017-06-17 cran (@0.2)                          
  bookdown    * 0.5        2017-08-20 CRAN (R 3.3.2)                       
  clisymbols    1.2.0      2017-05-21 CRAN (R 3.3.2)                       
  colorspace    1.3-2      2016-12-14 CRAN (R 3.3.2)                       
  digest        0.6.12     2017-01-27 CRAN (R 3.3.2)                       
  dplyr         0.7.3      2017-09-09 cran (@0.7.3)                        
  evaluate      0.10.1     2017-06-24 CRAN (R 3.3.2)                       
  gdtools       0.1.4      2017-03-17 cran (@0.1.4)                        
  ggiraph     * 0.4.0      2017-06-24 CRAN (R 3.3.2)                       
  ggplot2     * 2.2.1      2016-12-30 CRAN (R 3.3.2)                       
  glue          1.1.1      2017-06-21 CRAN (R 3.3.2)                       
  gtable        0.2.0      2016-02-26 CRAN (R 3.3.0)                       
  htmltools     0.3.6      2017-09-27 Github (rstudio/htmltools@02678ee)   
  htmlwidgets   0.9        2017-09-27 Github (ramnathv/htmlwidgets@2c49516)
  knitr         1.17       2017-08-10 CRAN (R 3.3.2)                       
  lazyeval      0.2.0      2016-06-12 CRAN (R 3.3.0)                       
  magrittr      1.5        2014-11-22 CRAN (R 3.3.0)                       
  munsell       0.4.3      2016-02-13 CRAN (R 3.3.0)                       
  officer       0.1.6      2017-08-30 CRAN (R 3.3.2)                       
  pkgconfig     2.0.1      2017-03-21 cran (@2.0.1)                        
  plyr          1.8.4      2016-06-08 CRAN (R 3.3.0)                       
  purrr         0.2.3      2017-08-02 cran (@0.2.3)                        
  R.methodsS3   1.7.1      2016-02-16 CRAN (R 3.3.0)                       
  R.oo          1.21.0     2016-11-01 CRAN (R 3.3.0)                       
  R.utils       2.5.0      2016-11-07 CRAN (R 3.3.0)                       
  R6            2.2.2      2017-06-17 cran (@2.2.2)                        
  Rcpp          0.12.12    2017-07-15 cran (@0.12.12)                      
  rlang         0.1.2      2017-08-09 cran (@0.1.2)                        
  rmarkdown   * 1.6        2017-06-15 CRAN (R 3.3.2)                       
  rprojroot     1.2        2017-01-16 CRAN (R 3.3.2)                       
  rstudioapi    0.7.0-9000 2017-09-10 Github (rstudio/rstudioapi@8e8bfb0)  
  rvg           0.1.4      2017-06-23 CRAN (R 3.3.2)                       
  scales        0.5.0      2017-08-24 CRAN (R 3.3.2)                       
  sessioninfo * 1.0.0      2017-06-21 CRAN (R 3.3.2)                       
  stringi       1.1.5      2017-04-07 CRAN (R 3.3.2)                       
  stringr       1.2.0      2017-02-18 CRAN (R 3.3.2)                       
  tibble        1.3.4      2017-08-22 cran (@1.3.4)                        
  uuid          0.1-2      2015-07-28 CRAN (R 3.3.0)                       
  withr         2.0.0      2017-07-28 CRAN (R 3.3.2)                       
  xml2          1.1.1      2017-01-24 CRAN (R 3.3.3)                       
  yaml          2.1.14     2016-11-12 CRAN (R 3.3.2)                       
  zip           1.0.0      2017-04-25 CRAN (R 3.3.2) 
  ```

回答1:


You were correct that this was related to rstudio/bookdown#15. The root cause is that ggiraph uses the temporary directory to create the HTML dependency for the widget, and R will clean up the temporary directory on exit. Since you used new_session: yes, the Rmd is compiled in a separate R session; your HTML widget is created there but actually rendered later (after that R session has quit). By the time it is to be rendered, the temporary directory has gone.

Therefore you have to choose between using ggiraph and new_session: yes. You cannot have both.



来源:https://stackoverflow.com/questions/46488976/ggiraph-htmlwidgets-with-bookdown-and-new-session-yes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!