R Flexdashboard和闪亮的互动情节

时间:2018-12-19 15:57:09

标签: r shiny flexdashboard

我正在学习flexdashboard,但是我不清楚结构。

我创建了一个样本数据集,以根据所选变量在仪表板上绘制条形图。

代码如下:

---
title: "flexdashboard: Shiny Embedding"
output: 
  flexdashboard::flex_dashboard:
    social: menu
    source_code: embed 
runtime: shiny
---

```{r global, include=FALSE}
library(tidyverse)
## generate a sample dataset
sample<-tbl_df(data.frame(c("City1","City2","City3","City1","City2","City3",
"City2","City3"),
c("A","B","C","D","D","A","A","B"),
c(12,14,15,12,12,14,8,10)))
colnames(sample)<-c("City","Country","Amount")
df<- 
reactive(sample%>%group_by(input$variable)%>%summarise(total=sum(Amount)))

```

Column {.sidebar}
-------------------------------------------
```{r}
selectInput(inputId="variable",label="group by",choices=c("City","Country"))
```

Column
------------------------------------------
### Plot

```{r}
renderPlot({barplot(total)})
```

但是,我无法获得所需的东西。

一些建议或帮助?

1 个答案:

答案 0 :(得分:0)

一个简单的选择是使用参数而不是反应式上下文创建函数:

makePlot <- function(groupBy) { #Instead of a reactive context
    data <- sample%>%group_by(groupBy)%>%summarise(total=sum(Amount))
    return(barplot(data[[groupBy]])) #Here we use a dynamic height using a variable name
}

然后从您的renderPlot调用它:

renderPlot(makePlot(input$variable))