Soluciones capítulo 8

Author

Luz Frias

Para probar que se genera correctamente el dashboard con flexdashboard, tendrás que copiar el código a un .Rmd y renderizarlo p.e. desde RStudio.

---
title: "Airbnb en Madrid"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(plotly)
library(dplyr)
library(leaflet)
```

```{r}
# Lectura y tratamiento de datos
listings <- read.csv("dat/listings.csv")
```

Row
-----------------------------------------------------------------------

### Número de alojamientos

```{r}
valueBox(nrow(listings), icon = "fa-home")
```

```{r}
valueBox(nrow(listings), icon = "fa-home")
```

### Precio medio por noche

```{r}
valueBox(round(mean(listings$price)), icon = "fa-euro")
```


### % alojamientos completos

```{r}
count_entire <- nrow(listings[listings$room_type == "Entire home/apt", ])
valueBox(round(100 * count_entire / nrow(listings)), icon = "fa-home")
```


### % habitaciones privadas

```{r}
count_private <- nrow(listings[listings$room_type == "Private room", ])
valueBox(round(100 * count_private / nrow(listings)), icon = "fa-home")
```


Row
-----------------------------------------------------------------------

### Distribución geográfica

```{r}
listings_1k <- listings %>%
  sample_n(1000)

paleta_tipo <- colorFactor("Set1", domain = unique(listings_1k$room_type))

leaflet(listings_1k) %>%
  addProviderTiles("CartoDB.Positron") %>%
  setView(lng = -3.69, lat = 40.43, zoom = 12) %>%
  addCircleMarkers(lat = ~latitude, lng = ~longitude, color = ~paleta_tipo(room_type),
                   stroke = FALSE, radius = 2, fillOpacity = 0.5) %>%
  addLegend(
    position = "bottomright",
    pal      = paleta_tipo,
    values   = ~room_type,
    title    = "Tipo de habitación"
  )
```

### Alojamientos por tipo de habitación

```{r}
count_by_type <- listings %>%
  group_by(room_type) %>%
  summarise(count = n())

plot_ly(count_by_type, type = "bar", x = ~room_type, y = ~count)
```