---
title: "ACCT 101, Session 1: Welcome and intro to FA"
author: 
  - name: Dr. Richard M. Crowley
    email: rcrowley@smu.edu.sg
    affiliation: https://rmc.link/
title-slide-attributes:
  data-background-video: "../Backgrounds/title.mp4"
  data-background-video-loop: "true"
  data-background-size: stretch
format:
  revealjs:
    theme: default
    css: quarto_white.css
    navigation-mode: vertical
    controls: true
    transition: slide
    background-transition: fade
    transition-speed: default
    width: 1536
    height: 864
    logo: ../Logos/logo2.png
    code-link: true
    code-line-numbers: false
    chalkboard:
      theme: whiteboard
    auto-stretch: false
revealjs-plugins:
  - attribution
execute:
  echo: true
  warning: false
  message: false
editor: source
---

```{r, include=FALSE, message=F}
library(tidyverse)
library(magrittr)
df1 <- read.csv("../Data/About you-Edit-G1.csv", stringsAsFactors=FALSE) %>%
  rename(country_name=Where.did.you.grow.up...country.or.region.,
         cafe=What.is.your.favorite.cafe..coffee.shop..tea.shop..etc....N.A.if.you.don.t.have.one.)
df2 <- read.csv("../Data/About you-Edit-G2.csv", stringsAsFactors=FALSE) %>%
  rename(country_name=Where.did.you.grow.up...country.or.region.,
         cafe=What.is.your.favorite.cafe..coffee.shop..tea.shop..etc....N.A.if.you.don.t.have.one.)
df3 <- read.csv("../Data/About you-Edit-G3.csv", stringsAsFactors=FALSE) %>%
  rename(country_name=Where.did.you.grow.up...country.or.region.,
         cafe=What.is.your.favorite.cafe..coffee.shop..tea.shop..etc....N.A.if.you.don.t.have.one.)
ll <- read_tsv("../Data/countries_latlon.csv") %>%
  rename(country_name=name)
df1 %<>% left_join(ll)
df2 %<>% left_join(ll)
df3 %<>% left_join(ll)
```

# Section G1 {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## Where you are from {data-background="../Backgrounds/default.jpg"}

```{r, message=F, echo=F, warning=F}
library(leaflet)
leaflet(df1) %>%
  setView(lng = 103.8198, lat = 1.3521, zoom = 3) %>%
  addTiles() %>%
  addMarkers(lng = ~longitude, lat = ~latitude, clusterOptions = markerClusterOptions(), label=~country_name)
  
```


## Favorite cafe {data-background="../Backgrounds/default.jpg"}

```{r, echo=F, include=F}
# Note: The below code only works when run at SMU
library(jsonlite)
soa_ll <- c(1.2956192, 103.8498277)
find_cafe_ll <- function(coord_ll, radius, keyword) {
  # setup
  coord <- paste(coord_ll[1],coord_ll[2],sep=",")
  baseurl <- "https://maps.googleapis.com/maps/api/place/findplacefromtext/json?"
  # You'll need your own API key on Google Cloud to replicate this.
  # The good news is that the first $200 USD in charges are waived per month.
  api_key <- readRDS('google_maps_api_key.rds')
  
  # query
  q <- paste(baseurl,"input=", URLencode(keyword),"&inputtype=textquery","&locationbias=circle:",radius,"@",coord,"&fields=formatted_address,name,rating,geometry,place_id","&key=",api_key, sep="")

  #print(q)
  
  # GET data
  data1<-fromJSON(q)
  
  # Check if found
  if (data1$status == "ZERO_RESULTS") {
    return(NULL)
  }
  
  if (is.null(data1$candidates$rating)) {
    rating = ''
  } else {
    rating = data1$candidates$rating
  }
  
  # Return as a data.frame
  df_return <- data.frame(cafe=keyword,
                          cafe_name=data1$candidates$name,
                          lat=data1$candidates$geometry$location$lat,
                          long=data1$candidates$geometry$location$lng,
                          place_id=data1$candidates$place_id,
                          address=data1$candidates$formatted_address,
                          rating=rating,
                          stringsAsFactors = F)
  return(df_return)
}

cafes = unique(df1$cafe)
cafes = cafes[cafes!="NA" & cafes!="N/A" & cafes != "na" & cafes != "n/a" & !is.na(cafes) & cafes != ""]

if(!file.exists("google_maps_query-G1.rds")) {
  first = T
  for (cafe in cafes) {
    cafe_data <- find_cafe_ll(soa_ll,radius = 1000,cafe)
    if (!is.null(cafe_data)) {
      if (first) {
        df_cafe <- cafe_data
        first <- F
      } else {
        df_cafe <- rbind(df_cafe,cafe_data)
      }
    }
  }
  saveRDS(df_cafe,"google_maps_query-G1.rds")
} else {
  df_cafe <- readRDS("google_maps_query-G1.rds")
}
```


