Skip to content

Latest commit

 

History

History
110 lines (102 loc) · 4.4 KB

R_project_guide.md

File metadata and controls

110 lines (102 loc) · 4.4 KB

Overview

This guide covers:

R : Structure a Package

  • project name/

    • DESCRIPTION:
     Package: package name 
     Title: one line description of the package
     Version: xx
     Authors@R: c(person("Surname", "Names", email = "email", role = c("aut", "cre")),
                  person("Surname", "Names", email = "email", role = "ctb"))
     Description: more detailed than the title, each line <80 characters, indent with 4 spaces for subsequent lines 
     Depends: R (>= 3.2.3)
     License: xxx 
     LazyData: True
     Imports: 
        xxx (>= 0.2), 
        xxx 
     Suggests:
        xxx (>= 0.3), 
        knitr  
     VignetteBuilder: knitr 
     RoxygenNote: roxygen2 version 
    
    • NAMESPACE : automatically generated/updated by devtools::document()

    • README.md : basic information on package, installation, main usage (with simple examples), accessing documention.

    • R/ : contains all the functions (source code)

    • tests/

      • testthat/ : contains all testing files (named with prefix test)
      • testthat.R : used to run all tests. called by devtools::test()
      library("testthat")
      library("package.name")
      test_check("package.name")
      • harness.R : script to run tests during development stage
      library("xxx")  # load in all the needed libraries 
      devtools::test("...") # run all the tests 
      devtools::test("...", filter="...") # do filtered tests, if desired 
    • man/ : contains helper files for each function. automatically generated by devtools::document()

    • vignette/ : contains the vignette files. initialise this folder using devtools::use_vignette("packagename").

      • vignette.md : contains walkthrough examples to show how to use this packages.
    • src/ : contains C++ files.

      • devtools::use_package("Rcpp")
      • devtools::use_package("RcppArmadillo")
      • devtools::use_package("BH")
    • data/ : contains package data files. can add data to package using devtools::use_data(data1,data2). Not recommend to include large dataset into a package.

    • inst/ : contains python files.

      • Run python code via rPython R package
      • Run python code via command line [prefered]
      arg1 <- "xxx" 
      arg2 <- "xxx"
      path <- paste(system.file(package="PackageName"), "xxx.py", sep="/")
      command <- paste("python", path, arg1, arg2, sep = " ") 
      response <- system(command, intern=T)) 
      

R : Coding Style Guide

  1. File Names :

    • end in .R
    • prefix with numbers (e.g. 0-open.R, 1-close.R), if need to run in sequence
  2. Identifiers:

    • variable names should be nouns
    • funciton names should be verbs
    • select a style and be consistent with it
      • style option 1: variable and function names should be lowercase; use _ or . to separate word.
      • style option 2 (google): don't use _ in identifier names. Prefer variable.name, FunctionName.
  3. Syntax

    • Assignment : use <- , not =
    • Spacing:
      • place spaces around all infix operators (e.g. =, +, -, <-, etc.)
      • place space after a comma not before
    • Curly Braces:
    if (y == 0) {
      message("Y is zero")
    } else {
      message("Y is non-zero")
    }
    • Line length : max 80 characters per line
    • Indentation: two spaces, no tabs
  4. Organisation

    • Genernal layout of a script
      • Copyright statement comment
      • Author comment
      • File description comment
      • source() and library () statements [optional]
      • Functions with Roxygen comments on top (e.g. description, @param, @return, @import [optional], @export [optional])
    • Use test should go in a separate file named: originalfilename_test.R
    • Commenting guidelines
      • Entire commented lines should begin with # and one space
      • use # ===== or # ------ to separate code into meaningful truncks

References