params <-
structure(list(n = 100L, a = "My Name", d = "Sys.Date()"), .Names = c("n", 
"a", "d"))

## ----setup, include=FALSE------------------------------------------------
knitr::opts_chunk$set(echo = TRUE)
options(shiny.deprecation.messages=FALSE)
analystName <- "MyName" # (with no spaces, used for some  files produced and saved on disk)

## ----Install requirements, message=FALSE---------------------------------
if (!require(rmarkdown, quietly = TRUE))  {
  install.packages('rmarkdown', repos='http://cran.rediris.es') 
  }
if (!require(knitr, quietly = TRUE))     {
  install.packages('knitr', repos='http://cran.rediris.es') 
}
if (!require(plotly, quietly = TRUE))     {
  install.packages('plotly', repos='http://cran.rediris.es') 
}
if (!require(readr))     {
  install.packages('readr', repos='http://cran.rediris.es') 
  }
if (!require(xtable))     {
  install.packages('xtable', repos='http://cran.rediris.es') 
  }
if (!require(stargazer))     {
  install.packages('stargazer', repos='http://cran.rediris.es') 
  }
if (suppressPackageStartupMessages(!require(googleVis, quietly = TRUE)))  {
  install.packages('googleVis', repos='http://cran.rediris.es') 
}
if (!require("DT", quietly = T)) {
  install.packages('DT', repos = 'http://cran.rstudio.com')
}
if (!require("webshot", quietly = T)) {
  install.packages('webshot', repos = 'http://cran.rstudio.com')
}
if (!require("shiny", quietly = T)) {
  install.packages('shiny', repos = 'http://cran.rstudio.com')
}

## ----Generating and inserting a simple figure, echo=TRUE-----------------
x <- 1:10
y <- x^3
plot(x,y)

## ----Generating and inserting a simple figure using params defined in the markdown header, echo=TRUE----
x <- 1:params$n
y <- x^3
plot(x, 
     y, 
     xlab="My X label",
     ylab="My Y label", 
     main="My Chart Title",
     sub=paste0("Chart generated on ", params$d, " by ", params$a) # "sub" in a plot stands for subtitle
     )


## ----display code source "Yahoo Stock Data Pull.R", eval=FALSE-----------
##   source("https://raw.githubusercontent.com/royr2/StockPriceAnalytics/master/support/Yahoo%20Stock%20Data%20Pull.R")
## 
## AAPL <- GetYahooData("AAPL")
## IBM <- GetYahooData("IBM")

## ----run source "Yahoo Stock Data Pull.R", echo=FALSE--------------------
  source("https://raw.githubusercontent.com/royr2/StockPriceAnalytics/master/support/Yahoo%20Stock%20Data%20Pull.R")

AAPL <- GetYahooData("AAPL")
IBM <- GetYahooData("IBM")

## ----Inserting an interactive graph, echo=TRUE---------------------------
# Plotly chart 
library(plotly)
mat <-  data.frame(Date = AAPL$Date, 
                   AAPL = round(AAPL$Adj.Close,2),
                   IBM = round(IBM$Adj.Close,2))
 
p <- mat %>% 
  plot_ly(x = Date, y = AAPL, fill = "tozeroy", name = "Microsoft") %>% 
  add_trace(y = IBM, fill = "tonexty", name = "IBM") %>% 
  layout(title = "Stock Prices", 
         xaxis = list(title = "Time"),
         yaxis = list(title = "Stock Prices"))
p  # Thats it !

## ----Fetch the dataset InformeSalut2014_2010.csv-------------------------
require(knitr)

getwd()
datafile <- "InformeSalut2014_2010.csv"
download.file(url="https://seeds4c.org/tiki-download_file.php?fileId=453", destfile=datafile)
# Take 1 at reading a csv file into R, with default function read.csv
my.data <- read.csv(datafile, check.names=FALSE)
vnames <- colnames(my.data)
colnames(my.data) <- c("District", "Suburb", paste0("V", 1:13))
dim(my.data)
head(my.data)

# As you can see there is an extra column that came with a note, and not real data. Therefore, we can remove it
tablelegend <- cbind(colnames(my.data[1:14]), vnames[1:14])
tablelegend <- rbind(tablelegend, unlist(strsplit(vnames[15], ":")))
colnames(tablelegend) <- c("Variable Code", "Variable Description")
knitr::kable(tablelegend, caption = "Table with kable")

# As you can see, all columns where taken as factors, and not as numbers.
str(my.data)
# Take 2 at reading the file, this time with readr package, since it's better for performance and
# for self-detecting data types: numbers as numbers and not as factors, for instance, in this example.
my.data <- readr::read_csv(datafile)
vnames <- colnames(my.data)
my.data<-my.data[,-15]
colnames(my.data) <- c("District", "Suburb", paste0("V", 1:12))
str(my.data)

