開始使用免費開始

加入互動式圖表輸出

和建立互動式表格類似,你可以把用 ggplot2 繪製的靜態圖,透過 plotly 套件輕鬆轉成互動式圖表。若要繪製互動式圖表,使用 plotly::renderPlotly(),並用 plotly::plotlyOutput() 在介面上顯示。

記得,就像其他 render 函式一樣,plotly::renderPlotly() 裡的程式碼必須包在大括號 {} 中!

本練習屬於課程

使用 R 的 Shiny 建立網頁應用程式

檢視課程

練習說明

  • 建立依性別與年份的前 10 名熱門名字的互動式圖表。將輸出命名為 "plot_trendy_names",並使用 plot_trends() 來產生圖表。
  • 在 UI 中顯示該圖表。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

ui <- fluidPage(
  selectInput('name', 'Select Name', top_trendy_names$name)
  # CODE BELOW: Add a plotly output named 'plot_trendy_names'
  
)

server <- function(input, output, session){
  # Function to plot trends in a name
  plot_trends <- function(){
     babynames %>% 
      filter(name == input$name) %>% 
      ggplot(aes(x = year, y = n)) +
      geom_col()
  }
  # CODE BELOW: Render a plotly output named 'plot_trendy_names'

  
  
}

shinyApp(ui = ui, server = server)
編輯並執行程式碼