Advertisement
jorandradefig

rmd-shiny.rmd

Jun 14th, 2021
2,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.65 KB | None | 0 0
  1. ---
  2. output: html_document
  3. runtime: shiny
  4. ---
  5.  
  6. # Tendencias en México
  7.  
  8. ```{r setup, echo=FALSE, include = FALSE}
  9. knitr::opts_chunk$set(echo = TRUE)
  10.  
  11. pacman::p_load(
  12.   tidyverse,
  13.   gtrendsR,
  14.   shiny,
  15.   sf,
  16.   leaflet,
  17.   plotly,
  18.   magrittr,
  19.   mapview,
  20.   plainview,
  21.   leafsync,
  22.   htmltools
  23. )
  24.  
  25. getwd()
  26.  
  27. statesJSON <- st_read("../input/mx-states.geo.json")
  28. ```
  29.  
  30. ```{r, echo=FALSE}
  31. inputPanel(
  32.   textInput(
  33.     inputId="keyword",
  34.     value="Uber Eats,Didi Food,Rappi",
  35.     label="Keyword:",
  36.     placeholder="Bitcoin"
  37.   ),
  38.   shiny::tags$style(
  39.     type="text/css",
  40.     ".shiny-flow-layout div {width: 100%; display: flex;  justify-content: center; align-items: center;} .shiny-input-container {display: flex; flex-direction: column; justify-content: center; align-items: center;} #keyword {  }"
  41.   )
  42. )
  43.  
  44. keyword <- reactive({
  45.   unlist(str_split(input$keyword,","))
  46. }) %>% debounce(500)
  47.  
  48. trends <- reactive({
  49.   trends <- gtrends(
  50.     keyword=keyword(),
  51.     geo="MX",
  52.     tz="America/Mexico_City",
  53.     time="now 7-d"
  54.   )
  55.   trendsOriginal <- trends
  56.   trends <- trends$interest_by_region
  57.   trends$name <- trends$location
  58.   trends %>%
  59.     mutate(
  60.       name=case_when(
  61.         name=="Mexico City" ~ "Ciudad De Mexico",
  62.         name=="State of Mexico" ~ "Estado De Mexico",
  63.         name=="Querétaro" ~ "Queretaro",
  64.         name=="Michoacán" ~ "Michoacan",
  65.         TRUE ~ name
  66.       )
  67.     )
  68. })
  69.  
  70. states <- reactive({
  71.   statesJSON %>% left_join(trends(),by="name")
  72. })
  73.  
  74. colors <- reactive({
  75.   colorNumeric(
  76.     palette="RdYlBu",
  77.     domain=states()$hits,
  78.     reverse=TRUE
  79.   )
  80. })
  81.  
  82. renderUI({
  83.   tagList(
  84.     lapply(keyword(),function(x){
  85.       data <- states() %>% filter(keyword == x)
  86.       leaflet(data) %>%
  87.         addTiles() %>%
  88.         setView(
  89.           lng=-102.5528,
  90.           lat=23.6345,
  91.           zoom=4
  92.         ) %>%
  93.         addPolygons(
  94.           weight=1,
  95.           color="#000000",
  96.           opacity=0.8,
  97.           fillOpacity=0.8,
  98.           fillColor=colors()(data$hits)
  99.         ) %>%
  100.         addLegend(
  101.           position="bottomleft",
  102.           pal=colors(),
  103.           values=data$hits,
  104.           title="Hits"
  105.         )
  106.     })
  107.   )
  108. })
  109.  
  110. renderPlotly({
  111.   trends() %>%
  112.     group_by(keyword) %>%
  113.     do(
  114.       p=plot_ly(.,
  115.         x=~reorder(name,hits),
  116.         y=~hits,
  117.         type="scatter",
  118.         mode="lines",
  119.         height=800
  120.       ) %>%
  121.       style(showlegend=FALSE)
  122.     ) %>%
  123.     subplot(
  124.       nrows=length(keyword()),
  125.       margin = 0.1
  126.     )
  127. })
  128.  
  129. renderTable({
  130.   trends() %>%
  131.     select(keyword,hits,name)
  132. },
  133. striped=TRUE,
  134. hover=TRUE,
  135. bordered=TRUE,
  136. width="100%"
  137. )
  138. ```
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement