Projects/RNWtemplate.R

#' @title Create a new Analysis RNW template
#'
#'
#' @description Creates a new Rnw template for a new analysis
#'
#'
#'
#' @param dir Path to the folder where the template is to be written.
#'
#' @usage RNWtemplate(dir=NULL, name=NULL)
#'
#' @examples
#' \donttest{
#' RNWtemplate(dir="C:/Projects", name="NewAnalysis")
#'
#' }
#'
#' @export
#' @name RNWtemplate
#' @author Dominic LaRoche
#'
RNWtemplate<-function(dir=NULL, name=NULL){
  if(is.null(dir)|is.null(name)){
    stop("You must specify both a directory and a file name")
  }
  #Check to ensure file paths have a trailing forward slash (but only 1)
  if(!grepl(".+/$", dir, perl=TRUE)){
    dir<-paste0(dir,"/")
  }
  if(!grepl(".+\\.Rnw$", name,ignore.case=TRUE, perl=TRUE)){
    name<-paste0(name,".Rnw")
  }
  filePath<-paste0(dir,name)
  writeLines(
    #template knitr code here
    "\\documentclass[12pt]{article}
    \\usepackage{graphicx}
    \\usepackage[colorlinks=true,linkcolor=blue]{hyperref}
    \\usepackage{subfig}
    \\usepackage[usenames,dvipsnames]{xcolor}
    \\usepackage{placeins}
    \\usepackage[headheight=37pt,margin=2.5cm]{geometry}
    \\usepackage{fancyhdr}
    \\fancypagestyle{htgheader}{%
         \\lhead{\\bfseries For Internal Use Only}
         \\rhead{\\includegraphics[scale=.25]{../Figures/HTGLogo.jpg}}
         \\renewcommand{\\headrulewidth}{2pt}% 2pt header rule
         \\renewcommand{\\headrule}{\\hbox to\\headwidth{%
         \\color{BurntOrange}\\leaders\\hrule height \\headrulewidth\\hfill}}
         \\setlength{\\headheight}{37pt}
    }\
%%%%Change title of document on next line%%%%
    \\title{PLACE TITLE HERE\\vspace{-4ex}}
    \\author{\\vspace{-4ex}}
    \\begin{document}
    \\maketitle
    \\thispagestyle{htgheader}
    \\pagestyle{htgheader}
%%%%Begin document below%%%
<<ReadData_LoadFile, echo=FALSE, message=FALSE>>=
#Read in data and load libraries in first chunk
@\n
\\section{Introduction}\n\n
\\section{Methods}\n\n
\\section{Results}\n\n
\\section{Discussion}\n\n
\\end{document}",
    con=filePath)
}
rocrat/HTGPackage documentation built on May 25, 2017, 8:32 a.m.