```{r, message=F, echo=F}
# Visualize Google maps data
df1 %>% left_join(df_cafe) %>%
  filter(!is.na(place_id)) %>%
  leaflet() %>%
  setView(lng = 103.8498277, lat = 1.2956192, zoom = 14) %>%
  addTiles() %>%
  addMarkers(lng = ~long, lat = ~lat, clusterOptions = markerClusterOptions(),
             label = ~paste0(cafe_name),
             popup = ~paste0('<div style="font-size:18px; padding:10px; line-height:20px">Query: ',cafe,
                             '<br><a href="https://www.google.com/maps/search/?api=1&query=Google&query_place_id=',place_id,'">Link on Gmaps</a>',
                             '<br>Rating on Gmaps: ',rating,
                             '<br>Address: ',address,'</div>'))
```

## What you like to do {data-background="../Backgrounds/default.jpg"}

```{r, echo=F, message=F}
library(waffle)  # Install from github only!  remotes::install_github("hrbrmstr/waffle")
library(rlang)

output_waffle_gg = function(data, cut=3, title="", ...) {
  dots <- ensyms(...)
  data2 <- data %>%
    select(!!!dots) %>%  # This is a quosure.  
    unlist()
  data2 <- data.frame(cats=data2, stringsAsFactors = F)
  data2 %<>%
    filter(cats != '') %>%
    group_by(cats) %>%
    mutate(count = n()) %>%
    dplyr::slice(1) %>%
    ungroup()
  cut = 0
  cells_cut = 0
  while (nrow(data2) > 10) {
    cut <- cut + 1
    cells_cut = cells_cut + sum(filter(data2,count <= cut)$count)
    data2 %<>% filter(count > cut)
  }
  data2 %>%
    ggplot(aes(fill=cats, values=count)) +
    geom_waffle(color="white", n_rows=5, size=1.125, na.rm=T) + 
    scale_x_discrete(expand=c(0,0)) +
    scale_y_discrete(expand=c(0,0)) + 
    ggthemes::scale_fill_tableau(name=NULL) + 
    labs(title=title, x = paste0("1 square is 1 response; Only responses with >",cut," (",cells_cut," items omitted)"))
}
```


```{r, echo=F}
df1 %>% output_waffle_gg(cut=3, "", Hobby.Norm1, Hobby.Norm2, Hobby.Norm3)
```

## What you look forward to {data-background="../Backgrounds/default.jpg"}

```{r, echo=F}
df1 %>% output_waffle_gg(3, "", Pro.Norm1, Pro.Norm2)
```

## What you *not* look forward to {data-background="../Backgrounds/default.jpg"}

```{r, echo=F}
df1 %>% output_waffle_gg(3, "", Con.Norm1)
```

# Section G2 {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## Where you are from {data-background="../Backgrounds/default.jpg"}

```{r, message=F, echo=F, warning=F}
leaflet(df2) %>%
  setView(lng = 103.8198, lat = 1.3521, zoom = 3) %>%
  addTiles() %>%
  addMarkers(lng = ~longitude, lat = ~latitude, clusterOptions = markerClusterOptions(), label=~country_name)
  
```


## Favorite cafe {data-background="../Backgrounds/default.jpg"}

```{r, echo=F, include=F}
# Note: The below code only works when run at SMU

cafes = unique(df2$cafe)
cafes = cafes[cafes!="NA" & cafes!="N/A" & cafes != "na" & cafes != "n/a" & !is.na(cafes) & cafes != ""]

if(!file.exists("google_maps_query-G2.rds")) {
  first = T
  for (cafe in cafes) {
    cafe_data <- find_cafe_ll(soa_ll,radius = 1000,cafe)
    if (!is.null(cafe_data)) {
      if (first) {
        df_cafe <- cafe_data
        first <- F
      } else {
        df_cafe <- rbind(df_cafe,cafe_data)
      }
    }
  }
  saveRDS(df_cafe,"google_maps_query-G2.rds")
} else {
  df_cafe <- readRDS("google_maps_query-G2.rds")
}
```


```{r, message=F, echo=F}
# Visualize Google maps data
df2 %>% left_join(df_cafe) %>%
  filter(!is.na(place_id)) %>%
  leaflet() %>%
  setView(lng = 103.8498277, lat = 1.2956192, zoom = 14) %>%
  addTiles() %>%
  addMarkers(lng = ~long, lat = ~lat, clusterOptions = markerClusterOptions(),
             label = ~paste0(cafe_name),
             popup = ~paste0('<div style="font-size:18px; padding:10px; line-height:20px">Query: ',cafe,
                             '<br><a href="https://www.google.com/maps/search/?api=1&query=Google&query_place_id=',place_id,'">Link on Gmaps</a>',
                             '<br>Rating on Gmaps: ',rating,
                             '<br>Address: ',address,'</div>'))
```

