Advertisement
jorandradefig

Untitled

Jun 4th, 2021
1,477
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.34 KB | None | 0 0
  1. ---
  2. output: html_document
  3. runtime: shiny
  4. ---
  5.  
  6. ```{r setup, include=FALSE}
  7. knitr::opts_chunk$set(echo = TRUE)
  8.  
  9. pacman::p_load(
  10.   tidyverse,
  11.   gtrendsR,
  12.   sf,
  13.   leaflet
  14. )
  15. ```
  16.  
  17. # Tendencias de México
  18.  
  19. ```{r eruptions, echo=FALSE}
  20.  
  21. statesJSON <- st_read("mx-states.geo.json")
  22.  
  23. ```
  24.  
  25. ```{r}
  26.  
  27. inputPanel(
  28.   textInput(
  29.     inputId="keyword",
  30.     label="Término de búsqueda:",
  31.     value="Google I/O",
  32.     placeholder="Bitcoin"
  33.   )
  34. )
  35.  
  36. ```
  37.  
  38. ```{r states}
  39.  
  40. colors <- colorNumeric("RdYlBu",domain=states$hits)
  41.  
  42. renderLeaflet({
  43.   # 23.6345° N, 102.5528° W
  44.   leaflet(states) %>%
  45.     addTiles() %>%
  46.     setView(lat=23.6345,lng=-102.5528,zoom=4) %>%
  47.     addPolygons(
  48.       weight=1,
  49.       color="#232323",
  50.       opacity=0.8,
  51.       fillOpacity=0.8,
  52.       fillColor=colors(states$hits)
  53.     )
  54. })
  55. ```
  56.  
  57. ```{r statesJSON}
  58.  
  59. renderTable({
  60.   trends <- gtrends(
  61.     keyword=input$keyword,
  62.     geo="MX",
  63.     tz="America/Mexico_City",
  64.     time="now 7-d"
  65.   )
  66.   trends <- trends$interest_by_region
  67.   trends$name <- trends$location
  68.   trends <- trends %>%
  69.     mutate(
  70.       name=case_when(
  71.        name=="State of Mexico" ~ "Estado De Mexico",
  72.        name=="Mexico City" ~ "Ciudad De Mexico",
  73.        !is.na(name) ~ name
  74.       )
  75.     )
  76.   states <- statesJSON %>% left_join(trends,by="name")
  77.   trends %>% select(-name)
  78. })
  79. ```
  80.  
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement