インタラクティブ要素付きの最初の shinydashboard
この章では、shinydashboard にインタラクティブな要素を追加する方法と、インタラクティブなデータテーブルを追加する方法を学びました。
この演習では、Airbnb のリスティングが listings というデータフレームに格納され、地理空間マップは m_london という leaflet オブジェクトに格納されています。ヘッダーとサイドバーはそれぞれ header と sidebar に格納されています。
カスタムのヘルパー関数:
make_plots: 箱ひげ図またはバイオリンプロットを描画しますnum_listings: 選択した範囲に基づいてリスティング数を計算しますnum_private_rooms: 選択した範囲に基づいて、全リスティングに占める個室の割合を計算しますmedian_price: 選択した範囲に基づいて中央値価格を計算します ヘルパー関数の詳細を知りたい場合は、コンソールで(make_plotsなどを)実行して確認できます。
この演習はコースの一部です
shinydashboard で作るダッシュボード
演習の手順
"plots"という名前のplotlyの箱ひげ図/バイオリンプロットを追加します。"table"という名前のデータテーブルを追加します。"map"という名前のleafletマップを追加します。dashboardPage()の引数を正しく設定します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
body <- dashboardBody(
tabItems(
tabItem(tabName = "charts",
fluidRow(
valueBoxOutput(outputId = "count"), valueBoxOutput(outputId = "prop"),valueBoxOutput(outputId = "med") ),
fluidRow(
tabBox(side = "left", id = "tabset", height = "500px",
tabPanel("Charts",
# Place plotly object here
fluidRow(box(___("plots", height = 500, width = 600)) ) ),
# Place dataTable object here
tabPanel("Data table", height = "500px", ___("table")) ),
box(side = "right", height = "200px", title = "Welcome to London!",),
box(side = "right", height = "385px", title = "Controls",
sliderInput(inputId = "range", label = "Select price range:",
min = 0, max = 25000,value = c(0,2500)),
selectInput(inputId = "select", label = "Select group:",
choices = c("Box plots", "Violin plots")) ) ) ),
tabItem(tabName = "map",
# Place leaflet object here
fluidRow(box(title = "Map of listings in London", ___("map", height = 600, width = 700))) ) ) )
# Set the correct arguments for dashboardPage()
ui <- dashboardPage(___, ___, ___)
server <- function(input, output) {
output$count <- renderValueBox(valueBox("Number of listings", num_listings(input$range),
icon = icon("house-user") ))
output$prop <- renderValueBox(valueBox("Private rooms", paste0(num_private_rooms(input$range), "% of all listings"),
icon = icon("eye"), color = "orange") )
output$med <- renderValueBox(valueBox("Median price", paste0(median_price(input$range), "£"),
icon = icon("money-bill-alt"), color = "olive") )
output$plots <- renderPlotly(make_plots(input$range, input$select))
output$table <- renderDataTable(filter(listings, price >= input$range[1], price <= input$range[2]) %>% select(c(name, neighbourhood, room_type, price)),
options = list(lengthMenu = c(5, 30, 50)))
output$map <- renderLeaflet(m_london)
}
shinyApp(ui, server)