无法在rmarkdown Shiny app中播放嵌入在手中的本地音频

时间:2016-08-18 11:01:00

标签: html5 shiny r-markdown handsontable rhandsontable

以下rmarkdown创建了一个Shiny文档,其中包含一个指向wav文件的链接,可以在浏览器中播放(使用Chrome)。表中的第一个链接指向外部wav文件http://www.nch.com.au/acm/11k16bitpcm.wav,第二个链接指向位于www文件夹中相对于rmarkdown的同一文件。第一个链接工作,第二个链接不工作。根据我在网上找到的各种文章,www文件夹是这种外部内容的正确位置,事实上,如果我在那里放置png文件,我可以在其中使用img()函数。 rmarkdown让Shiny正确显示图像。

---
title: "Playing audio in handsontable"
date: "18 August 2016"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
library(rmarkdown)
library(rhandsontable)
library(dplyr)
links = c('<audio controls preload="none" type="audio/wav" src="http://www.nch.com.au/acm/11k16bitpcm.wav" </audio>',
'<audio controls preload="none" type="audio/wav" src="www/11k16bitpcm.wav" </audio>')
toDisplay = data.frame(Listen = links)
```

The first entry in this table refers to an external WAV file <http://www.nch.com.au/acm/11k16bitpcm.wav> and can be played in a browser. The second entry refers to the same file called `11k16bitpcm.wav` located in the folder `www` relative to the markdown but cannot be played. As a check, if the file can be seen from the markdown, the following will be TRUE: `r file.exists("www/11k16bitpcm.wav")`.

```{r tabsets, echo=FALSE}
renderRHandsontable({
  rhandsontable(toDisplay, readOnly = TRUE, allowedTags = "<em><b><strong><a><big><audio>", rowHeaders = TRUE) %>%
  hot_cols(columnSorting = T) %>%
  hot_col(1, renderer = "html") %>%
  hot_col(1, renderer = htmlwidgets::JS("safeHtmlRenderer"))
})
```

有没有人有任何提示可以帮助我让第二个链接正常工作并正确提供音频?

1 个答案:

答案 0 :(得分:0)

闪亮包中的函数addResourcePath就是答案。

以下是具有相应呼叫的代码。

---
title: "Playing audio in handsontable"
date: "18 August 2016"
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(shiny)
library(rmarkdown)
library(rhandsontable)
library(dplyr)
links = c('<audio controls preload="none" type="audio/wav" src="http://www.nch.com.au/acm/11k16bitpcm.wav" </audio>',
'<audio controls preload="none" type="audio/wav" src="www/11k16bitpcm.wav" </audio>')
toDisplay = data.frame(Listen = links)
# this declares a path containing the resource
addResourcePath("www", "www")
```

The first entry in this table refers to an external WAV file <http://www.nch.com.au/acm/11k16bitpcm.wav> and can be played in a browser. The second entry refers to the same file called `11k16bitpcm.wav` located in the folder `www` relative to the markdown but cannot be played unless a call to `shiny::addResourcePath()` is made. As a check, if the file can be seen from the markdown, the following will be TRUE: `r file.exists("www/11k16bitpcm.wav")`.

```{r tabsets, echo=FALSE}
renderRHandsontable({
  rhandsontable(toDisplay, readOnly = TRUE, allowedTags = "<em><b><strong><a><big><audio>", rowHeaders = TRUE) %>%
  hot_cols(columnSorting = T) %>%
  hot_col(1, renderer = "html") %>%
  hot_col(1, renderer = htmlwidgets::JS("safeHtmlRenderer"))
})
```