dev_history.Rmd for working package

library(testthat)
# --> for parse tests
fusen::fill_description(
  pkg = here::here(),
  fields = list(
    Title = "Build A Package From Rmarkdown file",
    Description = "Use Rmarkdown First method to build your package. Start your package with documentation. Everything can be set from a Rmarkdown file in your project.",
    `Authors@R` = c(
      person("John", "Doe", email = "john@email.me", role = c("aut", "cre"), comment = c(ORCID = "0000-0000-0000-0000"))
    )
  )
)
# Define License with use_*_license()
usethis::use_mit_license("John Doe")

Read data

# Set , error=TRUE for checks() if needed
# Run all in the console directly
# Create "inst/" directory
dir.create(here::here("inst"))
# Example dataset
file.copy(system.file("nyc_squirrels_sample.csv", package = "fusen"), here::here("inst"))
# Make your dataset file available to the current Rmd
pkgload::load_all(path = here::here(), export_all = FALSE)
# You will be able to read your example data file in each of your function examples and tests as follows
# For check() only, we need to shim system.file, otherwise base::system.file does not recognised load_all()
pkgload:::shim_system.file(package = "<my_package_name>")
system.file(package = "<my_package_name>")
list.files(system.file(package = "<my_package_name>"))
datafile <- system.file("nyc_squirrels_sample.csv", package = "<my_package_name>")
datafile <- pkgload:::shim_system.file("nyc_squirrels_sample.csv", package = "<my_package_name>")
here::here()
datafile
nyc_squirrels <- read.csv(datafile, encoding = "UTF-8")

Calculate the median of a vector

#' My median
#'
#' @param x Vector of Numeric values
#' @inheritParams stats::median
#'
#' @return
#' Median of vector x
#' @export
#'
#' @examples
#' my_median(2:20)
my_median <- function(x, na.rm = TRUE) {
  if (!is.numeric(x)) {stop("x should be numeric")}
  stats::median(x, na.rm = na.rm)
}
my_median(1:12)
test_that("my_median works properly and show error if needed", {
  expect_error(my_median("text"))
})

Calculate the mean of a vector

Use sub-functions in the same chunk

#' My Other median
#'
#' @param x Vector of Numeric values
#' @inheritParams stats::median
#'
#' @return
#' Median of vector x
#' @export
#'
#' @examples
my_other_median <- function(x, na.rm = TRUE) {
  if (!is.numeric(x)) {stop("x should be numeric")}
  sub_median(x, na.rm =na.rm)
}

#' Core of the median not exported
#' @param x Vector of Numeric values
#' @inheritParams stats::median
sub_median <- function(x, na.rm = TRUE) {
  stats::median(x, na.rm)
}
my_other_median(1:12)
my_other_median(8:20)
my_other_median(20:50)
test_that("my_median works properly and show error if needed", {
  expect_true(my_other_median(1:12) == 6.5)
  expect_error(my_other_median("text"))
})

The third median without example

#' My third median
#'
#' @param x Vector of Numeric values
#' @inheritParams stats::median
#'
#' @return
#' Median of vector x
#' @export
my_third_median <- function(x, na.rm = TRUE) {
  if (!is.numeric(x)) {stop("x should be numeric")}
  stats::median(x, na.rm = na.rm)
}

The fourth median without example in but one out

#' My fourth median
#'
#' @param x Vector of Numeric values
#' @inheritParams stats::median
#'
#' @return
#' Median of vector x
#' @export
my_fourth_median <- function(x, na.rm = TRUE) {
  if (!is.numeric(x)) {stop("x should be numeric")}
  stats::median(x, na.rm = na.rm)
}
my_fourth_median(1:12)
my_fourth_median(8:20)

The fifth median without empty example and test

#' My fifth median
#'
#' @param x Vector of Numeric values
#' @inheritParams stats::median
#'
#' @return
#' Median of vector x
#' @export
my_fifth_median <- function(x, na.rm = TRUE) {
  if (!is.numeric(x)) {stop("x should be numeric")}
  stats::median(x, na.rm = na.rm)
}


The sixth median with dot in function name

#' My sixth median
#'
#' @param x Vector of Numeric values
#' @inheritParams stats::median
#'
#' @return
#' Median of vector x
#' @export
my.sixth.median_function <- function(x, na.rm = TRUE) {
  if (!is.numeric(x)) {stop("x should be numeric")}
  stats::median(x, na.rm = na.rm)
}
my.sixth.median_function(1:12)
my.sixth.median_function(8:20)
my.sixth.median_function(20:50)
test_that("my.sixth.median_function works properly and show error if needed", {
  expect_true(my.sixth.median_function(1:12) == 6.5)
  expect_error(my.sixth.median_function("text"))
})

Empty ones




Uppercase names

#' MyUppercaseFunction
MyUppercaseFunction <- function() {
  message("hello")
}
test_that("MyUppercaseFunction works properly and show error if needed", {
  expect_message(MyUppercaseFunction() == "hello")
})

No roxygen and chunk named fun only

my_norox <- function(x) {
  x + 10
}

No roxygen with example, chunk named fun- and chunk named ex-

my_norox2 <- function(x) {
  x + 10
}
#' \dontrun{
#' # comment
#' my_norox2(10)
#' }
#'
#' \dontrun{
# comment
my_norox2(12)
#' }

space with function, chunk named fun_, chunk named ex_ and extra or no space before @

#' My space function
#'
#'   @param x Vector of Numeric values
#'
#'@return Median of vector x
#' @examples
#' @export

my_space <- function(x) {
  x + 10
}
my_space(10)

space with no export, chunk named fun_

#' My space 2 function
#'
#' @param x Vector of Numeric values
#'
#' @return Median of vector x
#' @examples

my_space2 <- function(x) {
  x + 10
}

There can be development actions

These are only included in the present flat template file, their content will not be part of the package anywhere else.

# Short chunk dev name not in vignette
# Run but keep eval=FALSE to avoid infinite loop
# Execute in the console directly
fusen::inflate(flat_file = "dev/dev_history.Rmd")

Inflate your package

# duplicate empty name
# duplicate empty name

You're one inflate from paper to box. Build your package from this very Rmd using fusen::inflate()



Try the fusen package in your browser

Any scripts or data that you put into this service are public.

fusen documentation built on Aug. 17, 2023, 5:09 p.m.