闪亮的 flexdashboard:新选项卡集中的参考对象
shiny flexdashboard: reference object in new tabset
我有一个带有一列和两个标签集的 flexdashboard。我想在一个标签集中创建和绘制 dat2
,然后在第二个标签集中显示 dat2
的数据。真正的用例更复杂,我没有将它设置为 运行 从全局(也许我需要弄清楚如何做到这一点)。
以下导致无法定位dat2
的错误。
---
title: "test"
output:
flexdashboard::flex_dashboard:
theme: bootstrap
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
```
```{r global, include=FALSE}
set.seed(1)
dat <- data.frame(age = sample(15:99, 100, replace=TRUE),
y = runif(100))
```
Sidebar {.sidebar}
=====================================
```{r}
# age
sliderInput("agerange", label = "Age",
min = 15,
max = 99,
value = c(15, 99),
step=10)
```
Page 1
=====================================
Column {.tabset}
-----------------------------------------------------------------------
### Plot
```{r}
renderPlot({
dat2 <-
dat %>%
filter(age >= input$agerange[1] & age <= input$agerange[2]) %>%
mutate(y2 = y*2)
ggplot(dat2, aes(y2)) +
geom_histogram()
})
```
Column {.tabset}
-----------------------------------------------------------------------
### Table
```{r}
DT::renderDataTable({
DT::datatable(dat2, options = list(bPaginate = FALSE))
})
```
这似乎可行(基于 this SO answer)。那里有更简单的修复,或者是在反应式中进行修改的基本方法,分配给反应式内部的 object
,然后在 render
函数中重新分配该对象(例如,object <- object()
) ?
---
title: "test"
output:
flexdashboard::flex_dashboard:
theme: bootstrap
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
```
```{r global, include=FALSE}
set.seed(1)
dat <- data.frame(age = sample(15:99, 100, replace=TRUE),
y = runif(100))
```
Sidebar {.sidebar}
=====================================
```{r}
# age
sliderInput("agerange", label = "Age",
min = 15,
max = 99,
value = c(15, 99),
step=10)
```
Page 1
=====================================
Column {.tabset}
-----------------------------------------------------------------------
### Plot
```{r}
dat2 <- reactive(
dat %>%
filter(age >= input$agerange[1] & age <= input$agerange[2]) %>%
mutate(y2 = y*2)
)
renderPlot({
dat2 <- dat2()
ggplot(dat2, aes(y2)) +
geom_histogram()
})
```
Column {.tabset}
-----------------------------------------------------------------------
### Table
```{r}
DT::renderDataTable({
dat2 <- dat2()
DT::datatable(dat2, options = list(bPaginate = FALSE))
})
```
我有一个带有一列和两个标签集的 flexdashboard。我想在一个标签集中创建和绘制 dat2
,然后在第二个标签集中显示 dat2
的数据。真正的用例更复杂,我没有将它设置为 运行 从全局(也许我需要弄清楚如何做到这一点)。
以下导致无法定位dat2
的错误。
---
title: "test"
output:
flexdashboard::flex_dashboard:
theme: bootstrap
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
```
```{r global, include=FALSE}
set.seed(1)
dat <- data.frame(age = sample(15:99, 100, replace=TRUE),
y = runif(100))
```
Sidebar {.sidebar}
=====================================
```{r}
# age
sliderInput("agerange", label = "Age",
min = 15,
max = 99,
value = c(15, 99),
step=10)
```
Page 1
=====================================
Column {.tabset}
-----------------------------------------------------------------------
### Plot
```{r}
renderPlot({
dat2 <-
dat %>%
filter(age >= input$agerange[1] & age <= input$agerange[2]) %>%
mutate(y2 = y*2)
ggplot(dat2, aes(y2)) +
geom_histogram()
})
```
Column {.tabset}
-----------------------------------------------------------------------
### Table
```{r}
DT::renderDataTable({
DT::datatable(dat2, options = list(bPaginate = FALSE))
})
```
这似乎可行(基于 this SO answer)。那里有更简单的修复,或者是在反应式中进行修改的基本方法,分配给反应式内部的 object
,然后在 render
函数中重新分配该对象(例如,object <- object()
) ?
---
title: "test"
output:
flexdashboard::flex_dashboard:
theme: bootstrap
runtime: shiny
---
```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
```
```{r global, include=FALSE}
set.seed(1)
dat <- data.frame(age = sample(15:99, 100, replace=TRUE),
y = runif(100))
```
Sidebar {.sidebar}
=====================================
```{r}
# age
sliderInput("agerange", label = "Age",
min = 15,
max = 99,
value = c(15, 99),
step=10)
```
Page 1
=====================================
Column {.tabset}
-----------------------------------------------------------------------
### Plot
```{r}
dat2 <- reactive(
dat %>%
filter(age >= input$agerange[1] & age <= input$agerange[2]) %>%
mutate(y2 = y*2)
)
renderPlot({
dat2 <- dat2()
ggplot(dat2, aes(y2)) +
geom_histogram()
})
```
Column {.tabset}
-----------------------------------------------------------------------
### Table
```{r}
DT::renderDataTable({
dat2 <- dat2()
DT::datatable(dat2, options = list(bPaginate = FALSE))
})
```