diff --git a/DESCRIPTION b/DESCRIPTION index 5bca52e..694307a 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: epuRate -Title: My personal rmd template for every day analysis +Title: A clean template for R Markdown documents Version: 0.1 -Authors@R: person("First", "Last", email = "first.last@example.com", +Authors@R: person("Yan", "Holtz", email = "yan.holtz.data@gmail.com", role = c("aut", "cre")) Description: What the package does (one paragraph) Depends: R (>= 3.1.0) diff --git a/R/epurate.R b/R/epurate.R index 257d30b..36f1cee 100644 --- a/R/epurate.R +++ b/R/epurate.R @@ -1,10 +1,10 @@ library(rmarkdown) +# Features of the Epurate Template epurate <- function(toc = TRUE, code_folding = "hide", number_sections=TRUE) { # get the locations of resource files located within the package css <- system.file("rmarkdown", "templates", "epurate" ,"resources", "style.css", package = "epuRate") - header <- system.file("rmarkdown", "templates", "epurate" ,"resources", "header.html", package = "epuRate") template <- system.file("rmarkdown", "templates", "epurate" ,"resources", "template_epurate.html", package = "epuRate") # call the base html_document function @@ -17,11 +17,11 @@ epurate <- function(toc = TRUE, code_folding = "hide", number_sections=TRUE) { number_sections= number_sections, df_print = "paged", code_folding = code_folding, - includes = includes(before_body = header) ) } +# Features of PCTG Template PCTG <- function(toc = TRUE, code_folding = "hide", number_sections=TRUE) { # get the locations of resource files located within the package @@ -40,12 +40,4 @@ PCTG <- function(toc = TRUE, code_folding = "hide", number_sections=TRUE) { code_folding = code_folding, includes = includes(after_body = footer) ) - } - - - - - - - diff --git a/build.R b/build.R new file mode 100644 index 0000000..d2e8529 --- /dev/null +++ b/build.R @@ -0,0 +1,17 @@ +# ------------------------------- # +# Install Package From Source +# ------------------------------- # + +# Remove previous version just in Case +remove.packages("epuRate") + +# Install with devtools +library(devtools) +devtools::install("../epuRate") + +# Load to check it is all good +library(epuRate) + +# Run one of the skeleton to make sure it works? +library(rmarkdown) +render("inst/rmarkdown/templates/epurate/skeleton/skeleton.html") diff --git a/create_template_tuto.R b/create_template_tuto.R deleted file mode 100644 index a5ff90f..0000000 --- a/create_template_tuto.R +++ /dev/null @@ -1,50 +0,0 @@ -#Tutorial on: -#http://ismayc.github.io/ecots2k16/template_pkg/ - -# Set working directory -setwd("~/Dropbox/YH_template/") - -# Need devtools -library(devtools) - -# Create my template! -#devtools::create("yhPersoTemplate") - -# Create skeleton directory -#dir.create("yhPersoTemplate/inst/rmarkdown/templates/report/skeleton", recursive = TRUE) - -# Create skeleton.rmd file -# Create .yaml file - -# install package: -remove.packages("epuRate") -devtools::install("epuRate") -library(epuRate) - - - - - - - - - -# Call the library from Github? -library(devtools) -remove.packages("epuRate") -install_github("holtzy/epuRate", force=TRUE) -library(epuRate) - - - - - - - - - - - - - - diff --git a/inst/rmarkdown/templates/basic/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/basic/skeleton/skeleton.Rmd index b5758e1..e012c7e 100644 --- a/inst/rmarkdown/templates/basic/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/basic/skeleton/skeleton.Rmd @@ -1,7 +1,7 @@ --- title: "A clean and uncluttered template" output: - html_document: + html_document --- # title 1 diff --git a/inst/rmarkdown/templates/basic/skeleton/skeleton.html b/inst/rmarkdown/templates/basic/skeleton/skeleton.html new file mode 100644 index 0000000..9403806 --- /dev/null +++ b/inst/rmarkdown/templates/basic/skeleton/skeleton.html @@ -0,0 +1,161 @@ + + + + + + + + + + + + + +A clean and uncluttered template + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+

title 1

+
+
+

title 2

+
+ + + + +
+ + + + + + + + diff --git a/inst/rmarkdown/templates/epurate/resources/header.html b/inst/rmarkdown/templates/epurate/resources/header.html index 2366ca6..ab50175 100644 --- a/inst/rmarkdown/templates/epurate/resources/header.html +++ b/inst/rmarkdown/templates/epurate/resources/header.html @@ -1 +1 @@ - + diff --git a/inst/rmarkdown/templates/epurate/resources/style.css b/inst/rmarkdown/templates/epurate/resources/style.css index 4dc41c4..a83315f 100644 --- a/inst/rmarkdown/templates/epurate/resources/style.css +++ b/inst/rmarkdown/templates/epurate/resources/style.css @@ -2,6 +2,10 @@ h1, .h1, h2, .h2, h3, .h3 { margin-top: 84px; } +h1 { + padding-top: 100px; +} + h1.title { text-align: center; } @@ -12,15 +16,36 @@ pre { } + + + + + +/* ---------- Links ----------------- */ +a { + color: #69b3a2; +} +a:hover { + color: #69b3a2; +} + + + + + +/* ---------- Button ----------------- */ .btn { border-width: 0 0px 0px 0px; font-weight: normal; text-transform: ; + color: #69b3a2; } .btn-default { - color: #2ecc71; + color: #69b3a2; background-color: #ffffff; border-color: #ffffff; } - +.nav-pills > li.active > a, .nav-pills > li.active > a:hover, .nav-pills > li.active > a:focus { + background-color: #69b3a2; +} diff --git a/inst/rmarkdown/templates/epurate/resources/template_epurate.html b/inst/rmarkdown/templates/epurate/resources/template_epurate.html index a461081..15b53d5 100644 --- a/inst/rmarkdown/templates/epurate/resources/template_epurate.html +++ b/inst/rmarkdown/templates/epurate/resources/template_epurate.html @@ -8,6 +8,10 @@ + + + + $if(theme)$ $else$ @@ -458,7 +462,7 @@

$author$ - $date$

 
-

A work by $author$

+

A work by $author$

$mail$

diff --git a/inst/rmarkdown/templates/epurate/skeleton/skeleton.Rmd b/inst/rmarkdown/templates/epurate/skeleton/skeleton.Rmd index 3117524..31e2b1b 100644 --- a/inst/rmarkdown/templates/epurate/skeleton/skeleton.Rmd +++ b/inst/rmarkdown/templates/epurate/skeleton/skeleton.Rmd @@ -5,7 +5,7 @@ date: "`r format(Sys.time(), '%d %B %Y')`" mail: "yan.holtz.data@gmail.com" linkedin: "yan-holtz-2477534a" twitter: "r_graph_gallery" -github: "holtzy" +github: "tutu" output: epuRate::epurate: toc: FALSE diff --git a/inst/rmarkdown/templates/epurate/skeleton/skeleton.html b/inst/rmarkdown/templates/epurate/skeleton/skeleton.html new file mode 100644 index 0000000..fbf09c5 --- /dev/null +++ b/inst/rmarkdown/templates/epurate/skeleton/skeleton.html @@ -0,0 +1,407 @@ + + + + + + + + + + + + + + + + + + +A clean and uncluttered template + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + +



+
+

I find it not too bad to start with a couple of sentences of introduction. Explain briefly what this document is going to talk about. May be add a useful link relative to this project

+
+
+

1 Get the data

+
+

Usually I always start by loading libraries. The tidyverse is probably the package I use the most. Quite difficult to imagine analysing data without it. It is a good practice to load all the packages in the same time. It allows to know exactly what is needed to reproduce your analysis.

+
library(tidyverse)
+library(rmarkdown)    # You need this library to run this template.
+library(epuRate)      # Install with devtools: install_github("holtzy/epuRate", force=TRUE)
+

Then I explain what is my input data and I load it. Here is how it looks like:

+
# Show it:
+mtcars %>% head(5)
+
+ +
+

This file has 32 lines and 11 columns. It is ready to be analysed.

+
+
+

2 A few analysis

+
+

Here are 3 version of a circular barplot that you can find in the R graph gallery. Note that the figure is centered. You can change the size of the plot with fig.width and fig.height

+
+

2.1 basic

+

Let’s start with a basic version of a circular representation of the data

+
# Create dataset
+data=data.frame(
+  individual=paste( "Mister ", seq(1,60), sep=""),
+  value=sample( seq(10,100), 60, replace=T)
+)
+
+# Set a number of 'empty bar'
+empty_bar=10
+ 
+# Add lines to the initial dataset
+to_add = matrix(NA, empty_bar, ncol(data))
+colnames(to_add) = colnames(data)
+data=rbind(data, to_add)
+data$id=seq(1, nrow(data))
+ 
+# Get the name and the y position of each label
+label_data=data
+number_of_bar=nrow(label_data)
+angle= 90 - 360 * (label_data$id-0.5) /number_of_bar     # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
+label_data$hjust<-ifelse( angle < -90, 1, 0)
+label_data$angle<-ifelse(angle < -90, angle+180, angle)
+ 
+# Make the plot
+p = ggplot(data, aes(x=as.factor(id), y=value)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar
+  geom_bar(stat="identity", fill=alpha("green", 0.3)) +
+  ylim(-100,120) +
+  theme_minimal() +
+  theme(
+    axis.text = element_blank(),
+    axis.title = element_blank(),
+    panel.grid = element_blank(),
+    plot.margin = unit(rep(-1,4), "cm") 
+  ) +
+  coord_polar(start = 0) + 
+  geom_text(data=label_data, aes(x=id, y=value+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) 
+ 
+p
+

+
+
+

2.2 grouped

+
# Create dataset
+data=data.frame(
+  individual=paste( "Mister ", seq(1,60), sep=""),
+  group=c( rep('A', 10), rep('B', 30), rep('C', 14), rep('D', 6)) ,
+  value=sample( seq(10,100), 60, replace=T)
+)
+
+# Set a number of 'empty bar' to add at the end of each group
+empty_bar=4
+to_add = data.frame( matrix(NA, empty_bar*nlevels(data$group), ncol(data)) )
+colnames(to_add) = colnames(data)
+to_add$group=rep(levels(data$group), each=empty_bar)
+data=rbind(data, to_add)
+data=data %>% arrange(group)
+data$id=seq(1, nrow(data))
+ 
+# Get the name and the y position of each label
+label_data=data
+number_of_bar=nrow(label_data)
+angle= 90 - 360 * (label_data$id-0.5) /number_of_bar     # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
+label_data$hjust<-ifelse( angle < -90, 1, 0)
+label_data$angle<-ifelse(angle < -90, angle+180, angle)
+ 
+# Make the plot
+p = ggplot(data, aes(x=as.factor(id), y=value, fill=group)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar
+  geom_bar(stat="identity", alpha=0.5) +
+  ylim(-100,120) +
+  theme_minimal() +
+  theme(
+    legend.position = "none",
+    axis.text = element_blank(),
+    axis.title = element_blank(),
+    panel.grid = element_blank(),
+    plot.margin = unit(rep(-1,4), "cm") 
+  ) +
+  coord_polar() + 
+  geom_text(data=label_data, aes(x=id, y=value+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) 
+ 
+p
+

+
+
+

2.3 complex

+
# Create dataset
+data=data.frame(
+  individual=paste( "Mister ", seq(1,60), sep=""),
+  group=c( rep('A', 10), rep('B', 30), rep('C', 14), rep('D', 6)) ,
+  value=sample( seq(10,100), 60, replace=T)
+)
+ 
+# Set a number of 'empty bar' to add at the end of each group
+empty_bar=3
+to_add = data.frame( matrix(NA, empty_bar*nlevels(data$group), ncol(data)) )
+colnames(to_add) = colnames(data)
+to_add$group=rep(levels(data$group), each=empty_bar)
+data=rbind(data, to_add)
+data=data %>% arrange(group)
+data$id=seq(1, nrow(data))
+ 
+# Get the name and the y position of each label
+label_data=data
+number_of_bar=nrow(label_data)
+angle= 90 - 360 * (label_data$id-0.5) /number_of_bar     # I substract 0.5 because the letter must have the angle of the center of the bars. Not extreme right(1) or extreme left (0)
+label_data$hjust<-ifelse( angle < -90, 1, 0)
+label_data$angle<-ifelse(angle < -90, angle+180, angle)
+ 
+# prepare a data frame for base lines
+base_data=data %>% 
+  group_by(group) %>% 
+  summarize(start=min(id), end=max(id) - empty_bar) %>% 
+  rowwise() %>% 
+  mutate(title=mean(c(start, end)))
+ 
+# prepare a data frame for grid (scales)
+grid_data = base_data
+grid_data$end = grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
+grid_data$start = grid_data$start - 1
+grid_data=grid_data[-1,]
+ 
+# Make the plot
+p = ggplot(data, aes(x=as.factor(id), y=value, fill=group)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar
+  
+  geom_bar(aes(x=as.factor(id), y=value, fill=group), stat="identity", alpha=0.5) +
+  
+  # Add a val=100/75/50/25 lines. I do it at the beginning to make sur barplots are OVER it.
+  geom_segment(data=grid_data, aes(x = end, y = 80, xend = start, yend = 80), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
+  geom_segment(data=grid_data, aes(x = end, y = 60, xend = start, yend = 60), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
+  geom_segment(data=grid_data, aes(x = end, y = 40, xend = start, yend = 40), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
+  geom_segment(data=grid_data, aes(x = end, y = 20, xend = start, yend = 20), colour = "grey", alpha=1, size=0.3 , inherit.aes = FALSE ) +
+  
+  # Add text showing the value of each 100/75/50/25 lines
+  annotate("text", x = rep(max(data$id),4), y = c(20, 40, 60, 80), label = c("20", "40", "60", "80") , color="grey", size=3 , angle=0, fontface="bold", hjust=1) +
+  
+  geom_bar(aes(x=as.factor(id), y=value, fill=group), stat="identity", alpha=0.5) +
+  ylim(-100,120) +
+  theme_minimal() +
+  theme(
+    legend.position = "none",
+    axis.text = element_blank(),
+    axis.title = element_blank(),
+    panel.grid = element_blank(),
+    plot.margin = unit(rep(-1,4), "cm") 
+  ) +
+  coord_polar() + 
+  geom_text(data=label_data, aes(x=id, y=value+10, label=individual, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$angle, inherit.aes = FALSE ) +
+  
+  # Add base line information
+  geom_segment(data=base_data, aes(x = start, y = -5, xend = end, yend = -5), colour = "black", alpha=0.8, size=0.6 , inherit.aes = FALSE )  +
+  geom_text(data=base_data, aes(x = title, y = -18, label=group), hjust=c(1,1,0,0), colour = "black", alpha=0.8, size=4, fontface="bold", inherit.aes = FALSE)
+ 
+p
+

+
+
+ + + +  +
+

A work by Yan Holtz

+

yan.holtz.data@gmail.com

+ + + + + +

+ + + +

+ +  + + + + +
+ + + + + + + + + +