首页 > 解决方案 > Flexdashboard 无法在同一降价上呈现 ggplotly 和 ggplot 对象

问题描述

我在这里有一个基本的可重现示例,我认为这可能只是一个包限制。我想知道我是不是做错了什么?它们都可以单独绘制,但是当组合在同一个降价中时,仪表板无法正确呈现。

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    source_code: embed
runtime: shiny
---

```{r setup, include=FALSE}
library(tidyverse)
library(plotly)
library(albersusa)

state_sf <- usa_sf("aeqd")

state_dat <- data.frame(state = c("Washington", "Wyoming","Texas","California"), pct = c(0.3,0.5,0.8,0.1))

state_map <- state_sf %>% 
  left_join(state_dat, by = c("name" = "state"))
```

Test
===================================== 

Sidebar {.sidebar data-width=200}
-------------------------------------

Testing

Row
-----------------------------------------------------------------------

###Plotly

```{r graph 1, fig.height=4, fig.width=6}
#Symptoms by state last week===================================================
ggplotly(
  ggplot(data = state_map) + 
    geom_sf(aes(fill=pct))
)
```

###Bar

```{r graph 2, fig.height=4, fig.width=3}
ggplot(data=state_dat) +
  geom_col(aes(state,pct,fill=pct)) 
```

标签: ggplot2shinyplotlyflexdashboardggplotly

解决方案


如果您正在使用,则runtime: shiny需要renderX()为每种类型的绘图对象使用正确类型的 Shiny 函数才能正确显示。我不知道为什么只有一个情节块(w/o renderX())有效,但有两个打破了它。

### Plotly

```{r graph_1, fig.height=4, fig.width=3}
#Symptoms by state last week
renderPlotly({
  ggplotly(
    ggplot(data = state_map) + 
    geom_sf(aes(fill=pct))
  )
})
```

### Bar

```{r graph_2, fig.height=4, fig.width=3}
renderPlot({
  ggplot(data=state_dat) +
    geom_col(aes(state,pct,fill=pct))
})
```

推荐阅读