将 HTML 和 javascript 代码添加到 R 中的 flexdashboard

Man*_*anu 1 html r flexdashboard

我有以下创建 Flexdashboard 的代码:

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    runtime: shiny
---

```{r setup, include=FALSE}
library(flexdashboard)
Run Code Online (Sandbox Code Playgroud)

我想插入一些 HTML 和 javascript 代码。我试过这个

    Column
    -----------------------------------------------------------------------

    ### Block 1

    ```{r}
    <p>"This is a paragraph"</p>
    <script>
      alert("This is an alert")
    </script>
    ```
Run Code Online (Sandbox Code Playgroud)

但这不起作用。请问您能帮我解答这个问题吗?谢谢。

Sté*_*ent 6

您可以直接键入 HTML 代码,无需块。tags您还可以在块中使用“htmltools”包的功能(或闪亮的 UI 功能)。对于 JavaScript,请使用js块。

---
title: "TEST"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
---

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

```{r packages, include=FALSE}
library(flexdashboard)
library(htmltools)
```

Page
====================================

Row
-----------------------------

### HTML and JavaScript

<button id="btn">Click me</button>

```{js, echo=FALSE}
$("#btn").on("click", function() {
  alert("You clicked the button!")
})
```

### HTML using 'htmltools'

```{r, echo=FALSE}
tags$button("Another button")
```
Run Code Online (Sandbox Code Playgroud)