---
title: "Code for Session 1"
author: "Dr. Richard M. Crowley"
date: ""
output:
  html_notebook
---

Note that the directories used to store data are likely different on your computer, and such references will need to be changed before using any such code.

```{r helpers, warning=FALSE}
library(knitr)
library(kableExtra)
html_df <- function(text, cols=NULL, col1=FALSE, full=F) {
  if(!length(cols)) {
    cols=colnames(text)
  }
  if(!col1) {
    kable(text,"html", col.names = cols, align = c("l",rep('c',length(cols)-1))) %>%
      kable_styling(bootstrap_options = c("striped","hover"), full_width=full)
  } else {
    kable(text,"html", col.names = cols, align = c("l",rep('c',length(cols)-1))) %>%
      kable_styling(bootstrap_options = c("striped","hover"), full_width=full) %>%
      column_spec(1,bold=T)
  }
}
```

```{r , warning=FALSE}
options(knitr.table.format = "html") 
text <- data.frame(
  Day = c('Tuesday','Wednesday','Thursday','Thursday','Friday'),
  Time = c('1:45pm to 3:15pm','3:30pm to 5:00pm','10:30am to 11:30am','1:45pm to 3:15pm','1:45pm to 3:15pm'),
  Who = c('Prof Crowley','TA Mei Hui','TA Trenna','TA Joshua','Prof Crowley'),
  Where = c('SOA 4044','SOA 5043','SOA 5043','SOA 5043','SOA 4044'),
  Sections = c('All','G5, G6, G7', 'G5, G8', 'G6, G7, G8','All')
)
html_df(text, col1=TRUE)
```

```{r , warning=FALSE, results='hide',message=FALSE}
library(readr)
m <- list(
  l = 50,
  r = 50,
  b = 50,
  t = 50,
  pad = 4
)
suppressPackageStartupMessages(library(plotly))
suppressPackageStartupMessages(library(zoo))
act <- read_csv("accounting.csv")
tot <- read_csv("totals.csv")
df <- merge(act, tot, by="year")
df$percent_words = df$words/df$words_t
df$percent_books = df$books/df$books_t
year3 = df$year[3:length(df$year)-1]
percent_words_3 <- rollapply(zoo(df$percent_words), width=3, FUN=mean, align="center")
percent_books_3 <- rollapply(zoo(df$percent_books), width=3, FUN=mean, align="center")
df2 <- data.frame(year=year3, percent_words=percent_words_3, percent_books=percent_books_3)
```

```{r, warning=FALSE, message=FALSE, fig.height = 5, fig.width = 4.3}
plot_ly(df2[df$year>1800,], x = ~year, y = ~percent_books, type = 'scatter', mode = 'lines') %>%
   layout(xaxis = list(title="Year"), yaxis = list(title="Proportion of books"), title = "Books mentioning accounting (Google Ngrams)", margin = m)

```

```{r , warning=FALSE}
options(knitr.table.format = "html") 
text <- data.frame(
  Characteristic = c('Owner(s)', 'Liability for debt', 'Tax status'),
  Proprietorship = c('One owner (proprietor)','Proprietor is personally liable','Income tax passed through to owner'),
  Partnership = c('>1 owner, at least 1 general partner (GP), may have limited partners (LPs)','GPs are personally liable, LPs are not liable','Income tax passed through to partners'),
  Corporation = c('Shareholders, usually many, but could be as low as 1','Shareholders are not personally liable','Own legal entity, corporation taxed directly')
)
html_df(text, col1=TRUE)
```

```{r, results='hide',message=FALSE,warning=FALSE}
suppressPackageStartupMessages(library(rworldmap))
library(readr)
Ifrs <- read_csv("IFRS.csv")
#join to a coarse resolution map
spdf <- joinCountryData2Map(Ifrs, joinCode="NAME", nameJoinColumn="Country")
```

```{r map2, warning = FALSE, message = FALSE}
mapCountryData(spdf, nameColumnToPlot="IFRS", catMethod="fixedWidth", mapTitle="IFRS usage")
```

```{r, warning=FALSE}
text <- data.frame(
  Effect=c('Increase equity','Decrease equity'),
  Ordinary=c('Revenue','Expense'),
  Nonrd=c('Gain','Loss')
)
#label(text['Nonord']) <- "Not ordinary"
html_df(text, cols=c('Effect on equity', 'Ordinary activity', 'Not ordinary'), col1=T, full=F)
```

