Notes

Since I am going to talk about Data Science topics, I thought to start this blog with a nice visualisation about the keyword Data Science

The data are taken from Google Trend via the R library gtrendsR. The page is static and the data in the next tabs have been updated on: Thursday 14 February 2019

This is a demo of how to use a Flexdashboard embeded inside a website made with Blogdown. Since the framework of Flexdashboard is not compatible with the one of Blogdown, I saved the Rmarkdown which generated this code in a different folder from content–>post and then I saved the html ouput in the public folder. In the folder content–>post I saved a post with an iframe calling this html output.

To create the interactions with the menu I used the Crosstalk library.

By country

In which location the term Data Science is most popular. Values are calculated on a scale from 0 to 100, where 100 is the location with the most popularity as a fraction of total searches in that location.

By time

---
title: "Google trend for Data Science"
output: 
  flexdashboard::flex_dashboard:
    theme: cosmo
    orientation: rows
    vertical_layout: scroll
    self_contained: yes
    source_code: embed
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
	echo = FALSE,
	message = FALSE,
	warning = FALSE
)
library(plyr)
library(crosstalk)
```

```{r eval=FALSE, include=FALSE}
devtools::install_github("kent37/summarywidget")
```

```{r KEYWORD}
KEYWORD <- "Data Science"
```

Notes
===

```{r include=FALSE}
library(lubridate)
actualtime <- format (now(), "%A %d %B %Y")
```

Since I am going to talk about Data Science topics, I thought to start this blog with a nice visualisation about the keyword **`r KEYWORD`**

The data are taken from **Google Trend** via the R library **gtrendsR**.
The page is static and the data in the next tabs have been updated on:
**`r actualtime`**

This is a demo of how to use a [Flexdashboard](https://rmarkdown.rstudio.com/flexdashboard/) embeded inside a website made with **Blogdown**. 
Since the framework of Flexdashboard is not compatible with the one of Blogdown, I saved the Rmarkdown which generated this code in a different folder from *content-->post*  and then I saved the html ouput in the public folder.
In the folder *content-->post* I saved a post with an iframe calling this html output.

To create the interactions with the menu I used the [Crosstalk](https://rstudio.github.io/crosstalk/) library.


```{r gtrendsR, eval=TRUE, message=FALSE, warning=FALSE, include=FALSE, paged.print=FALSE}

library(gtrendsR)
  
trend90days <- gtrends(KEYWORD, time ="today 3-m")
trend12months <- gtrends(KEYWORD, time ="today 12-m")
trend5years <- gtrends(KEYWORD, time ="today+5-y")

trend90days_time <- trend90days$interest_over_time
trend90days_time$period <- "Last 90 days"
trend90days_time$hitstime <- trend90days_time$hits

trend12months_time <- trend12months$interest_over_time
trend12months_time$period <- "Last 12 months"
trend12months_time$hitstime <- trend12months_time$hits

trend5years_time <- trend5years$interest_over_time
trend5years_time$period <- "Last 5 years"
trend5years_time$hitstime <- trend5years_time$hits

combined_time <- rbind.fill(trend12months_time,trend5years_time, trend90days_time)

library(plotly)
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_world_gdp_with_codes.csv')

# light grey boundaries
l <- list(color = toRGB("grey"), width = 0.5)

# specify map projection/options
g <- list(
  showframe = FALSE,
  showcoastlines = FALSE,
  projection = list(type = 'Mercator')
)

trend90days_COUNTRIES <- trend90days$interest_by_country
trend90days_COUNTRIES <- trend90days_COUNTRIES[,c(1,2)]

colnames(trend90days_COUNTRIES)[which(names(trend90days_COUNTRIES) == "location")] <- "COUNTRY"

trend90days_COUNTRIES$COUNTRY <- ifelse(trend90days_COUNTRIES$COUNTRY=="Czechia","Czech Republic",trend90days_COUNTRIES$COUNTRY )
trend90days_COUNTRIES$present <- 1
trend90days_COUNTRIES$hitscountry <- trend90days_COUNTRIES$hits
library(dplyr)
trend90days_COUNTRIES <- df %>% left_join(trend90days_COUNTRIES, by="COUNTRY")
# sum(is.na(trend12months_COUNTRIES$hits))
trend90days_COUNTRIES[is.na(trend90days_COUNTRIES)] <- 0
trend90days_COUNTRIES <- trend90days_COUNTRIES[,c(1,3,4,5,6)]
trend90days_COUNTRIES$period <- "Last 90 days"

