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

Add HTML and javascript code to flexdashboard in R

我有以下创建 flexdashboard 的代码:

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

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

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

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

    ### Block 1

    ```{r}
    <p>"This is a paragraph"</p>
    <script>
      alert("This is an alert")
    </script>
    ```

但是没用。拜托,你能帮我解决这个问题吗?谢谢。

您可以直接输入 HTML 代码,无需分块。您还可以在块中使用 'htmltools' 包的 tags 函数(或 Shiny 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")
```