# Take 3 at reading the file, this time setting the locale properly to indicate that the dataset came with the decimal mark also as a comma, besides the field delimiter. Field delimiter also comes surrounded by quotation marks, therefore there is no confusion with this format.
# We will finally detect all numbers as numbers and not as factors
my.data <- readr::read_csv(datafile, locale=readr::locale("es", decimal_mark = ","))
vnames <- colnames(my.data)
my.data<-my.data[,-15]
colnames(my.data) <- c("District", "Suburb", paste0("V", 1:12))
str(my.data)


## ------------------------------------------------------------------------
my.ag.data <-aggregate(my.data[,3:14], by=list(my.data$District), 
  FUN=mean, na.rm=TRUE)
my.ag.data

## ----results = 'asis'----------------------------------------------------
my.subset <- subset(my.data, District == "Sants-Montjuïc", select = -V12)
knitr::kable(my.subset, caption = "Table with kable")

## ----results = 'asis'----------------------------------------------------
knitr::kable(head(my.data[,1:5]), caption = "Table with kable")

## ----results = "asis"----------------------------------------------------
print(xtable::xtable(head(my.data[,1:5]), caption = "Table with xtable"),
 type = "html", html.table.attributes = "border=1")

## ----results = "asis"----------------------------------------------------
stargazer::stargazer(head(my.data[,1:5]), type = "html",
 title = "Table with stargazer", summary=FALSE)

## ----Display my.data set in a Table, results='asis', tidy=TRUE-----------
  require(googleVis)
  #Set the googleVis options first to change the behaviour of plot.gvis, so that only the chart component of the HTML file is written into the output file.
  op <- options(gvis.plot.tag='chart')

  # Make a clone of my.data, with a first extra column for id or samples (with 2 digits for easy re-sorting later on)
  my.data.indexed <- cbind(sprintf("%02d", as.numeric(rownames(my.subset))), my.subset) 
  colnames(my.data.indexed)[1] <- "#"

  ## Table with enabled paging
  gvT <- gvisTable(my.data.indexed, options=list(page='disable',
                                             height='automatic',
                                             width='automatic'))
  plot(gvT)
  # save the googlevis table to disk

  # Assign file name for the my.data.indexed
  outFileName <- paste("my.subset", analystName, format(Sys.Date(), "%y%m%d"), "indexed.html", sep=".")

  # Display just the chart in the generated html
  cat(gvT$html$chart, file=outFileName)



## ------------------------------------------------------------------------
require('DT')
    d = data.frame(
      my.data,
      stringsAsFactors = FALSE
    )
    dt <- datatable(d, filter = 'bottom', options = list(pageLength = 5)) %>%
    formatStyle('V1',  
                color = styleInterval(c(0.5, 56), c('black', 'red', 'blue')),
                backgroundColor = styleInterval(56.5, c('snow', 'lightyellow')),
                fontWeight = styleInterval(58.0, c('italics', 'bold')))

## ------------------------------------------------------------------------
dt

## ------------------------------------------------------------------------
saveWidget(dt, paste("my.data", analystName, format(Sys.Date(), "%y%m%d"),'summary.html', sep="."))

## ------------------------------------------------------------------------
# Learn about API authentication here: https://plot.ly/r/getting-started
# Find your api_key here: https://plot.ly/settings/api

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 = 'Equirectangular') # Instead of the usual but very biased 'Mercator' projection
)

plot_ly(df, z = GDP..BILLIONS., text = COUNTRY, locations = CODE, type = 'choropleth',
        color = GDP..BILLIONS., colors = 'Blues', marker = list(line = l),
        colorbar = list(tickprefix = '$', title = 'GDP Billions US$')) %>%
  layout(title = '2014 Global GDP
Source:CIA World Factbook',
         geo = g)

## ----Choropleth Inset Map------------------------------------------------
df <- read.csv('https://raw.githubusercontent.com/plotly/datasets/master/2014_ebola.csv')
# restrict from June to September
df <- subset(df, Month %in% 6:9)
# ordered factor variable with month abbreviations
df$abbrev <- ordered(month.abb[df$Month], levels = month.abb[6:9])
# September totals
df9 <- subset(df, Month == 9)

# common plot options
g <- list(
  scope = 'africa',
  showframe = F,
  showland = T,
  landcolor = toRGB("grey90")
)

