2016-08-18 40 views
1

以下的rmarkdown創建一個Shiny文檔,其中包含一個可以在瀏覽器中使用的wav文件鏈接(使用Chrome)。表中的第一個鏈接指向外部wav文件http://www.nch.com.au/acm/11k16bitpcm.wav,第二個鏈接指向位於相對於rmarkdown的www文件夾中的同一文件。第一個鏈接起作用,第二個鏈接不起作用。根據我在網上找到的各種文章,www文件夾是此類外部內容的正確位置,事實上,如果我將png文件放在那裏,我可以使用rmarkdown中的img()函數讓Shiny正確顯示圖像。無法在rmondown內播放嵌入在handsontable中的本地音頻Shiny app

--- 
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")) 
}) 
``` 

有沒有人有任何提示,以幫助我得到第二個鏈接工作和正確提供音頻?

回答

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")) 
}) 
```