knitr::opts_chunk$set(echo = TRUE, eval = F)
library(devtools)
library(roxygen2)

Package creation

     # Use the create function to set up your first package
    create_package('elrtest')

    # Take a look at the files and folders in your package
    dir()

Fonction

To save in /R folder as function_name.R

#' Ramene une date/heure a 5am
#'
#' Ramene une date heure a 5am UTC avec un decalage d'heures optionel; par defaut en UTC. fonctionne au changement d'heure ete/hiver
#'
#' @param date date/heure a decaler - converti en POSIXct
#' @param shift_hours  nombre d'heures a decaler - positif ou negatif
#' @param tz fuseau horaire du resultat - UTC par defaut
#' @importFrom lubridate period force_tz floor_date
#' @export
#' @examples
#' f_goto_5am('2019-04-20 06:00')
#' f_goto_5am('2019-03-31 01:00')
#'
#' @return POSIXct
#'
## Add in the author of the `data_summary()` function
#' @author OG, \email{olivier.granacher@trimet.fr}

f_goto_5am <- function(date = floor_date(Sys.time(), 'year'), shift_hours = 0 , tz =' UTC' ) {
  p1 <- lubridate::period(5, units = 'hours')
  p2 <- lubridate::period( floor(shift_hours), units = 'hours')
  force_tz(as.POSIXct(floor_date(as.POSIXct(date), 'day') + p1 + p2), tz)
}

Creation du fichier R pour la fonction

        #dump("f_goto_5am", file = "utils/R/f_goto_5am.R", )
        dir('R/')

Package documentation creation : packagename.R dans /R

#' elrtest
#'
#' Fontions utilisees frequemment
#'
#' @docType package
#' 
#' @name elrtest
#'
"_PACKAGE"
    # Creation de la documentation dans R/man
    devtools::document('elrtest')
    help(f_goto_5am)

Adds dataset to package http://www.davekleinschmidt.com/r-packages/

saved in data-raw/

  usethis::use_data_raw()
  GN <- stringr::str_pad(as.character(seq(from = 61, to = 179, by = 2)),
                       width = 3,
                       side = 'left',
                       pad = '0')
GN <- paste0('G', GN)

GS <- stringr::str_pad(as.character(seq(from = 62, to = 180, by = 2)),
                       width = 3,
                       side = 'left',
                       pad = '0')
GS <- paste0('G', GS)

FN <- stringr::str_pad(as.character(seq(from = 1, to = 59, by = 2)),
                       width = 3,
                       side = 'left',
                       pad = '0')
FN <- paste0('F', FN)

FS <- stringr::str_pad(as.character(seq(from = 2, to = 60, by = 2)),
                       width = 3,
                       side = 'left',
                       pad = '0')
FS <- paste0('F', FS)

df_pots <- tibble::tibble(pots = as.character(c(FN, GN, FS, GS)))
df_pots$pots_fac <- factor(df_pots$pots, levels = c(FN, GN, FS, GS), ordered = T )
df_pots$hall <- c(rep('N', 90), rep('S', 90))
usethis::use_data(df_pots, overwrite = T)

Documentation data df_pots - A sauvegarder dans R/df_pots.R

#' df liste des cuves F et G
#'
#' tibble contenant les cuves dans l'ordre Nord et Sud
#'
#' @format Tibble de 2 colonnes et 180 lignes
#' \describe{
#'  \item{pots}{char F001, F003 ... G180}
#'  \item{pots_fac}{ordered factor de pots}
#'  \item{hall}{Hall N ou S}
#' }
#' @source genere manuellement
#'
"df_pots"

Creation de la documentation dans man/

  document()
  help(df_pots)

Check package

  run_examples()
  check()

Use package. Goes to description file

    use_package('lubridate' )

Build package

    build()

use of GIT

    use_git_config(user.email = "olivier.granacher@gmail.com")
    use_git()

Setup test structure

    require(testthat)
    devtools::use_testthat()

Test - Save in tests/testthat/test-f_goto_5am.R

    context("test f_goto_5am")
    test_that("Test f_goto_5am",
              {
    expect_equal(as.character(f_goto_5am('2019-01-01 01:00:00', 8)), '2019-01-01 13:00:00')
    expect_error(f_goto_5am(1))
              }
    )

runs tests

    test()


OlivierGranacher/elrtest documentation built on May 22, 2019, 5:01 p.m.