【问题标题】:R flexdashboard and shiny interactive plotR flexdashboard 和闪亮的交互式绘图
【发布时间】:2019-05-20 03:53:08
【问题描述】:

我正在学习 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)})
```

但是,我无法得到我需要的东西。

有什么建议或帮助?

【问题讨论】:

    标签: r shiny flexdashboard


    【解决方案1】:

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

    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))
    

    【讨论】:

      猜你喜欢
      • 2021-10-14
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-03
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      相关资源
      最近更新 更多