Soluciones capítulo 8
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
<- read.csv("dat/listings.csv")
listings ```
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}
<- nrow(listings[listings$room_type == "Entire home/apt", ])
count_entire valueBox(round(100 * count_entire / nrow(listings)), icon = "fa-home")
```
### % habitaciones privadas
```{r}
<- nrow(listings[listings$room_type == "Private room", ])
count_private valueBox(round(100 * count_private / nrow(listings)), icon = "fa-home")
```
Row
-----------------------------------------------------------------------
### Distribución geográfica
```{r}
<- listings %>%
listings_1k sample_n(1000)
<- colorFactor("Set1", domain = unique(listings_1k$room_type))
paleta_tipo
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}
<- listings %>%
count_by_type group_by(room_type) %>%
summarise(count = n())
plot_ly(count_by_type, type = "bar", x = ~room_type, y = ~count)
```