2017-07-28 28 views
0

讓我們來看看這段代碼的動態報表,它基本上是默認模板條到最小:在RMarkdown有光澤,如何包裝輸入面板和renderPlot在一個獨特的呼叫

--- 
output: html_document 
runtime: shiny 
--- 

```{r} 
inputPanel(selectInput("n_breaks", label = "Number of bins:",choices = c(10, 20, 35, 50), selected = 20)) 
renderPlot({hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks))}) 
``` 

我想定義功能f1這樣我就可以運行,而不是這一點,具有完全相同的輸出:

--- 
output: html_document 
runtime: shiny 
--- 

```{r} 
f1() 
``` 

這可能嗎?

回答

0

該解決方案位於code塊選項中。

這裏有一個方法,使之爲我的工作例如(雖然在實踐中會更容易直接供給字符串):

--- 
output: html_document 
runtime: shiny 
--- 
```{r} 
f1 <- function(){ 
    inputPanel(selectInput("n_breaks", label = "Number of bins:",choices = c(10, 20, 35, 50), selected = 20)) 
    renderPlot({hist(faithful$eruptions, probability = TRUE, breaks = as.numeric(input$n_breaks))}) 
} 
``` 

```{r,code = as.character(body(f1))[-1]} 
```