构建您的第一个包含交互元素的 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)