## What you like to do {data-background="../Backgrounds/default.jpg"}


```{r, echo=F}
df2 %>% output_waffle_gg(3, "", Hobby.Norm1, Hobby.Norm2, Hobby.Norm3)
```

## What you look forward to {data-background="../Backgrounds/default.jpg"}

```{r, echo=F}
df2 %>% output_waffle_gg(3, "", Pro.Norm1, Pro.Norm2)
```

## What you *not* look forward to {data-background="../Backgrounds/default.jpg"}

```{r, echo=F}
df2 %>% output_waffle_gg(3, "", Con.Norm1)
```



# Section G3 {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## Where you are from {data-background="../Backgrounds/default.jpg"}

```{r, message=F, echo=F, warning=F}
leaflet(df3) %>%
  setView(lng = 103.8198, lat = 1.3521, zoom = 3) %>%
  addTiles() %>%
  addMarkers(lng = ~longitude, lat = ~latitude, clusterOptions = markerClusterOptions(), label=~country_name)
  
```


## Favorite cafe {data-background="../Backgrounds/default.jpg"}

```{r, echo=F, include=F}
# Note: The below code only works when run at SMU

cafes = unique(df3$cafe)
cafes = cafes[cafes!="NA" & cafes!="N/A" & cafes != "na" & cafes != "n/a" & !is.na(cafes) & cafes != ""]

if(!file.exists("google_maps_query-G3.rds")) {
  first = T
  for (cafe in cafes) {
    cafe_data <- find_cafe_ll(soa_ll,radius = 1000,cafe)
    if (!is.null(cafe_data)) {
      if (first) {
        df_cafe <- cafe_data
        first <- F
      } else {
        df_cafe <- rbind(df_cafe,cafe_data)
      }
    }
  }
  saveRDS(df_cafe,"google_maps_query-G3.rds")
} else {
  df_cafe <- readRDS("google_maps_query-G3.rds")
}
```


```{r, message=F, echo=F}
# Visualize Google maps data
df3 %>% left_join(df_cafe) %>%
  filter(!is.na(place_id)) %>%
  leaflet() %>%
  setView(lng = 103.8498277, lat = 1.2956192, zoom = 14) %>%
  addTiles() %>%
  addMarkers(lng = ~long, lat = ~lat, clusterOptions = markerClusterOptions(),
             label = ~paste0(cafe_name),
             popup = ~paste0('<div style="font-size:18px; padding:10px; line-height:20px">Query: ',cafe,
                             '<br><a href="https://www.google.com/maps/search/?api=1&query=Google&query_place_id=',place_id,'">Link on Gmaps</a>',
                             '<br>Rating on Gmaps: ',rating,
                             '<br>Address: ',address,'</div>'))
```

## What you like to do {data-background="../Backgrounds/default.jpg"}


```{r, echo=F}
df3 %>% output_waffle_gg(3, "", Hobby.Norm1, Hobby.Norm2, Hobby.Norm3)
```

## What you look forward to {data-background="../Backgrounds/default.jpg"}

```{r, echo=F}
df3 %>% output_waffle_gg(3, "", Pro.Norm1, Pro.Norm2)
```

## What you *not* look forward to {data-background="../Backgrounds/default.jpg"}

```{r, echo=F}
df3 %>% output_waffle_gg(3, "", Con.Norm1)
```


# Review of feedback {data-background-video="../Backgrounds/section.mp4" data-background-video-loop="true"}

## Review of feedback {data-background="../Backgrounds/default.jpg"}

> Note: I won't go over the feedback each week explicitly

1. You guys seem really happy with the course so far!
2. The lecture slides are a big hit!
3. You also enjoyed the group activities
4. You also liked the examples and questions
5. What you didn't like: Most popular answer is "Nil"
6. Most common question: Can you release the slides earlier?
    - Subsequent weeks should be available Sunday morning (or earlier)
6. There are some difficulties with Equity vs Liabilities
    - This is a common difficulty, we will be working on it more this week and next

## Review of feedback {data-background="../Backgrounds/default.jpg"}

> Not asked this year, but a common question in the past:

7. How to take notes? A few options:
    1. Use your own [virtual or paper] notebook
    2. Take notes on the PDF copy of the slides (on eLearn and my website)
        - For Windows, you can use Adobe Reader, Drawboard, etc.
    3. Print out the slides and take notes on those
8. What will be on the final exam?
    - If it is in the slides, it can be on the exam
    - If it is not in the slides, it won't be on the exam

## Packages used for these slides {data-background="../Backgrounds/coding.jpg"}

- `package:jsonlite` with the Google Maps API
- `package:knitr`
- `package:leaflet`
- `package:magrittr`
- `package:revealjs`
- `package:rlang`
- `package:tidyverse`
- `package:waffle`


## NOTES {class="never"}


