【问题标题】:ggplotly 不能在闪亮的应用程序中工作,情节没有出现
【发布时间】:2022-01-21 17:00:32
【问题描述】:

我目前正在制作一个闪亮的应用程序,并尝试使用此图(在服务器中使用):

  output$COTY_out1 <- renderPlot({
  
  data %>%
    group_by(year) %>%
    summarize(mean_ls = mean(life_satisfaction, na.rm = TRUE)) %>%
    ggplot(mapping = aes(x = year, y = mean_ls)) +
    geom_line() +
    geom_smooth() +
    labs(y = "Mean Life Satisfaction", x = "Year", title = "World")
  
})

这在应用程序中显示得很好!

现在,我尝试在其中集成,并且基本上从工作应用程序中复制了这段代码。我还查阅了堆栈并找到了类似的答案。

  output$COTY_out1 <- renderPlotly({

p <- data %>%
  group_by(year) %>%
  summarize(mean_ls = mean(life_satisfaction, na.rm = TRUE)) %>%
  ggplot(mapping = aes(x = year, y = mean_ls)) +
  geom_line() +
  geom_smooth() +
  labs(y = "Mean Life Satisfaction", x = "Year", title = "World")

p <- ggplotly(p)
p
        
})

但是,执行此操作后,图表不会显示。我集成 ggplotly 的方式有问题吗?就像我之前说的,我在这个论坛上查看了类似的答案,基本上尝试复制粘贴很多都无济于事。

【问题讨论】:

  • 只需用last_plot() 拨打ggplotly 吗?您可能需要使用 print,也许我记得 shiny 有时需要这样做。
  • 我有点困惑,您是不是建议不要将我的 ggplot 重命名为 p,而是按原样编写情节,然后执行 ggplotly(last_plot())?对不起,我是闪亮的新手。
  • 是的,但这可能是不利的,具体取决于最后一个情节是什么,或者您可以将 ggplot 调用通过管道传递给ggplotly()
  • 您的代码看起来不错。但是没有a minimal reproducible example,只能猜测。如何在 UI 中添加绘图图表?你用过plotlyOutput吗?

标签: r ggplot2 shiny r-plotly ggplotly


【解决方案1】:

我最终通过一些实验找到了解决方案!

  output$COTY_out1 <- renderPlotly({

data %>%
    group_by(year) %>%
    summarize(mean_ls = mean(life_satisfaction, na.rm = TRUE)) %>%
    ggplot(mapping = aes(x = year, y = mean_ls)) +
    geom_line() +
    geom_smooth() +
    labs(y = "Mean Life Satisfaction", x = "Year", title = "World")
  
})

确保在 UI 中使用 plotlyOutput

【讨论】:

    猜你喜欢
    • 2019-06-19
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2017-11-07
    • 2020-10-25
    • 2016-12-25
    相关资源
    最近更新 更多