簡體   English   中英

ggplotly 不能在 shiny 應用程序中工作,plot 沒有出現

[英]ggplotly not working in shiny app, plot not showing up

我目前正在制作 Shiny 應用程序,我嘗試使用此圖(在服務器中使用):

  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")
  
})

這在應用程序中顯示得很好!

在此處輸入圖像描述

現在,我嘗試在其中集成 plotly 並基本上從工作應用程序中復制了這段代碼。 我還查閱了堆棧並找到了類似的答案。

  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 的方式有問題嗎? 就像我之前說的,我在這個論壇上看了類似的答案,基本上試圖復制粘貼很多都無濟於事。

我最終通過一些實驗找到了解決方案!

  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM