---
title: "Tutorial 4 Worksheet AY 19/20 Sem I"
subtitle: DSA2101
output:
  pdf_document:
    keep_tex: yes
  html_document: default
---

```{r message=FALSE}
poly_area <- function(xy_mat) {
  require(sp)
  tmp_p <- Polygons(list(Polygon(xy_mat)), "id")
  tmp_p@area
}

den_clusters <- readRDS("../data/dengue_cluster_2014-09-09.rds")
```

```{r echo=FALSE}
library(sp)
library(stringr)

get_coords <- function(x) {
  # str_extract(tmp$XY, "(.*?)(\\|)")
  # or str_split on | and take the first component
  # XY_str <- str_extract(x$XY, "^[^(\\|)]*")
  XY_str <- str_split(x$XY, "\\|")[[1]]
  XY_str <- str_split(XY_str, ",")
  XY_num <- sapply(XY_str, as.numeric)
  t(XY_num)
}

as.data.frame.clusters <- function(x) {
  XY <- lapply(x[[1]][-1], get_coords)
  areas <- sapply(XY, poly_area)/1e6
  
  descr <- sapply(x[[1]][-1], function(z) z$DESCRIPTION)
  num_cases <- sapply(x[[1]][-1], function(z) as.numeric(z[[2]]))
  data.frame(descr= descr, num_cases=num_cases, area = areas, 
             stringsAsFactors = FALSE)
}

summary.clusters <- function(x, ...) {
  df <- as.data.frame(x)
  num_clusters <- nrow(df)
  total_area <- sum(df$area)
  total_cases <- sum(df$num_cases)
  cat('Cluster Summary:\n')
  cat('----------------\n')
  cat('No. of clusters:\t', as.character(num_clusters), '\n')
  cat('No. of cases   :\t', as.character(total_cases), '\n')
  cat('Total area     :\t', as.character(round(total_area, 2)), 'km2 \n')
}
```


```{r eval=TRUE, echo=FALSE, message=FALSE}
library(rvest)

front_page <- read_html("https://www.r-bloggers.com/2017/05/")

pages_node <-  html_text( html_element(front_page, ".pagination .last") )
pages_node <- as.numeric(pages_node)

titles <- NULL
dates <- NULL

#for(ii in 1:pages_node) {
for(ii in 1:10) {
  page_url <- paste0("https://www.r-bloggers.com/2017/05/page/", ii, "/")
  read_page <- tryCatch(read_html(page_url),
                        error = function(e) return(NULL))
  if(is.null(read_page)) {
    cat(paste0("Page ", ii, " failed. Skipping it.\n"))
    next
  }
  title_nodes <- html_elements(read_page, ".loop-title a")
  titles <- c(titles, html_text(title_nodes))
  
  date_nodes <- html_elements(read_page, ".meta")
  dates <- c(dates, str_trim(str_split_i(html_text(date_nodes), "\\|", 1)))
  cat("Done with page ", ii, "..\n")
}

titles_df <- data.frame(titles= titles, dates= dates, stringsAsFactors = FALSE)
```


```{r test_001, autoharp.objs=c("titles_df"), autoharp.scalars=c("exists_titles")}
exists_titles <- exists("titles_df")
```

```{r test_002, autoharp.scalars=c("names_titles", "dim_titles")}
if(exists_titles) { 
  names_titles <- paste0(colnames(titles_df), collapse=",")
  dim_titles <- paste0(dim(titles_df), collapse=",")
} else {
  names_titles <- NA_character_
  dim_titles <- NA_character_
}
```

```{r test_003, autoharp.scalars=c("match_titles")}
if(exists_titles) { 
  match_titles <- setequal(.titles_df$titles, titles_df$titles)
} else {
  match_titles <- NA
}
```

```{r test_004, autoharp.scalars=c("rvest_fns")}
f1 <- rmd_to_forestharp(.myfilename)
rvest_fns <- fapply(f1, extract_fn_call, combine=TRUE,  
                    function(x) paste0(unique(unlist(x)), collapse=","), 
                    pkg_name="rvest")
```


```{r test_005, autoharp.objs=c("den_clusters"), autoharp.scalars=c("exists_as_df", "exists_summary")}
library(stringr) # these need to be loaded so that the function can be run
library(sp)

exists_as_df <- exists("as.data.frame.clusters")
exists_summary <- exists("summary.clusters")
```

```{r test_006, autoharp.scalars=c("class_as_df", "dim_as_df")}
if(exists_as_df) {
  df2 <- as.data.frame(.den_clusters)
  class_as_df <- paste0(class(df2), collapse=",")
  dim_as_df <- tryCatch(paste0(dim(df2), collapse=","), error = function(e) "error")
} else{
  class_as_df <- NA_character_
  dim_as_df <- NA_character_
}
```