---
title: "Plot surface texture parameters for data mining"
author: "Ivan Calandra"
date: "`r Sys.time()`"
output:
  html_document:
    toc: true
    toc_depth: 5
    toc_float: true
    theme: cerulean
    highlight: pygments
params:
  data: 
    value: "Data/brushing_v2.Rbin"
---

<!-- Define the Rbin-file to use on line 14 --> 
<!-- This Rbin-file must be in a 'Data' folder within the directory of the present Rmd file --> 

<!-- Define numeric variables to plot on line 75 --> 
<!-- Define X, symbol and color variables to plot on line 92 --> 

---

Rbin-file used for this script: **`r paste0("~/", params$data)`**


```{r Knitr Options, include=FALSE}
	knitr::opts_chunk$set(comment=NA, message=FALSE, indent="", error=TRUE)
```

---


### 1. Goal of the script
```{r, echo=FALSE}
dir.plots <- "Plots"
dir.create(dir.plots, showWarnings=FALSE)
```

The script will plot all variables to see which ones should be used for further analysis.  
Scatterplot of each variable will be plotted.  
All plots will be saved in a subfolder "`r dir.plots`" within the directory of the present Rmd file, i.e. "`r paste("~",dir.plots,sep='/')`".


---


### 2. Load packages
```{r}
library(R.utils)
library(ggplot2)
library(tools)
```


---


### 3. Load data into R object
```{r}
imp.info <- file.info(params$data)
imp.data <- loadObject(params$data)
```

The imported file is: "`r paste0("~/", params$data)`"  
Its modification, 'last status change' (= 'creation' on Windows) and last access times are, respectively: "`r imp.info$mtime`", "`r imp.info$ctime`" and "`r imp.info$atime`".


---


### 4. Prepare variables
#### 4.1. Define numeric variables
```{r}
num.var <- 21:length(imp.data)
```

The following variables will be used: 

```{r, echo=FALSE}
for (i in num.var) cat("[",i,"] ", names(imp.data)[i], "\n", sep="")
```


#### 4.2. Define grouping variable
```{r}
imp.data[["Sample.Area"]] <- paste(imp.data[["Sample"]], imp.data[["Area"]], sep="_")
```

---


### 5. Plot each of the selected numeric variable (1 plot for flint, 1 plot for quartzite)
```{r}
for (i in num.var){
	#plot
	p <- ggplot(data=imp.data, aes_string(x="Before.after", y=names(imp.data)[i], color="Dirt", shape="Brush")) + geom_point(size=3) + geom_line(aes(group=Sample.Area))
	p <- p + geom_text(aes(label=ifelse(imp.data[["Before.after"]]=="Before",gsub("FLT|QTZ","",imp.data$Sample.Area),"")), nudge_x=-0.4)
	p <- p + expand_limits(x=0) + theme_classic() + facet_wrap(~Material)
	print(p)
	
	#save to PDF
	file.out <- paste0(basename(file_path_sans_ext(row.names(imp.info))), "_plot_", names(imp.data)[i], ".pdf")
	ggsave(filename=file.out, plot=p, path=dir.plots, device="pdf", width=21, units="cm")
}
```

The following plot(s) was (were) created and saved in "`r paste0('~/',dir.plots)`":  
```{r, echo=FALSE}
files.out <- list.files(path=paste(getwd(),dir.plots,sep="/"), pattern=basename(file_path_sans_ext(row.names(imp.info))))
files.out
```


---

```{r}
sessionInfo()
```

RStudio Version `r readLines("RStudioVersion.txt")`

---

END OF SCRIPT