Skip to content
Snippets Groups Projects
Commit 765fd8da authored by ronan.vignard's avatar ronan.vignard
Browse files

Initial commit

parents
Branches
Tags
No related merge requests found
^dev$
.Rproj.user
.Rhistory
.RData
.DS_Store
.httr-oauth
Version: 1.0
RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Sweave
LaTeX: pdfLaTeX
AutoAppendNewline: Yes
StripTrailingWhitespace: Yes
LineEndingConversion: Posix
*.html
*.R
---
title: "Development actions history"
output: html_document
editor_options:
chunk_output_type: console
---
All commands that you use to use when developing packages...
# First time just after creating the project
- Fill the following chunk to create the DESCRIPTION of your package
```{r description}
# Describe your package
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")
```
# All-in-one function to deploy publicly on GitHub
Either use this GitHub all-in-one function or choose the steps one-by-one in the following sections.
See `vignette("share-on-a-github-website", package = "fusen")` for more information.
```{r, eval=FALSE}
# _All-in-one share of your package and its documentation on GitHub
fusen::init_share_on_github()
```
# Start using git
```{r, eval=FALSE}
usethis::use_git()
# Deal with classical files to ignore
usethis::git_vaccinate()
# Use main for primary branch
usethis::git_default_branch_rename()
```
# Set extra sources of documentation
```{r, eval=FALSE}
# Install a first time
remotes::install_local()
# README
usethis::use_readme_rmd()
# Code of Conduct
usethis::use_code_of_conduct("contact@fake.com")
# NEWS
usethis::use_news_md()
```
**From now, you will need to "inflate" your package at least once to be able to use the following commands. Let's go to your flat template, and come back here later if/when needed.**
# Package development tools
## Use once
```{r, eval=FALSE}
# Pipe
usethis::use_pipe()
# package-level documentation
usethis::use_package_doc()
# GitHub
# Add your credentials for GitHub
usethis::create_github_token()
gitcreds::gitcreds_set()
# _Or one step at a time
# Send your project to a new GitHub project (included in `init_share_on_github()`)
usethis::use_github()
# Set Continuous Integration
# _GitHub (included in `init_share_on_github()`)
usethis::use_github_action_check_standard()
usethis::use_github_action("pkgdown")
usethis::use_github_action("test-coverage")
# _GitLab
gitlabr::use_gitlab_ci(type = "check-coverage-pkgdown")
```
## Use everytime needed
```{r, eval=FALSE}
# Simulate package installation
pkgload::load_all()
# Generate documentation and deal with dependencies
attachment::att_amend_desc()
# Check the package
devtools::check()
# Add a new flat template
fusen::add_flat_template("add")
```
# Share the package
```{r, eval=FALSE}
# set and try pkgdown documentation website locally
usethis::use_pkgdown()
pkgdown::build_site()
# build the tar.gz with vignettes to share with others
devtools::build(vignettes = TRUE)
# Share your package and its documentation on GitHub
# usethis::create_github_token()
# gitcreds::gitcreds_set()
fusen::init_share_on_github()
```
---
title: "flat_first.Rmd for working package"
output: html_document
editor_options:
chunk_output_type: console
---
<!-- Run this 'development' chunk -->
<!-- Store every call to library() that you need to explore your functions -->
```{r development, include=FALSE}
library(testthat)
```
<!--
You need to run the 'description' chunk in the '0-dev_history.Rmd' file before continuing your code there.
If it is the first time you use {fusen}, after 'description', you can directly run the last chunk of the present file with inflate() inside.
-->
```{r development-load}
# Load already included functions if relevant
pkgload::load_all(export_all = FALSE)
```
# Include some data examples in your package
<!--
Store your dataset in a directory named "inst/" at the root of your project.
Use it for your tests in this Rmd thanks to `pkgload::load_all()` to make it available
and `system.file()` to read it in your examples.
- There already is a dataset in the "inst/" directory to be used in the examples below
-->
```{r development-dataset}
# Run all this chunk in the console directly
# There already is a dataset in the "inst/" directory
# Make the dataset file available to the current Rmd during development
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 - see chunks below
datafile <- system.file("nyc_squirrels_sample.csv", package = "data.nitrates")
nyc_squirrels <- read.csv(datafile, encoding = "UTF-8")
nyc_squirrels
```
# The first function of the package: Calculate the median of a vector
<!--
Create a chunk for the core of the function
- The chunk needs to be named `function` at least
- It contains the code of a documented function
- The chunk can also be named `function-my_median` to make it easily
findable in your Rmd
- Let the `@examples` part empty, and use the next `examples` chunk instead to present reproducible examples
After inflating the template
- This function code will automatically be added in a new file in the "R/" directory
-->
```{r function}
#' My median
#'
#' @param x Vector of Numeric values
#' @inheritParams stats::median
#'
#' @return
#' Median of vector x
#' @export
#'
#' @examples
my_median <- function(x, na.rm = TRUE) {
if (!is.numeric(x)) {stop("x should be numeric")}
stats::median(x, na.rm = na.rm)
}
```
<!--
Create a chunk with an example of use for your function
- The chunk needs to be named `examples` at least
- It contains working examples of your function
- The chunk is better be named `examples-my_median` to be handled
correctly when inflated as a vignette
After inflating the template
- This example will automatically be added in the '@examples' part of our function above in the "R/" directory
- This example will automatically be added in the vignette created from this Rmd template
-->
```{r examples}
my_median(1:12)
# Example with your dataset in "inst/"
datafile <- system.file("nyc_squirrels_sample.csv", package = "data.nitrates")
nyc_squirrels <- read.csv(datafile, encoding = "UTF-8")
# Apply my function
my_median(nyc_squirrels[,"hectare_squirrel_number"])
```
<!--
Create a chunk with a test of use for your function
- The chunk needs to be named `tests` at least
- It contains working tests of your function
- The chunk is better be named `tests-my_median` to be handled
correctly when inflated as a vignette
After inflating the template
- This test code will automatically be added in the "tests/testthat/" directory
-->
```{r tests}
test_that("my_median works properly and show error if needed", {
expect_true(my_median(1:12) == 6.5)
expect_error(my_median("text"))
})
# Test with your dataset in "inst/"
datafile <- system.file("nyc_squirrels_sample.csv", package = "data.nitrates")
nyc_squirrels <- read.csv(datafile, encoding = "UTF-8")
# Apply test on my function
test_that("my_median works properly with internal dataset", {
expect_equal(my_median(nyc_squirrels[,"hectare_squirrel_number"]), 3)
})
```
# Calculate the mean of a vector
<!--
There can be other functions, examples and tests in your flat template.
Each of them will be inflated in a different file, provided that there is a level-1 or level-2 section title to separate from previous functions.
-->
## Use sub-functions in the same chunk
```{r function-my_other_median}
#' 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)
}
```
```{r examples-my_other_median}
my_other_median(1:12)
```
```{r tests-my_other_median}
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"))
})
```
<!--
# There can be development actions
Create a chunk with 'development' actions
- The chunk needs to be named `development` or `dev`
- It contains functions that are used for package development only
- Note that you may want to store most of these functions in the 0-dev_history.Rmd file
These are only included in the present flat template file, their content will not be part of the package anywhere else.
-->
```{r development-inflate, eval=FALSE}
# Keep eval=FALSE to avoid infinite loop in case you hit the knit button
# Execute in the console directly
fusen::inflate(flat_file = "dev/flat_first.Rmd", vignette_name = "Get started")
```
# Inflate your package
You're one inflate from paper to box.
Build your package from this very Rmd using `fusen::inflate()`
- Verify your `"DESCRIPTION"` file has been updated
- Verify your function is in `"R/"` directory
- Verify your test is in `"tests/testthat/"` directory
- Verify this Rmd appears in `"vignettes/"` directory
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment