Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---
- output: html_document
- runtime: shiny
- ---
- # Tendencias en México
- ```{r setup, echo=FALSE, include = FALSE}
- knitr::opts_chunk$set(echo = TRUE)
- pacman::p_load(
- tidyverse,
- gtrendsR,
- shiny,
- sf,
- leaflet,
- plotly,
- magrittr,
- mapview,
- plainview,
- leafsync,
- htmltools
- )
- getwd()
- statesJSON <- st_read("../input/mx-states.geo.json")
- ```
- ```{r, echo=FALSE}
- inputPanel(
- textInput(
- inputId="keyword",
- value="Uber Eats,Didi Food,Rappi",
- label="Keyword:",
- placeholder="Bitcoin"
- ),
- shiny::tags$style(
- type="text/css",
- ".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 { }"
- )
- )
- keyword <- reactive({
- unlist(str_split(input$keyword,","))
- }) %>% debounce(500)
- trends <- reactive({
- trends <- gtrends(
- keyword=keyword(),
- geo="MX",
- tz="America/Mexico_City",
- time="now 7-d"
- )
- trendsOriginal <- trends
- trends <- trends$interest_by_region
- trends$name <- trends$location
- trends %>%
- mutate(
- name=case_when(
- name=="Mexico City" ~ "Ciudad De Mexico",
- name=="State of Mexico" ~ "Estado De Mexico",
- name=="Querétaro" ~ "Queretaro",
- name=="Michoacán" ~ "Michoacan",
- TRUE ~ name
- )
- )
- })
- states <- reactive({
- statesJSON %>% left_join(trends(),by="name")
- })
- colors <- reactive({
- colorNumeric(
- palette="RdYlBu",
- domain=states()$hits,
- reverse=TRUE
- )
- })
- renderUI({
- tagList(
- lapply(keyword(),function(x){
- data <- states() %>% filter(keyword == x)
- leaflet(data) %>%
- addTiles() %>%
- setView(
- lng=-102.5528,
- lat=23.6345,
- zoom=4
- ) %>%
- addPolygons(
- weight=1,
- color="#000000",
- opacity=0.8,
- fillOpacity=0.8,
- fillColor=colors()(data$hits)
- ) %>%
- addLegend(
- position="bottomleft",
- pal=colors(),
- values=data$hits,
- title="Hits"
- )
- })
- )
- })
- renderPlotly({
- trends() %>%
- group_by(keyword) %>%
- do(
- p=plot_ly(.,
- x=~reorder(name,hits),
- y=~hits,
- type="scatter",
- mode="lines",
- height=800
- ) %>%
- style(showlegend=FALSE)
- ) %>%
- subplot(
- nrows=length(keyword()),
- margin = 0.1
- )
- })
- renderTable({
- trends() %>%
- select(keyword,hits,name)
- },
- striped=TRUE,
- hover=TRUE,
- bordered=TRUE,
- width="100%"
- )
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement