From c1e4ffaef1adf6aa8aaf6403ec2f71a9a09dabc7 Mon Sep 17 00:00:00 2001
From: "B.Anderson" <ba1e12@srv02405.soton.ac.uk>
Date: Thu, 19 Dec 2019 23:39:38 +0000
Subject: [PATCH] place to store .Rmd with worked example to list all UKDA
 archive data files

---
 Rmd/README.md            |   5 ++
 Rmd/makefile_testFiles.R |  32 ++++++++++
 Rmd/testFiles.Rmd        | 132 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 169 insertions(+)
 create mode 100644 Rmd/README.md
 create mode 100644 Rmd/makefile_testFiles.R
 create mode 100644 Rmd/testFiles.Rmd

diff --git a/Rmd/README.md b/Rmd/README.md
new file mode 100644
index 0000000..38f5e61
--- /dev/null
+++ b/Rmd/README.md
@@ -0,0 +1,5 @@
+# Rmd
+
+A place to put .Rmd files.
+
+In general .Rmd scripts get called by .R makefiles so that knitted outputs can go into `/docs` for publishing via github/gitlab pages (if you wish).
\ No newline at end of file
diff --git a/Rmd/makefile_testFiles.R b/Rmd/makefile_testFiles.R
new file mode 100644
index 0000000..fe691c0
--- /dev/null
+++ b/Rmd/makefile_testFiles.R
@@ -0,0 +1,32 @@
+# runs testFiles.Rmd
+require(saveData)
+source(paste0(here::here(), "/.RProfile")) # re-sets project parameters
+
+# remember most repo parameters are in repoParams.R which gets sourced by .RProfile on
+# project open. XX need to test if still works if library loaded in a different project XX
+
+makeReport <- function(inF, outF){
+  # default = html
+  rmarkdown::render(input = inF,
+                    params = list(title = title,
+                                  subtitle = subtitle,
+                                  authors = authors),
+                    output_file = outF
+  )
+}
+
+# code ----
+
+# > Make report ----
+# >> yaml ----
+version <- "1.0"
+title <- paste0("SAVE Data")
+subtitle <- paste0("UKDA archive file listing report v", version)
+authors <- "Ben Anderson (b.anderson@soton.ac.uk)"
+
+# >> run report ----
+rmdFile <- "testFiles"
+inF <- paste0(projParams$projLoc, "/Rmd/", rmdFile,".Rmd")
+outF <- paste0(projParams$projLoc,"/docs/",rmdFile, "_" , version, ".html")
+
+makeReport(inF, outF)
diff --git a/Rmd/testFiles.Rmd b/Rmd/testFiles.Rmd
new file mode 100644
index 0000000..2367c77
--- /dev/null
+++ b/Rmd/testFiles.Rmd
@@ -0,0 +1,132 @@
+---
+params:
+  subtitle: ""
+  title: ""
+  authors: ""
+title: '`r params$title`'
+subtitle: '`r params$subtitle`'
+author: '`r params$authors`'
+date: 'Last run at: `r Sys.time()`'
+output:
+  bookdown::html_document2:
+    fig_caption: yes
+    code_folding: hide
+    number_sections: yes
+    toc: yes
+    toc_depth: 2
+    toc_float: TRUE
+  bookdown::pdf_document2:
+    fig_caption: yes
+    number_sections: yes
+  bookdown::word_document2:
+    fig_caption: yes
+    number_sections: yes
+    toc: yes
+    toc_depth: 2
+    fig_width: 5
+always_allow_html: yes
+bibliography: '`r paste0(here::here(), "/bibliography.bib")`'
+---
+
+```{r setup, include=FALSE}
+# Set start time ----
+startTime <- proc.time()
+
+# set params ----
+knitr::opts_chunk$set(echo = TRUE)
+
+# libraries
+require(data.table)
+require(kableExtra)
+
+# functions ----
+getFileList <- function(p){
+  # check for files in a given path
+  message("Checking for data files in ", p)
+  all.files <- list.files(path = p, recursive = TRUE, full.names = TRUE)
+  dt <- data.table::as.data.table(all.files)
+  for(f in dt$all.files){
+    fs <- file.size(f)
+    dt[all.files == f, fSize_Mb := fs/projParams$bytesToMbytes]
+  }
+  message("Found ", nrow(dt))
+  return(dt)
+}
+```
+
+# Introduction
+
+This report forms part of the `[saveData](https://git.soton.ac.uk/SERG/saveData)` R package [@saveData] and acts as both a test report to list all the files in the UKDA SAVE data archive and also a template that can be adapted for other reporting on this data.
+
+> NB: it will not run without the SAVE data [@saveUKDA] so you should [download it](xxx) before you start!
+
+# Data files
+
+## Documentation
+
+```{r docsFiles}
+p <- paste0(projParams$ukdaDataPath, "docs")
+files <- getFileList(p)
+
+if(nrow(files) > 0){
+  kableExtra::kable(files, caption = "Documentation files",
+                    digits = 2) %>%
+    kable_styling(bootstrap_options = c("striped", "hover"))
+}
+```
+
+## Survey data
+
+
+```{r surveyFiles}
+p <- paste0(projParams$ukdaDataPath, "survey")
+files <- getFileList(p)
+
+if(nrow(files) > 0){
+  kableExtra::kable(files, caption = "Survey data files",
+                    digits = 2) %>%
+    kable_styling(bootstrap_options = c("striped", "hover"))
+}
+```
+
+## Electricity consumption data
+
+
+```{r consumptionFiles}
+p <- paste0(projParams$ukdaDataPath, "consumption")
+files <- getFileList(p)
+
+if(nrow(files) > 0){
+  kableExtra::kable(files, caption = "Electricity consumption data files",
+                    digits = 2) %>%
+    kable_styling(bootstrap_options = c("striped", "hover"))
+}
+```
+
+# R environment
+
+```{r check runtime, include=FALSE}
+t <- proc.time() - startTime
+elapsed <- t[[3]]
+```
+
+Analysis completed in `r round(elapsed,2)` seconds ( `r round(elapsed/60,2)` minutes) using [knitr](https://cran.r-project.org/package=knitr) [@knitr] in [RStudio](http://www.rstudio.com) with `r R.version.string` running on `r R.version$platform`.
+
+## R packages
+
+ * base R [@baseR]
+ * bookdown [@bookdown]
+ * data.table [@data.table]
+ * here [@here]
+ * kableExtra [@kableExtra]
+ * knitr [@knitr]
+ * rmarkdown [@rmarkdown]
+
+## Session info
+
+```{r sessionInfo, echo=FALSE}
+sessionInfo()
+```
+
+# References
+
-- 
GitLab