How to add an interactive visualization to R markdown

前端 未结 2 1139
长情又很酷
长情又很酷 2021-02-02 15:32

My question is I want to integrate a d3.js visualization to my markdown rather than a link pointing to the visualization on external website. Is there a way to achieve that?

2条回答
  •  北海茫月
    2021-02-02 16:13

    To accomplish adding nonlocal javascript such as d3.v3.min.js to our Rmd, there are a couple ways to do it. If you are looking to include local copy of d3, it is much easier.

    This is my favorite way. If for some reason, you would like to see the others, I will be happy to show them. Note: I am still experimenting.

    ---
    title: "rmarkdown example with external js"
    output:
      html_document:
        self_contained: false
        keep_md: true
        includes:
          in_header: "header_include_d3.html"
    ---
    
    Let's create a very basic d3 graph using data from R.  since the graph is d3, we will need the d3.js file for the graph to render.
    
    ```{r results='asis'}
    
    cat('
    
    ')
    
    ```
    
    
    

    then in the same directory as this .Rmd file, save this

    
    

    into a file I called header_include_d3.html or whatever name you would like. If you change the name, just be sure to change the reference in the includes in the yaml of your Rmd.

    As I said before, this is much easier if you have d3.js locally that you would like to use.

    Also, inside the body will work if you are not particular about have your js in the header. In that case, just include it anywhere in the Rmd.

提交回复
热议问题