EmpezarEmpieza gratis

Tu primer shinydashboard con elementos interactivos

Ya has visto cómo añadir elementos interactivos en un shinydashboard y también cómo incluir tablas de datos interactivas.

En este ejercicio, los anuncios de Airbnb se han guardado en un data frame llamado listings, y el mapa geoespacial se ha guardado como un objeto de leaflet llamado m_london. El encabezado y la barra lateral se han guardado como header y sidebar.

Funciones auxiliares personalizadas:

  • make_plots: Dibuja diagramas de caja o violin plots
  • num_listings: Calcula el número de anuncios según el rango seleccionado
  • num_private_rooms: Calcula el número de habitaciones privadas como porcentaje del total de anuncios, según el rango seleccionado
  • median_price: Calcula el precio mediano según el rango seleccionado Para saber más sobre una función auxiliar, puedes ejecutarla (por ejemplo, make_plots) en la consola.

Este ejercicio forma parte del curso

Creación de paneles con shinydashboard

Ver curso

Instrucciones del ejercicio

  • Añade box plots/violin plots de plotly llamados "plots".
  • Añade una tabla de datos llamada "table".
  • Añade un mapa de leaflet llamado "map".
  • Establece los argumentos correctos en dashboardPage().

ejercicio interactivo práctico

Prueba este ejercicio completando este código de ejemplo.

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)
Editar y ejecutar código