Nothing
## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set (
collapse = TRUE,
warning = TRUE,
message = TRUE,
width = 120,
comment = "#>",
fig.retina = 2
)
## ----create_package-----------------------------------------------------------
path <- file.path (tempdir (), "demo")
usethis::create_package (path, check_name = FALSE, open = FALSE)
## ----dir_tree-----------------------------------------------------------------
fs::dir_tree (path)
## ----first-fn-----------------------------------------------------------------
code <- c (
"#' my_function",
"#'",
"#' @param x An input",
"#' @return Something else",
"#' @examples",
"#' y <- my_function (x = 1)",
"#' @export",
"my_function <- function (x) {",
" return (x + 1)",
"}"
)
writeLines (code, file.path (path, "R", "myfn.R"))
roxygen2::roxygenise (path)
## ----dir_tree2----------------------------------------------------------------
fs::dir_tree (path)
## ----autotest1-fakey, eval = FALSE, echo = TRUE-------------------------------
# library (autotest)
# x0 <- autotest_package (path)
## ----autotest1, eval = TRUE, echo = FALSE-------------------------------------
devtools::load_all (".", export_all = FALSE)
x0 <- autotest_package (path)
## -----------------------------------------------------------------------------
DT::datatable (x0, options = list (dom = "t")) # display table only
## ----autotest-TRUE------------------------------------------------------------
x1 <- autotest_package (path, test = TRUE)
DT::datatable (x1, options = list (dom = "t"))
## ----assert-length------------------------------------------------------------
code <- c (
"#' my_function",
"#'",
"#' @param x An input",
"#' @return Something else",
"#' @examples",
"#' y <- my_function (x = 1.)",
"#' @export",
"my_function <- function (x) {",
" if (length(x) > 1) {",
" warning(\"only the first value of x will be used\")",
" x <- x [1]",
" }",
" return (x + 1)",
"}"
)
writeLines (code, file.path (path, "R", "myfn.R"))
roxygen2::roxygenise (path)
## ----autotest-TRUE2-----------------------------------------------------------
autotest_package (path, test = TRUE)
## ----int-input----------------------------------------------------------------
code [6] <- gsub ("1.", "1L", code [6], fixed = TRUE)
writeLines (code, file.path (path, "R", "myfn.R"))
roxygen2::roxygenise (path)
x2 <- autotest_package (path, test = TRUE)
DT::datatable (x2, options = list (dom = "t"))
## ----use-as-int---------------------------------------------------------------
code <- c (
code [1:12],
" if (is.numeric (x))",
" x <- as.integer (x)",
code [13:length (code)]
)
## ----unrestricted-------------------------------------------------------------
code [3] <- gsub ("An input", "An unrestricted input", code [3], fixed = TRUE)
writeLines (code, file.path (path, "R", "myfn.R"))
roxygen2::roxygenise (path)
autotest_package (path, test = TRUE)
## ----input-range--------------------------------------------------------------
code <- c (
"#' my_function",
"#'",
"#' @param x An input between 0 and 10",
"#' @return Something else",
"#' @examples",
"#' y <- my_function (x = 1L)",
"#' @export",
"my_function <- function (x) {",
" if (length(x) > 1) {",
" warning(\"only the first value of x will be used\")",
" x <- x [1]",
" }",
" if (is.numeric (x))",
" x <- as.integer (x)",
" if (x < 0 | x > 10) {",
" stop (\"x must be between 0 and 10\")",
" }",
" return (x + 1L)",
"}"
)
writeLines (code, file.path (path, "R", "myfn.R"))
roxygen2::roxygenise (path)
autotest_package (path, test = TRUE)
## ----vector-input-------------------------------------------------------------
code <- c (
"#' my_function",
"#'",
"#' @param x An input",
"#' @return Something else",
"#' @examples",
"#' y <- my_function (x = 1)",
"#' y <- my_function (x = 1:2)",
"#' @export",
"my_function <- function (x) {",
" if (is.numeric (x)) {",
" x <- as.integer (x)",
" }",
" return (x + 1L)",
"}"
)
writeLines (code, file.path (path, "R", "myfn.R"))
roxygen2::roxygenise (path)
## ----autotest-TRUE3-----------------------------------------------------------
x3 <- autotest_package (path, test = TRUE)
DT::datatable (x3, options = list (dom = "t"))
## ----list-col-demo------------------------------------------------------------
dat <- data.frame (x = 1:3, y = 4:6)
dat$x <- I (as.list (dat$x)) # base R
dat <- tidyr::nest (dat, y = y)
print (dat)
## ----mtcars-error, error = TRUE-----------------------------------------------
try({
m <- mtcars
head (m, n = 2L)
m$mpg <- I (as.list (m$mpg))
head (m, n = 2L) # looks exaxtly the same
cor (m)
})
## ----mtcars-okay--------------------------------------------------------------
m$mpg <- paste0 ("a", m$mpg)
class (m$mpg)
## ----list-col-input-----------------------------------------------------------
code <- c (
"#' my_function",
"#'",
"#' @param x An input",
"#' @return Something else",
"#' @examples",
"#' y <- my_function (x = 1)",
"#' y <- my_function (x = 1:2)",
"#' @export",
"my_function <- function (x) {",
" if (methods::is (unclass (x), \"list\")) {",
" x <- unlist (x)",
" }",
" if (is.numeric (x)) {",
" x <- as.integer (x)",
" }",
" return (x + 1L)",
"}"
)
writeLines (code, file.path (path, "R", "myfn.R"))
roxygen2::roxygenise (path)
## ----autotest-TRUE4-----------------------------------------------------------
autotest_package (path, test = TRUE)
## ----return-val---------------------------------------------------------------
code <- c (
"#' my_function3",
"#'",
"#' @param x An input",
"#' @examples",
"#' y <- my_function3 (x = 1:2)",
"#' @export",
"my_function3 <- function (x) {",
" return (datasets::iris)",
"}"
)
writeLines (code, file.path (path, "R", "myfn3.R"))
roxygen2::roxygenise (path) # need to update docs with seed param
x4 <- autotest_package (path, test = TRUE)
DT::datatable (x4, options = list (dom = "t"))
## ----return-val-2-------------------------------------------------------------
code <- c (
code [1:3],
"#' @return The iris data set as dataframe",
code [4:length (code)]
)
writeLines (code, file.path (path, "R", "myfn3.R"))
roxygen2::roxygenise (path) # need to update docs with seed param
x5 <- autotest_package (path, test = TRUE)
DT::datatable (x5, options = list (dom = "t"))
## ----iris-update--------------------------------------------------------------
code [4] <- "#' @return The iris data set as data.frame"
writeLines (code, file.path (path, "R", "myfn3.R"))
roxygen2::roxygenise (path) # need to update docs with seed param
autotest_package (path, test = TRUE)
## ----input-checks-------------------------------------------------------------
code <- c (
"#' my_function3",
"#'",
"#' @param x An input",
"#' @return The iris data set as data.frame",
"#' @examples",
"#' y <- my_function3 (x = datasets::iris)",
"#' @export",
"my_function3 <- function (x) {",
" return (x)",
"}"
)
writeLines (code, file.path (path, "R", "myfn3.R"))
roxygen2::roxygenise (path) # need to update docs with seed param
x6 <- autotest_package (path, test = TRUE)
DT::datatable (x6, options = list (dom = "t"))
## ----input-fix, eval = FALSE--------------------------------------------------
# code [3] <- "#' @param x An input which can be a data.frame"
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.