Skip to content
all functions and scripts of the lazy trade educational project on udemy
Branch: master
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
R minor investigations Jul 12, 2019
data more corrections and examples Jun 25, 2019
inst/extdata allowing examples without polluting files Jul 10, 2019
man minor investigations Jul 12, 2019
tests checks and tests passing Jun 22, 2019
.Rbuildignore added test coverage Jul 10, 2019
.gitignore Update .gitignore Jul 10, 2019
.travis.yml add covr Jul 12, 2019
DESCRIPTION added test coverage Jul 10, 2019
LICENSE fixed note about license Jun 22, 2019 fixed note about license Jun 22, 2019
NAMESPACE fixing RL for market type Jun 23, 2019 added info Jul 10, 2019
README.Rmd minor investigations Jul 12, 2019
codecov.yml added test coverage Jul 10, 2019 removed redundant comments Jul 13, 2019


Travis build status codecov

The goal of lazytrade is to keep all functions and scripts of the lazy trade educational project. Provided functions are providing an opportunity to learn Computer and Data Science using example of Algorithmic Trading


You can install the released version of lazytrade from CRAN with:


And the development version from GitHub with:

# install.packages("devtools")


This is a basic example which shows you how to solve a common problem:

## basic example code

What is special about using README.Rmd instead of just You can include R chunks like so:

#>      speed           dist       
#>  Min.   : 4.0   Min.   :  2.00  
#>  1st Qu.:12.0   1st Qu.: 26.00  
#>  Median :15.0   Median : 36.00  
#>  Mean   :15.4   Mean   : 42.98  
#>  3rd Qu.:19.0   3rd Qu.: 56.00  
#>  Max.   :25.0   Max.   :120.00

You’ll still need to render README.Rmd regularly, to keep up-to-date.

Notes to remind myself how to create R package

taken from and

Generating Documentation

Create right title case for the title of the package

tools::toTitleCase(“Lazy Trading as sets of R functions to facilitate algorithmic trading”)

Run this code to re-generate documentation devtools::document()

Run this code to fix license: usethis::use_mit_license(name = "Vladimir Zhbanko")

Adding data to the package for internal tests

Run this code to add data to the folder data/ x <- sample(1000) usethis::use_data(x)

Note: use option ’LazyLoad` to make data available only when user wants it always include LazyData: true in your DESCRIPTION. Note: to document dataset see

Adding examples to test package function

Tests setup first time

Run this command to setup tests ‘usethis::use_testthat()’

This will create a folder with the name tests

Inside this folder there will be another folder testthat.

Examples in Roxygen code

@examples …

code to execute during package checks



code to NOT execute during package checks


Testing a package

Create a test script

Run this command to create a new script with the test skeleton:


Enrich the test script


  1. add libraries used for test
  2. add function context("profit_factor")
  3. add function test_that(“test description”, {test process})
  4. load data using function data(named_data_object)
#> ── Attaching packages ────────────────────────────────────────────────── tidyverse 1.2.1 ──
#> ✔ ggplot2 3.2.0     ✔ purrr   0.3.2
#> ✔ tibble  2.1.3     ✔ dplyr   0.8.3
#> ✔ tidyr   0.8.3     ✔ stringr 1.4.0
#> ✔ readr   1.3.1     ✔ forcats 0.4.0
#> ── Conflicts ───────────────────────────────────────────────────── tidyverse_conflicts() ──
#> ✖ dplyr::filter()  masks stats::filter()
#> ✖ purrr::is_null() masks testthat::is_null()
#> ✖ dplyr::lag()     masks stats::lag()
#> ✖ dplyr::matches() masks testthat::matches()

test_that("test value of the calculation", {


  DF_Stats <- profit_factor_data %>%
    group_by(X1) %>%
    summarise(PnL = sum(X5),
              NumTrades = n(),
              PrFact = profit_factor(X5)) %>%
    select(PrFact) %>%
    head(1) %>%
    as.vector() %>%

  expect_equal(DF_Stats$PrFact, 0.68)


Test of the coverage for the script

Test coverage shows you what you’ve tested devtools::test_coverage_file()


Automated checks

This will add automatic test coverage badge to the readme file on github usethis::use_coverage()

Checking package

Step 1. devtools::document() Step 2. devtools::run_examples() Step 3. Menu ‘Build’ Clean and Rebuild Step 4. ‘Check’


Handling functions that write files

In case functions are writing files there are few considerations to take into account:

  • examples section must contain working example of code that writes files
  • example code must write to the temporary directory defined by tempdir() function
  • after package check performed with devtools::check() there should nothing remain in the ‘tmp/’ directory


File names defined by function tempdir() would look like this:

# > tempdir()
# [1] "/tmp/RtmpkaFStZ"

File names defined by function tempfile() would look like this:

# > tempfile()
# [1] "/tmp/RtmpkaFStZ/file7a33be992b4"

This is example of how function write_csv example works:

tmp <- tempfile()
write_csv(mtcars, tmp)

results of this code are correctly stored to the temporary file

however this example from readr package function write_csv is showing that file will be written to the ‘/tmp/’ directory

dir <- tempdir()
write_tsv(mtcars, file.path(dir, "mtcars.tsv.gz"))

Deleting files after running examples:

We use function unlink() to do this:

unlink("/tmp/*.csv", recursive = TRUE, force = TRUE)

and we check that there is nothing more remained:


CRAN Note Avoidance

see see

Versioning of the package


Test Environments

Clone package from GitHub and test check it in Docker Container

  • started docker container vladdsm/docker-r-studio
  • new project package
  • clone from vzhomeexperiments/lazytrade.git
  • use check button to pass the test

Build package


Adding Readme Rmd


Automatic check with Travis


Upload package to CRAN

before release checks

spelling devtools::spell_check()

checking on R hub devtools::check_rhub()

checking win devel devtools::check_win_devel()

Update file

releasing the package (questions) devtools::release() usethis::use_release_issue()

uploading the package archive to CRAN

You can’t perform that action at this time.