trend12months_COUNTRIES <- trend12months$interest_by_country
trend12months_COUNTRIES <- trend12months_COUNTRIES[,c(1,2)]
colnames(trend12months_COUNTRIES)[which(names(trend12months_COUNTRIES) == "location")] <- "COUNTRY"
trend12months_COUNTRIES$COUNTRY <- ifelse(trend12months_COUNTRIES$COUNTRY=="Czechia","Czech Republic",trend12months_COUNTRIES$COUNTRY  )
trend12months_COUNTRIES$present <- 1
trend12months_COUNTRIES$hitscountry <- trend12months_COUNTRIES$hits
library(dplyr)
trend12months_COUNTRIES <- df %>% left_join(trend12months_COUNTRIES, by="COUNTRY")
# sum(is.na(trend12months_COUNTRIES$hits))
trend12months_COUNTRIES[is.na(trend12months_COUNTRIES)] <- 0
trend12months_COUNTRIES <- trend12months_COUNTRIES[,c(1,3,4,5,6)]
trend12months_COUNTRIES$period <- "Last 12 months"

trend5years_COUNTRIES <-trend5years$interest_by_country
trend5years_COUNTRIES <- trend5years_COUNTRIES[,c(1,2)]
colnames(trend5years_COUNTRIES)[which(names(trend5years_COUNTRIES) == "location")] <- "COUNTRY"
trend5years_COUNTRIES$COUNTRY <- ifelse(trend5years_COUNTRIES$COUNTRY=="Czechia","Czech Republic",trend5years_COUNTRIES$COUNTRY  )
trend5years_COUNTRIES$present <- 1
trend5years_COUNTRIES$hitscountry <- trend5years_COUNTRIES$hits
trend5years_COUNTRIES <- df %>% left_join(trend5years_COUNTRIES, by="COUNTRY")
trend5years_COUNTRIES[is.na(trend5years_COUNTRIES)] <- 0
trend5years_COUNTRIES <- trend5years_COUNTRIES[,c(1,3,4,5,6)]
trend5years_COUNTRIES$period <- "Last 5 years"

combined_COUNTRIES <- rbind.fill(trend90days_COUNTRIES,trend12months_COUNTRIES,trend5years_COUNTRIES)
combined_time <- combined_time[,c(1,2,7,8)]

combined_ALL <- rbind.fill(combined_time,combined_COUNTRIES)


combined_ALL$present[is.na(combined_ALL$present)] <- 0
combined_ALL$hitscountry[is.na(combined_ALL$hitscountry)] <- 0
combined_ALL$hitstime[is.na(combined_ALL$hitstime)] <- 0

combined_ALL$hits <- as.numeric(combined_ALL$hits)
combined_ALL$hits[is.na(combined_ALL$hits)] <- 0
```

By country
===
In which location the term **`r KEYWORD`** is most popular. Values are calculated on a scale from 0 to 100, where 100 is the location with the most popularity as a fraction of total searches in that location. 




```{r gtrendR by region, echo=FALSE}
library(summarywidget)
SD = crosstalk::SharedData$new(combined_ALL)
filter_select("period", "Select a period", SD, ~period, multiple = FALSE)
title_plot2 <- paste0("Interest by country for ",KEYWORD,"")
crosstalk_countries <- plot_geo(SD) %>%
  add_trace(
    z = ~hits, color = ~hits, colors = 'Blues',
    text = ~COUNTRY, locations = ~CODE, marker = list(line = l)
  ) %>%
  colorbar(title = "") %>%
  layout(
    title = title_plot2,
    geo = g
  )
count_countries <- summarywidget(SD, statistic='sum', column='present', digits=0)
mean_hits_countries <-summarywidget(SD, 'mean', 'hitscountry',selection=~present==1, digits=2)

# In the selected period there are **`r count_countries`** countries with at least 1 hit, with an average of **`r mean_hits_countries`** hits per country 
```


```{r output country crosstalk, echo=FALSE, fig.height=8}
crosstalk_countries
```

By time
===

```{r gtrendR, echo=FALSE, fig.height=8, message=FALSE, warning=FALSE}
title_layout <- paste0("Interest over time for ", KEYWORD)
SDtime = crosstalk::SharedData$new(combined_time)

bscols( widths = c(2,NA),
  list(
  filter_select("period", "Select a period", SDtime, ~period, multiple =  FALSE)  ),
       
    plot_ly(SDtime, x = ~date, y = ~hits, mode = 'lines')%>%
  layout(
    title = title_layout,
      xaxis = list(title = "Time"),
      yaxis = list(title = "Search hits"), width="100%", height=600)
  )

```