当动态过滤和构面在数据中造成问题时,如何避免在Flexdashboard图表注释中出现错误消息?例如,这是一个示例,其中选择输入导致没有数据。
---
title: "Example"
runtime: shiny
output:
flexdashboard::flex_dashboard:
vertical_layout: fill
---
```{r}
library(tidyverse)
library(flexdashboard)
```
### Chart 1
```{r}
df <- fortify(forecast::gold)
fillCol(height = "100%", flex = c(1, NA),
plotOutput("plot", height = "100%"),
wellPanel(
tags$style(".well {background-color:#ffffff; border-color:#ffffff;}"),
radioButtons("myInput", label = NULL,
choices = list("All" = "all",
"Problematic filtering" = "filter"),
selected = "all", inline=T)
)
)
output$plot <- renderPlot({
ggplot(df, aes(x, y)) +
geom_line()
})
notes <- reactive({
if (input$myInput=="all") {
df %>%
summarise(mean = mean(y, na.rm=TRUE)) %>%
pull(mean)
} else {
df %>%
filter(x==0) %>%
summarise(mean = mean(y, na.rm=TRUE)) %>%
pull(mean)
}
})
# new attempt
notes_ <- reactive({
notes <- notes()
if (exists("notes") & !is.na(notes) & !is.nan(notes)) {
print(notes)
} else {
print("[not available]")
}
})
```
> The mean is `r notes_`.
尝试
这将打印“ [不可用]”(上面已集成)。有更好的方法吗?
# new attempt
notes_ <- reactive({
notes <- notes()
if (exists("notes") & !is.na(notes) & !is.nan(notes)) {
print(notes)
} else {
print("[not available]")
}
})
```
> The mean is `r notes_`.
答案 0 :(得分:0)
这将打印“ [不可用]”。开放寻求更好的解决方案...
---
title: "Example"
runtime: shiny
output:
flexdashboard::flex_dashboard:
vertical_layout: fill
---
```{r}
library(tidyverse)
library(flexdashboard)
```
### Chart 1
```{r}
df <- fortify(forecast::gold)
fillCol(height = "100%", flex = c(1, NA),
plotOutput("plot", height = "100%"),
wellPanel(
tags$style(".well {background-color:#ffffff; border-color:#ffffff;}"),
radioButtons("myInput", label = NULL,
choices = list("All" = "all",
"Problematic filtering" = "filter"),
selected = "all", inline=T)
)
)
output$plot <- renderPlot({
ggplot(df, aes(x, y)) +
geom_line()
})
notes <- reactive({
if (input$myInput=="all") {
df %>%
summarise(mean = mean(y, na.rm=TRUE)) %>%
pull(mean)
} else {
df %>%
filter(x==0) %>%
summarise(mean = mean(y, na.rm=TRUE)) %>%
pull(mean)
}
})
notes_ <- reactive({
notes <- notes()
if (exists("notes") & !is.na(notes) & !is.nan(notes)) {
print(notes)
} else {
print("[not available]")
}
})
```
> The mean is `r notes_`.