g1 <- c(
  g,
  resolution = 50,
  showcoastlines = T,
  countrycolor = toRGB("white"),
  coastlinecolor = toRGB("white"),
  projection = list(type = 'Mercator'),
  list(lonaxis = list(range = c(-15, -5))),
  list(lataxis = list(range = c(0, 12))),
  list(domain = list(x = c(0, 1), y = c(0, 1)))
)


g2 <- c(
  g,
  showcountries = F,
  bgcolor = toRGB("white", alpha = 0),
  list(domain = list(x = c(0, .6), y = c(0, .6)))
)

plot_ly(df, type = 'scattergeo', mode = 'markers', locations = Country,
        locationmode = 'country names', text = paste(Value, "cases"),
        color = as.ordered(abbrev), marker = list(size = Value/50), inherit = F) %>%
  add_trace(type = 'scattergeo', mode = 'text', geo = 'geo2', showlegend = F,
            lon = 21.0936, lat = 7.1881, text = 'Africa') %>%
  add_trace(type = 'choropleth', locations = Country, locationmode = 'country names',
            z = Month, colors = "black", showscale = F, geo = 'geo2', data = df9) %>%
  layout(title = 'Ebola cases reported by month in West Africa 2014<br> Source: <a href="https://data.hdx.rwlabs.org/dataset/rowca-ebola-cases">HDX</a>',
         geo = g1, geo2 = g2)


## ---- echo = TRUE, eval=FALSE--------------------------------------------
## source("https://raw.githubusercontent.com/rstudio/rmdexamples/master/R/kmeans_cluster.R")
## kmeans_cluster(iris)

## ----Create a plot.ly chart out of the previous dataset------------------
require(knitr)

getwd()
datafile <- "InformeSalut2014_2010.csv"
download.file(url="https://seeds4c.org/tiki-download_file.php?fileId=453", destfile=datafile)

# Get all data in place
my.data <- readr::read_csv(datafile, locale=readr::locale("es", decimal_mark = ","))
vnames <- colnames(my.data)
my.data<-my.data[,-15]
colnames(my.data) <- c("District", "Suburb", paste0("V", 1:12))
str(my.data)

# As you can see there is an extra column that came with a note, and not real data. Therefore, we can remove it
tablelegend <- cbind(colnames(my.data[1:14]), vnames[1:14])
tablelegend <- rbind(tablelegend, unlist(strsplit(vnames[15], ":")))
colnames(tablelegend) <- c("Variable Code", "Variable Description")
knitr::kable(tablelegend, caption = "Table with kable")

# Simple interactive scatter chart
library(plotly)

# Add some info about variables displayed
cat(paste0("V3: ", tablelegend[5,2]),
    paste0("\nV5: ", tablelegend[7,2]),
    paste0("\nV6: ", tablelegend[8,2]),
    paste0("\nV11: ", tablelegend[13,2]))

plot_ly(my.data, 
        x = V5, y = V6, text = paste("Over-aging: ", V1, 
                                     "Income: ", V3,
                                     "Fecundity: ", V11,
                                     "Suburb: ", Suburb),
        mode="marker",
        size = V3, opacity = V3,
        group = District)

## ----eval=FALSE----------------------------------------------------------
## ---
## title: "Markdown, Automation & HTML Reports (UBISS16, Jul 6)"
## author: "Xavier de Pedro, Ph.D. xavier.depedro@vhir.org - https://seeds4c.org/ubiss16d3"
## date: "July 6, 2016"
## output:
##   html_document:
##     toc: true
##     number_sections: true
##     toc_depth: 3
##     toc_float:
##       collapsed: true
##       smooth_scroll: true
##     theme: united
##   pdf_document:
##     toc: true
##     highlight: zenburn
## runtime: static
## params:
##   n: 100
##   d: !r Sys.Date()
##   a: "My Name"
## ---

## ----extract R from Rmd, eval=FALSE--------------------------------------
## wd <- "/home/xavi/Dropbox/2016_SummeR_School_UB_HospClinic/day3"
## myfile <- file.path(wd, "ubiss16_d3_Markdown_Automation_Html_Reports.Rmd")
## knitr::purl(myfile)

## ----Generate pdf output from Rmd, eval=FALSE----------------------------
## wd <- "/home/xavi/Dropbox/2016_SummeR_School_UB_HospClinic/day3"
## myfile <- file.path(wd, "ubiss16_d3_Markdown_Automation_Html_Reports.Rmd")
## render(myfile, "pdf_document")
## 
## # You can also render all formats defined in an input file with:
## render(myfile, "all")

## ----show session info---------------------------------------------------
sessionInfo()

## ------------------------------------------------------------------------
## Set options back to original options
options(op)

