r - SQLite 和 Shiny/flexdashboard:无法在 SQL 查询中嵌入 reactiveValues() 对象
问题描述
我正在尝试过滤 sqlite3 数据库并打印/绘制结果数据。例如nycflights13::weather
数据。但在 flexdashboard 环境中,它抱怨无法在 SQL 查询中嵌入反应式对象。这是否意味着我不能将 SQLite 与 flexdashboard 一起使用?
---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(DBI)
library(dplyr)
library(tidyr)
library(pool)
con <- dbConnect(RSQLite::SQLite(), dbdir=":memory:")
DBI::dbWriteTable(con, "weather", nycflights13::weather)
bigdf<-tbl(con, "weather")
```
Column {.sidebar}
----------------------------
```{r}
selectInput("year", label = "year :",
choices = c(2012,2013,2010))
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
a<-reactive({
a<-
bigdf%>%
filter(origin=="EWR" & year == as.integer(input$year)) %>%
head(25)%>%
collect()
})
renderTable(a())
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart B
```{r}
reactive({
plot(a()$year,a()$wind_speed)
})
```
解决方案
我稍微调整了代码以使用!!
建议的修复程序,并使b
绘图成为反应对象,然后可以通过调用renderPlot
.
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(DBI)
library(dplyr)
library(tidyr)
library(pool)
con <- dbConnect(RSQLite::SQLite(), dbdir=":memory:")
DBI::dbWriteTable(con, "weather", nycflights13::weather)
bigdf<-tbl(con, "weather")
```
Column {.sidebar}
----------------------------
```{r}
selectInput("year", label = "year :",choices = c(2013,2012,2010),selected = 2013)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A
```{r}
a <- reactive({
a <-
bigdf %>%
filter(origin=="EWR" & year == as.integer(!!input$year)) %>%
head(25) %>%
collect()
a # make sure to return something
})
renderTable(a())
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart B
```{r}
b <- reactive({ # make the plot a reactive object
plot(a()$year, a()$wind_speed)
})
renderPlot(b()) # then render it
```
推荐阅读
- reactjs - 如何放置 Graphql 查询以从反应组件中的 Wordpress API 获取数据,而不是 GatsbyJS 中的模板?
- python - Python 2.7 PIP 安装以使用 Artifactory 作为代理
- mysql - 如何在完全导入期间在 Apache Solr 中创建新列?
- python - 我想从 HTML 中抓取数据并将其转换为 pandas DataDrame 最后将其存储为 aCSV 文件
- unity3d - 为什么刚体上的 OnMouseDown() 事件不会触发?
- laravel - 每个租户的多租户和自定义功能
- sql - 仅显示 MAX 行
- algorithm - 图中所有可能的路径
- arrays - 如何在执行快速排序时正确找到枢轴元素?
- javascript - 数字计数器将数字从 1 增加到最大值,然后从该值减少到 1。但它应该增加然后停止