【问题标题】:Javascript code block in rmarkdown does not workrmarkdown 中的 Javascript 代码块不起作用
【发布时间】:2019-12-10 04:44:08
【问题描述】:

我正在尝试创建一个带有复制文本片段的按钮的 Rmarkdown 文档。我已经在在线 HTML 编辑器中独立测试了代码,当我按下按钮时,文本被成功复制到剪贴板。然后,我尝试使用 bookdown 作者 Yihui Xie.(https://bookdown.org/yihui/rmarkdown/) 推荐的代码块,使其在 Rmarkdown 文档中工作。 Rmarkdown文档编译成.html时,按钮不起作用(RStudio用来编译)。

    ---
    author: "Nick Riches"
    output:
      html_document:
        number_sections: no
        toc: yes
        toc_float:
          collapsed: yes
      pdf_document:
        toc: yes
      word_document:
        toc: yes
    ---

    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```

    ```{js, echo=FALSE}

    function myFunction() {
      var copyText = document.getElementById("myInput");
      copyText.select();
      document.execCommand("copy");
      alert("Copied the text: " + copyText.value);

    ```


    <p>Click on the button to copy the text from the text field. Try to paste the text (e.g. ctrl+v) afterwards in a different window, to see the effect.</p>

    <input type="text" value="Hello World" id="myInput">
    <button onclick="myFunction()">Copy text</button>

【问题讨论】:

    标签: html r-markdown


    【解决方案1】:

    我在 github 上发现了一个由 Romain Lesur 开发的名为 klippy 的包。这就是你可以使用它的方式

    ```{r, echo=FALSE, message=FALSE}
    library(devtools)
    devtools::install_github("RLesur/klippy")
    ```
    
    
    ```{r klippy, echo=FALSE, include=TRUE}
    klippy::klippy(lang = "markdown")
    ```
    
    ```{markdown}
    Here insert text to be copied
    ```
    

    完整语法参考:https://rlesur.github.io/klippy/reference/klippy.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多