Last updated: 2019-04-09
Checks: 6 0
Knit directory: rrresearch/
This reproducible R Markdown analysis was created with workflowr (version 1.2.0). The Report tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20190216)
was run prior to running the code in the R Markdown file. Setting a seed ensures that any results that rely on randomness, e.g. subsampling or permutations, are reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility. The version displayed above was the version of the Git repository at the time these results were generated.
Note that you need to be careful to ensure that all relevant files for the analysis have been committed to Git prior to generating the results (you can use wflow_publish
or wflow_git_commit
). workflowr only checks the R Markdown file, but you know if there are other scripts or data files that it depends on. Below is the status of the Git repository when the results were generated:
Ignored files:
Ignored: .DS_Store
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: analysis/.DS_Store
Ignored: analysis/assets/
Ignored: assets/
Ignored: data/metadata/
Ignored: data/raw/
Ignored: demos/demo-rmd-0_files/
Ignored: demos/demo-rmd-1_files/
Ignored: demos/demo-rmd_files/
Ignored: docs/.DS_Store
Ignored: docs/assets/.DS_Store
Ignored: docs/assets/img/.DS_Store
Ignored: docs/demo-rmd-0_files/
Ignored: docs/demo-rmd-1_files/
Ignored: docs/demo-rmd-2_files/
Ignored: docs/demo-rmd-3_files/
Ignored: docs/demo-rmd_files/
Ignored: docs/index-demo-pre_files/
Ignored: figure/
Ignored: install.R
Ignored: rmd/
Ignored: slides/libs/
Unstaged changes:
Modified: analysis/_site.yml
Modified: analysis/about.Rmd
Modified: render-other.R
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the R Markdown and HTML files. If you’ve configured a remote Git repository (see ?wflow_git_remote
), click on the hyperlinks in the table below to view them.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | c534ba5 | Anna Krystalli | 2019-04-09 | add packagking materials |
html | 74ffa63 | Anna Krystalli | 2019-04-09 | commit site |
# Hello, world!
#
# This is an example function named 'hello'
# which prints 'Hello, world!'.
#
# You can learn more about package authoring with RStudio at:
#
# http://r-pkgs.had.co.nz/
#
# Some useful keyboard shortcuts for package authoring:
#
# Build and Reload Package: 'Cmd + Shift + B'
# Check Package: 'Cmd + Shift + E'
# Test Package: 'Cmd + Shift + T'
hello <- function() {
print("Hello, world!")
}
[1] "Hello, world!"
Used to control namespace and document functions
First delete automatically generated NAMESPACE
& man
#' Hello World!
#'
#' Print hello greeting
#' @return prints hello greeting to console
#' @export
#'
#' @examples
#' hello()
hello <- function() {
print("Hello, world!")
}
#' Hello World!
#'
#' Print hello greeting
#' @return prints hello greeting to console from me
#' @export
#'
#' @examples
#' hello()
hello <- function() {
print("Hello, world from Anna")
}
hello()
[1] "Hello, world from Anna"
#' Hello World!
#'
#' Print personalised hello greeting from me.
#'
#' @param name character string. Your name!
#' @param ... additional arguments passed to `cowsay::say()`
#'
#' @return prints hello greeting to console
#' @export
#'
#' @examples
#' hello()
#' hello("Lucy Elen")
hello <- function(name = NULL, ...) {
if(is.null(name)){name <- "world"}
greeting <- paste("Hello", name, "from Anna!")
animal_names <- names(cowsay::animals)
i <- sample(1:length(animal_names), 1)
cowsay::say(greeting, animal_names[i], ...)
}
To create a new test file (and the testing framework if required), use function usethis::use_test()
. It’s good practice to name the test files after the .R
files containing the functions being tested.
✔ Setting active project to '/Users/Anna/Documents/workflows/workshops/materials/mypackage'
✔ Adding 'testthat' to Suggests field in DESCRIPTION
✔ Creating 'tests/testthat/'
✔ Writing 'tests/testthat.R'
✔ Writing 'tests/testthat/test-hello.R'
● Modify 'tests/testthat/test-hello.R'
This just created the following folders and files
tests
├── testthat
│ └── test-hello.R
└── testthat.R
1 directory, 2 files
When the tests are run (either through running devtools::test()
, clicking on More > Test Package in the Build panel or Cmd/Ctrl + Shift + T
), the code in each test script in directory testthat
is run.
It also added testthat
to the suggested packages in the DESCRIPTION
file.
Suggests:
testthat
That’s because you don’t need test that to run the functions in mypackage
, but you do if you want to run the tests.
test-hello.R
Let’s load the library so we can explore the testthat
testing framework
If the test doesn’t pass it throws an error
Error: Test failed: 'multiplication works'
* 2 * 2 not equal to 5.
1/1 mismatches
[1] 4 - 5 == -1
We’ll write tests to ensure that when our function is given a certain set of arguments as input, it generates output that we know to be correct
Let’s create something to test against.
The first thing to note, looking at the say()
documentation is that it takes an argument type
which allows us to specify the output we want. It defaults message
which means the output of the function is returned as a message.
We can therefore use testthat::expect_message()
Colors cannot be applied in this environment :( Try using a terminal or RStudio.
"\n\n ----- \nHello world from Anna! \n ------ \n \\ \n \\\n __\n / \\\n | |\n @ @\n || ||\n || ||\n |\\_/|\n \\___/ GB\n"
R version 3.5.2 (2018-12-20)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Mojave 10.14.3
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] testthat_2.0.1
loaded via a namespace (and not attached):
[1] rmsfact_0.0.3 workflowr_1.2.0 Rcpp_1.0.1
[4] crayon_1.3.4 digest_0.6.18 rprojroot_1.3-2
[7] R6_2.4.0 backports_1.1.3 git2r_0.24.0.9001
[10] magrittr_1.5 evaluate_0.13 rlang_0.3.1
[13] stringi_1.3.1 fs_1.2.7 whisker_0.3-2
[16] cowsay_0.7.0 fortunes_1.5-4 rmarkdown_1.12
[19] tools_3.5.2 stringr_1.4.0 glue_1.3.1
[22] xfun_0.5 yaml_2.2.0 compiler_3.5.2
[25] htmltools_0.3.6 knitr_1.22