Why create your own R-packages?

Packages you will need to create packages easily

Install if you do not have them already.

library(devtools)
## Loading required package: usethis
library(roxygen2)

Note: there are several pipelines to create packages, some using directly RStudio tools. I chose to demonstrate using a pipeline that relies mostly on code and little on clicking buttons, because the former is more reproducible.

Getting started

Start a R session from a parent directory in which you want to create your package. The package will be initialized inside a new directory. This new directory will also be a new RStudio project.

We will create a package that contains a few functions to handle temperature data. Our package will simply be called “temperature”.

Let’s initialize the package:

create("temperature")

Check the content of the new directory “temperature”:

Let’s add some R-functions to the R-folder. Create a R-file called “functions.R” and containing the following two functions that form the base of our package:

fahr_to_kelvin <- function(temp) {
  stopifnot(is.numeric(temp))
  kelvin <- ((temp - 32) * (5 / 9)) + 273.15
  return(kelvin)
}

kelvin_to_celsius <- function(temp) {
  celsius <- temp - 273.15
  return(celsius)
}

celsius_to_kelvin <- function(temp) {
  kelvin <- temp + 273.15
  return(kelvin)
}

You can create this file by hand or use some code:

sink(file = "R/Functions.R")
cat("fahr_to_kelvin <- function(temp) {
  stopifnot(is.numeric(temp))
  kelvin <- ((temp - 32) * (5 / 9)) + 273.15
  return(kelvin)
}

kelvin_to_celsius <- function(temp) {
  celsius <- temp - 273.15
  return(celsius)
}

celsius_to_kelvin <- function(temp) {
  kelvin <- temp + 273.15
  return(kelvin)
}
")
sink()

Now we can load the functions and get ready to install your package:

load_all(".")
## Loading temperature
document(".")

For now document() only created the empty directory man/ and nothing else. That is because there was nothing to document yet.

Now we can install the package from the parent directory:

install("../temperature")

Your package already appears in R-Studio Package tab and can be used. However, there is not much information about the package and its functions.

Our functions are not accessible in R:

fahr_to_kelvin(10)

And there is no information about them:

?fahr_to_kelvin

Let’s add more information to our package using Roxygen syntax. First for the fahr_to_kelvin function:

#' Fahrenheit to Kelvin conversion
#' 
#' This function takes a temperature expressed in Fahrenheit and convert it to Kelvin.
#' @param temp A temperature expressed in Fahrenheit
#'
#' @export
fahr_to_kelvin <- function(temp) {
  stopifnot(is.numeric(temp))
  kelvin <- ((temp - 32) * (5 / 9)) + 273.15
  return(kelvin)
}

The @export line is critical. This tells Roxygen2 to add this function to the NAMESPACE file, so that it will be accessible to users. For your first R package, you’ll probably want to include @export for each of your functions (in large packages it is sometimes a good idea to hide some functions that are used only internally by other functions).

Now let’s repeat the process:

load_all(".")
document(".")

Notice that the NAMESPACE file has changed.

This time a file has been created in /man

install("../temperature")

And we can reach our function help page and run the function:

?fahr_to_kelvin
fahr_to_kelvin(10)

You can see that it is still missing some usual components that are for instance on the mean() help page: Value, References, See Also, Examples…

#' Fahrenheit to Kelvin conversion
#' 
#' This function takes a temperature expressed in Fahrenheit and convert it to Kelvin.
#' @param temp A temperature expressed in Fahrenheit
#' 
#' @return A temperature in Kelvin
#' 
#' @examples
#' fahr_to_kelvin(32)
#' 
#' @export
fahr_to_kelvin <- function(temp) {
  stopifnot(is.numeric(temp))
  kelvin <- ((temp - 32) * (5 / 9)) + 273.15
  return(kelvin)
}
load_all(".", reset = TRUE)
document(".")
install("../temperature", reload = TRUE)
library(temperature)
?fahr_to_kelvin
fahr_to_kelvin(temp = )

By the way, sometimes you need to restart your R-session between installations for RStudio to be able to find help pages.

Now create documentation for kelvin_to_celsius(), re-install the package and check you can access the new help page.

#' Kelvin to Celsius conversion
#'
#' This function takes a temperature expressed in Kelvin and convert it to Celsius.
#'
#' @param temp A temperature expressed in Kelvin
#'
#' @return A temperature in Celsius.
#'
#' @examples
#' kelvin_to_celsius(300)
#'
#' @export
kelvin_to_celsius <- function(temp) {
  celsius <- temp - 273.15
  return(celsius)
}

Initial fill of the DESCRIPTION

Package: temperature
Title: Utilities to handle temperature data
Version: 0.0.1
Authors@R: 
    person(given = "Timothée",
           family = "Bonnet",
           role = c("aut", "cre"),
           email = "timotheebonnetc@gmail.com",
           comment = c(ORCID = "0000-0001-7186-5288"))
Description: What the package does (one paragraph).
License: GPL-3
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0

More complete build

#roxygenise(".", clean = TRUE)
load_all(".")
document(".")
build(pkg = ".", manual = TRUE)
check(".", manual = TRUE )
install("../temperature", reload = TRUE)

Check is very useful. Always try to get 0 errors, 0 warnings, 0 notes.

More complete documentation:

build_manual()
build_readme()
build_site()

At a later stage consider creating vignettes (i.e. tutorials, examples…)

build_vignettes()

Add data

Temperature data from Tuggeranong 1997-2019 http://www.bom.gov.au/climate/data/

download.file("https://timotheenivalis.github.io/data/tuggeranong.csv", 
              destfile = "tuggeranong.csv")
tuggeranong <- read.csv("tuggeranong.csv")

Initialize the dataset

use_data(tuggeranong, overwrite = TRUE)

We need to document the dataset. The documentation goes into the /R folder in a file called data.R.

sink("R/data.R")
cat("
#' Mean maximum temperature (monthly average) at Tuggeranong
#'
#' A dataset containing the average mean maximum temperature per month from 1997 to 2019.
#'
#' @format A data frame with 23 rows and 13 variables:
#' \\describe{
#'   \\item{Year}{Year}
#'   \\item{Jan}{January mean maximum temperature}
#'   \\item{Feb}{February mean maximum temperature}
#'   \\item{Mar}{March mean maximum temperature}
#'   \\item{Apr}{April mean maximum temperature}
#'   \\item{May}{May mean maximum temperature}
#'   \\item{Jun}{June mean maximum temperature}
#'   \\item{Jul}{July mean maximum temperature}
#'   \\item{Aug}{August mean maximum temperature}
#'   \\item{Sep}{September mean maximum temperature}
#'   \\item{Oct}{October mean maximum temperature}
#'   \\item{Nov}{November mean maximum temperature}
#'   \\item{Dec}{December mean maximum temperature}
#'   ...
#' }
#' @source \\url{http://www.bom.gov.au/climate/data/}
\"tuggeranong\"
")
sink()

Don’t use @export for a dataset. The export is automatic.

#roxygenise(".", clean = TRUE)
load_all(".")
document(".")
build(pkg = ".", manual = TRUE)
check(".", manual = TRUE )
install("../temperature", reload = TRUE)
library(temperature)
data("tuggeranong")
tuggeranong
##    Year  Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
## 1  1997 27.2 30.4 24.8 23.0 16.7 12.8 12.2 13.9 16.1 21.7 26.9 29.9
## 2  1998 29.5 30.5 27.8 21.4 16.2 12.8 11.2 13.7 17.7 18.7 21.4 27.8
## 3  1999 29.5 27.1 25.1 19.0 17.8 12.6 13.3 14.9 18.1 20.2 20.9 24.9
## 4  2000 25.1 29.2 25.7 20.4 15.1 12.6 11.9 13.5 18.2 19.5 22.8 28.0
## 5  2001 31.2 29.0 23.7 21.3 16.6 14.3 12.6 13.7 18.8 18.9 22.4 26.5
## 6  2002 28.7 24.5 25.5 22.6 16.2 13.4 12.8 14.9 17.9 21.7 27.7 28.4
## 7  2003 30.6 29.2 23.9 20.1 17.2 13.2 12.5 13.3 16.8 17.9 24.4 27.6
## 8  2004 29.5 30.5 27.0 22.1 16.5 13.1 12.1 14.6 17.5 20.3 23.7 26.4
## 9  2005 28.8 27.0 24.5 24.3 18.3 13.9 12.7 14.5 16.4 20.4 22.9 28.8
## 10 2006 30.6 30.5 27.0 19.6 16.1 11.8 12.5 15.4 19.3 24.0 26.4 27.7
## 11 2007 30.8 28.3 25.8 22.0 17.9 11.6 11.4 15.2 17.4 23.0 24.8 25.1
## 12 2008 29.2 24.5 26.0 19.6 17.2 14.5 11.6 13.0 18.3 23.1 23.1 24.9
## 13 2009 31.3 28.9 26.6 19.6 16.4 12.4 12.6 14.7 17.6 18.8 28.8 29.0
## 14 2010 31.6 27.2 24.9 21.4 16.6 13.3 12.8 12.5 16.7 19.7 22.5 24.9
## 15 2011 28.7 26.5 23.3 19.4 15.3 13.4 12.2 15.8 17.9 20.3 25.0 23.4
## 16 2012 26.8 24.7 22.1 20.2 15.5 12.7 12.7 13.5 18.0 20.9 25.3 27.2
## 17 2013 32.0 26.8 25.1 21.8 16.8 13.4 12.7 14.3 19.7 21.9 23.7 28.5
## 18 2014 31.2 28.9 24.1 19.3 17.4 13.0 12.1 13.9 17.8 22.7 27.3 27.3
## 19 2015 26.9 27.1 25.6 18.5 16.0 13.3 10.9 13.4 17.2 24.8 24.4 28.6
## 20 2016 27.7 29.1 26.8 23.4 17.2 12.8 12.6 14.2 15.7 18.5 25.0 28.1
## 21 2017 31.9 29.6 25.5 19.6 16.2 13.4 12.7 13.6 17.8 22.6 23.8 27.4
## 22 2018 31.0 28.3 26.3 24.9 17.3 13.0 13.3 13.7 17.9 22.4 24.1 29.1
## 23 2019 34.1 28.9 25.6 22.5 16.4 13.7 13.6 14.1 18.6 23.3 26.2 31.2
image(as.matrix(tuggeranong[,-1]))

At least two reasons to include data in your package:

  1. Give realistic input to examples in your function documentation
  2. Share data along your analyses so that analyses can be reproduced and improved

Connect to other packages

So far our package only uses R-base code. However we could write fancier functions if we used code already available in other packages.

Let’s try to include this function in the package:

Wide_To_Long_Temperature <- function(dat, id.vars="Year", 
                                variable.name = "Month",
                                value.name = "Temperature"){
  require(reshape2)
  stopifnot(expr={is.data.frame(dat)
    id.vars %in% colnames(dat)
    })
  melttemp <- melt(dat, id.vars = id.vars,
                 variable.name = variable.name, 
                 value.name = value.name)
  return(melttemp)
}

Wide_To_Long_Temperature(tuggeranong)
## Loading required package: reshape2
##     Year Month Temperature
## 1   1997   Jan        27.2
## 2   1998   Jan        29.5
## 3   1999   Jan        29.5
## 4   2000   Jan        25.1
## 5   2001   Jan        31.2
## 6   2002   Jan        28.7
## 7   2003   Jan        30.6
## 8   2004   Jan        29.5
## 9   2005   Jan        28.8
## 10  2006   Jan        30.6
## 11  2007   Jan        30.8
## 12  2008   Jan        29.2
## 13  2009   Jan        31.3
## 14  2010   Jan        31.6
## 15  2011   Jan        28.7
## 16  2012   Jan        26.8
## 17  2013   Jan        32.0
## 18  2014   Jan        31.2
## 19  2015   Jan        26.9
## 20  2016   Jan        27.7
## 21  2017   Jan        31.9
## 22  2018   Jan        31.0
## 23  2019   Jan        34.1
## 24  1997   Feb        30.4
## 25  1998   Feb        30.5
## 26  1999   Feb        27.1
## 27  2000   Feb        29.2
## 28  2001   Feb        29.0
## 29  2002   Feb        24.5
## 30  2003   Feb        29.2
## 31  2004   Feb        30.5
## 32  2005   Feb        27.0
## 33  2006   Feb        30.5
## 34  2007   Feb        28.3
## 35  2008   Feb        24.5
## 36  2009   Feb        28.9
## 37  2010   Feb        27.2
## 38  2011   Feb        26.5
## 39  2012   Feb        24.7
## 40  2013   Feb        26.8
## 41  2014   Feb        28.9
## 42  2015   Feb        27.1
## 43  2016   Feb        29.1
## 44  2017   Feb        29.6
## 45  2018   Feb        28.3
## 46  2019   Feb        28.9
## 47  1997   Mar        24.8
## 48  1998   Mar        27.8
## 49  1999   Mar        25.1
## 50  2000   Mar        25.7
## 51  2001   Mar        23.7
## 52  2002   Mar        25.5
## 53  2003   Mar        23.9
## 54  2004   Mar        27.0
## 55  2005   Mar        24.5
## 56  2006   Mar        27.0
## 57  2007   Mar        25.8
## 58  2008   Mar        26.0
## 59  2009   Mar        26.6
## 60  2010   Mar        24.9
## 61  2011   Mar        23.3
## 62  2012   Mar        22.1
## 63  2013   Mar        25.1
## 64  2014   Mar        24.1
## 65  2015   Mar        25.6
## 66  2016   Mar        26.8
## 67  2017   Mar        25.5
## 68  2018   Mar        26.3
## 69  2019   Mar        25.6
## 70  1997   Apr        23.0
## 71  1998   Apr        21.4
## 72  1999   Apr        19.0
## 73  2000   Apr        20.4
## 74  2001   Apr        21.3
## 75  2002   Apr        22.6
## 76  2003   Apr        20.1
## 77  2004   Apr        22.1
## 78  2005   Apr        24.3
## 79  2006   Apr        19.6
## 80  2007   Apr        22.0
## 81  2008   Apr        19.6
## 82  2009   Apr        19.6
## 83  2010   Apr        21.4
## 84  2011   Apr        19.4
## 85  2012   Apr        20.2
## 86  2013   Apr        21.8
## 87  2014   Apr        19.3
## 88  2015   Apr        18.5
## 89  2016   Apr        23.4
## 90  2017   Apr        19.6
## 91  2018   Apr        24.9
## 92  2019   Apr        22.5
## 93  1997   May        16.7
## 94  1998   May        16.2
## 95  1999   May        17.8
## 96  2000   May        15.1
## 97  2001   May        16.6
## 98  2002   May        16.2
## 99  2003   May        17.2
## 100 2004   May        16.5
## 101 2005   May        18.3
## 102 2006   May        16.1
## 103 2007   May        17.9
## 104 2008   May        17.2
## 105 2009   May        16.4
## 106 2010   May        16.6
## 107 2011   May        15.3
## 108 2012   May        15.5
## 109 2013   May        16.8
## 110 2014   May        17.4
## 111 2015   May        16.0
## 112 2016   May        17.2
## 113 2017   May        16.2
## 114 2018   May        17.3
## 115 2019   May        16.4
## 116 1997   Jun        12.8
## 117 1998   Jun        12.8
## 118 1999   Jun        12.6
## 119 2000   Jun        12.6
## 120 2001   Jun        14.3
## 121 2002   Jun        13.4
## 122 2003   Jun        13.2
## 123 2004   Jun        13.1
## 124 2005   Jun        13.9
## 125 2006   Jun        11.8
## 126 2007   Jun        11.6
## 127 2008   Jun        14.5
## 128 2009   Jun        12.4
## 129 2010   Jun        13.3
## 130 2011   Jun        13.4
## 131 2012   Jun        12.7
## 132 2013   Jun        13.4
## 133 2014   Jun        13.0
## 134 2015   Jun        13.3
## 135 2016   Jun        12.8
## 136 2017   Jun        13.4
## 137 2018   Jun        13.0
## 138 2019   Jun        13.7
## 139 1997   Jul        12.2
## 140 1998   Jul        11.2
## 141 1999   Jul        13.3
## 142 2000   Jul        11.9
## 143 2001   Jul        12.6
## 144 2002   Jul        12.8
## 145 2003   Jul        12.5
## 146 2004   Jul        12.1
## 147 2005   Jul        12.7
## 148 2006   Jul        12.5
## 149 2007   Jul        11.4
## 150 2008   Jul        11.6
## 151 2009   Jul        12.6
## 152 2010   Jul        12.8
## 153 2011   Jul        12.2
## 154 2012   Jul        12.7
## 155 2013   Jul        12.7
## 156 2014   Jul        12.1
## 157 2015   Jul        10.9
## 158 2016   Jul        12.6
## 159 2017   Jul        12.7
## 160 2018   Jul        13.3
## 161 2019   Jul        13.6
## 162 1997   Aug        13.9
## 163 1998   Aug        13.7
## 164 1999   Aug        14.9
## 165 2000   Aug        13.5
## 166 2001   Aug        13.7
## 167 2002   Aug        14.9
## 168 2003   Aug        13.3
## 169 2004   Aug        14.6
## 170 2005   Aug        14.5
## 171 2006   Aug        15.4
## 172 2007   Aug        15.2
## 173 2008   Aug        13.0
## 174 2009   Aug        14.7
## 175 2010   Aug        12.5
## 176 2011   Aug        15.8
## 177 2012   Aug        13.5
## 178 2013   Aug        14.3
## 179 2014   Aug        13.9
## 180 2015   Aug        13.4
## 181 2016   Aug        14.2
## 182 2017   Aug        13.6
## 183 2018   Aug        13.7
## 184 2019   Aug        14.1
## 185 1997   Sep        16.1
## 186 1998   Sep        17.7
## 187 1999   Sep        18.1
## 188 2000   Sep        18.2
## 189 2001   Sep        18.8
## 190 2002   Sep        17.9
## 191 2003   Sep        16.8
## 192 2004   Sep        17.5
## 193 2005   Sep        16.4
## 194 2006   Sep        19.3
## 195 2007   Sep        17.4
## 196 2008   Sep        18.3
## 197 2009   Sep        17.6
## 198 2010   Sep        16.7
## 199 2011   Sep        17.9
## 200 2012   Sep        18.0
## 201 2013   Sep        19.7
## 202 2014   Sep        17.8
## 203 2015   Sep        17.2
## 204 2016   Sep        15.7
## 205 2017   Sep        17.8
## 206 2018   Sep        17.9
## 207 2019   Sep        18.6
## 208 1997   Oct        21.7
## 209 1998   Oct        18.7
## 210 1999   Oct        20.2
## 211 2000   Oct        19.5
## 212 2001   Oct        18.9
## 213 2002   Oct        21.7
## 214 2003   Oct        17.9
## 215 2004   Oct        20.3
## 216 2005   Oct        20.4
## 217 2006   Oct        24.0
## 218 2007   Oct        23.0
## 219 2008   Oct        23.1
## 220 2009   Oct        18.8
## 221 2010   Oct        19.7
## 222 2011   Oct        20.3
## 223 2012   Oct        20.9
## 224 2013   Oct        21.9
## 225 2014   Oct        22.7
## 226 2015   Oct        24.8
## 227 2016   Oct        18.5
## 228 2017   Oct        22.6
## 229 2018   Oct        22.4
## 230 2019   Oct        23.3
## 231 1997   Nov        26.9
## 232 1998   Nov        21.4
## 233 1999   Nov        20.9
## 234 2000   Nov        22.8
## 235 2001   Nov        22.4
## 236 2002   Nov        27.7
## 237 2003   Nov        24.4
## 238 2004   Nov        23.7
## 239 2005   Nov        22.9
## 240 2006   Nov        26.4
## 241 2007   Nov        24.8
## 242 2008   Nov        23.1
## 243 2009   Nov        28.8
## 244 2010   Nov        22.5
## 245 2011   Nov        25.0
## 246 2012   Nov        25.3
## 247 2013   Nov        23.7
## 248 2014   Nov        27.3
## 249 2015   Nov        24.4
## 250 2016   Nov        25.0
## 251 2017   Nov        23.8
## 252 2018   Nov        24.1
## 253 2019   Nov        26.2
## 254 1997   Dec        29.9
## 255 1998   Dec        27.8
## 256 1999   Dec        24.9
## 257 2000   Dec        28.0
## 258 2001   Dec        26.5
## 259 2002   Dec        28.4
## 260 2003   Dec        27.6
## 261 2004   Dec        26.4
## 262 2005   Dec        28.8
## 263 2006   Dec        27.7
## 264 2007   Dec        25.1
## 265 2008   Dec        24.9
## 266 2009   Dec        29.0
## 267 2010   Dec        24.9
## 268 2011   Dec        23.4
## 269 2012   Dec        27.2
## 270 2013   Dec        28.5
## 271 2014   Dec        27.3
## 272 2015   Dec        28.6
## 273 2016   Dec        28.1
## 274 2017   Dec        27.4
## 275 2018   Dec        29.1
## 276 2019   Dec        31.2
#' Wide to long conversion for standard monthly temperature
#'
#' This function takes a standard monthly temperature dataframe in wide format to long format.
#'
#' @param dat A temperature dataframe
#' @param id.vars Character string. vector of id variables. Can be integer (variable position) or string (variable name). If blank, will use all non-measured variables.
#' @param variable.name Character string. Nname of variable used to store measured variable names
#' @param value.name Character string. Name of variable used to store values
#'
#' @return A dataframe
#'
#' @examples
#' data("tuggeranong")
#' Wide_To_Long_Temperature(tuggeranong)
#'
#' @export
Wide_To_Long_Temperature <- function(dat, id.vars="Year",
                                     variable.name = "Month",
                                     value.name = "Temperature"){
  require(reshape2)
  stopifnot(expr={is.data.frame(dat)
    id.vars %in% colnames(dat)
  })
  melttemp <- melt(dat, id.vars = id.vars,
                   variable.name = variable.name,
                   value.name = value.name)
  return(melttemp)
}
load_all()
document()
check()

We get a warning message. Never use library() or require() in a R package!

Two things to do:

  1. In DESCRIPTION add:

    Imports:
    reshape2
  2. In your function change melt to reshape2::melt

load_all()
## Loading temperature
## Warning: 
## ── Conflicts ──────────────────────────────────────────────────────────────────────────── temperature conflicts ──
## x Wide_To_Long_Temperature() masks temperature::Wide_To_Long_Temperature()
## 
## Did you accidentally source a file rather than using `load_all()`?
## Run `rm(list = c("Wide_To_Long_Temperature"))` to remove the conflicts.
document()
## Updating temperature documentation
## Loading temperature
## Warning: 
## ── Conflicts ──────────────────────────────────────────────────────────────────────────── temperature conflicts ──
## x Wide_To_Long_Temperature() masks temperature::Wide_To_Long_Temperature()
## 
## Did you accidentally source a file rather than using `load_all()`?
## Run `rm(list = c("Wide_To_Long_Temperature"))` to remove the conflicts.
## Writing NAMESPACE
## Writing NAMESPACE
check()
## Updating temperature documentation
## Loading temperature
## Warning: 
## ── Conflicts ──────────────────────────────────────────────────────────────────────────── temperature conflicts ──
## x Wide_To_Long_Temperature() masks temperature::Wide_To_Long_Temperature()
## 
## Did you accidentally source a file rather than using `load_all()`?
## Run `rm(list = c("Wide_To_Long_Temperature"))` to remove the conflicts.
## Writing NAMESPACE
## Writing NAMESPACE
## ── Building ─────────────────────────────────────────────────────────────────────────────────────── temperature ──
## Setting env vars:
## ● CFLAGS    : -Wall -pedantic
## ● CXXFLAGS  : -Wall -pedantic
## ● CXX11FLAGS: -Wall -pedantic
## ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────
##   
   checking for file ‘/home/timothee/Documents/CanberraPostDoc/teaching/RStats2020/Rpackages/temperature/DESCRIPTION’ ...
  
✓  checking for file ‘/home/timothee/Documents/CanberraPostDoc/teaching/RStats2020/Rpackages/temperature/DESCRIPTION’
## 
  
─  preparing ‘temperature’:
##    checking DESCRIPTION meta-information ...
  
✓  checking DESCRIPTION meta-information
## 
  
─  checking for LF line-endings in source and make files and shell scripts
## 
  
─  checking for empty or unneeded directories
## ─  looking to see if a ‘data/datalist’ file should be added
## 
  
─  building ‘temperature_0.0.1.tar.gz’
## 
  
   
## 
── Checking ─────────────────────────────────────────────────────────────────────────────────────── temperature ──
## Setting env vars:
## ● _R_CHECK_CRAN_INCOMING_USE_ASPELL_: TRUE
## ● _R_CHECK_CRAN_INCOMING_REMOTE_    : FALSE
## ● _R_CHECK_CRAN_INCOMING_           : FALSE
## ● _R_CHECK_FORCE_SUGGESTS_          : FALSE
## ● NOT_CRAN                          : true
## ── R CMD check ─────────────────────────────────────────────────────────────────
## * using log directory ‘/tmp/RtmpoR7D94/temperature.Rcheck’
## * using R version 3.6.3 (2020-02-29)
## * using platform: x86_64-pc-linux-gnu (64-bit)
## * using session charset: UTF-8
## * using options ‘--no-manual --as-cran’
## * checking for file ‘temperature/DESCRIPTION’ ... OK
## * this is package ‘temperature’ version ‘0.0.1’
## * package encoding: UTF-8
## * checking package namespace information ... OK
## * checking package dependencies ... OK
## * checking if this is a source package ... OK
## * checking if there is a namespace ... OK
## * checking for executable files ... OK
## * checking for hidden files and directories ... OK
## * checking for portable file names ... OK
## * checking for sufficient/correct file permissions ... OK
## * checking serialization versions ... OK
## * checking whether package ‘temperature’ can be installed ... OK
## * checking installed package size ... OK
## * checking package directory ... OK
## * checking for future file timestamps ... OK
## * checking DESCRIPTION meta-information ... OK
## * checking top-level files ... NOTE
## Non-standard file/directory found at top level:
##   ‘preparation_files’
## * checking for left-over files ... OK
## * checking index information ... OK
## * checking package subdirectories ... OK
## * checking R files for non-ASCII characters ... OK
## * checking R files for syntax errors ... OK
## * checking whether the package can be loaded ... OK
## * checking whether the package can be loaded with stated dependencies ... OK
## * checking whether the package can be unloaded cleanly ... OK
## * checking whether the namespace can be loaded with stated dependencies ... OK
## * checking whether the namespace can be unloaded cleanly ... OK
## * checking loading without being on the library search path ... OK
## * checking dependencies in R code ... OK
## * checking S3 generic/method consistency ... OK
## * checking replacement functions ... OK
## * checking foreign function calls ... OK
## * checking R code for possible problems ... OK
## * checking Rd files ... OK
## * checking Rd metadata ... OK
## * checking Rd line widths ... OK
## * checking Rd cross-references ... OK
## * checking for missing documentation entries ... OK
## * checking for code/documentation mismatches ... OK
## * checking Rd \usage sections ... OK
## * checking Rd contents ... OK
## * checking for unstated dependencies in examples ... OK
## * checking contents of ‘data’ directory ... OK
## * checking data for non-ASCII characters ... OK
## * checking data for ASCII and uncompressed saves ... OK
## * checking examples ... OK
## * checking for detritus in the temp directory ... OK
## * DONE
## 
## Status: 1 NOTE
## See
##   ‘/tmp/RtmpoR7D94/temperature.Rcheck/00check.log’
## for details.
## ── R CMD check results ────────────────────────────────── temperature 0.0.1 ────
## Duration: 9.8s
## 
## > checking top-level files ... NOTE
##   Non-standard file/directory found at top level:
##     ‘preparation_files’
## 
## 0 errors ✓ | 0 warnings ✓ | 1 note x

No more warnings!

Now practice by adding the function below to your package:

plot_trend <- function(dat=tuggeranong){
  melttemp <- Wide_To_Long_Temperature(dat)
  
  model <- summary(lm(Temperature ~ Year + Month, data = melttemp))
  year_effect <- round(model$coefficients["Year",],3)
  
  library(ggplot2)
  ggplot(data=melttemp, 
         aes(x=Year,y=Temperature, color=Month))+
    geom_point() + geom_smooth(alpha=0.2) +
    annotate("label", x = mean(melttemp$Year), 
             y = max(melttemp$Temperature), 
             label = paste0(ifelse(year_effect[1]>=0, "+", "-"),
                            year_effect[1], "°C/year, p=",year_effect[4])) 
}

Make the package a GitHub repository and install from github

If you already have a Github account create a new repo for your package. You can leave the repository completely empty during initialization.

echo "# temperature" >> README.md
git init
git add README.md
git commit -m "first commit"
git remote add origin https://github.com/timotheenivalis/temperature.git
git push -u origin master
git add data/ man/ R/ NAMESPACE DESCRIPTION .gitignore .Rbuildignore
git commit -m "load all package files"
git push

Now people can install your package from github. If you want to see my final code, it is there!

install_github("timotheenivalis/temperature")
## Downloading GitHub repo timotheenivalis/temperature@master
## 
##   
   checking for file ‘/tmp/RtmpoR7D94/remotes32a3272eb708/timotheenivalis-temperature-3c7f1ce/DESCRIPTION’ ...
  
✓  checking for file ‘/tmp/RtmpoR7D94/remotes32a3272eb708/timotheenivalis-temperature-3c7f1ce/DESCRIPTION’
## 
  
─  preparing ‘temperature’:
## 
  
   checking DESCRIPTION meta-information ...
  
✓  checking DESCRIPTION meta-information
## 
  
─  checking for LF line-endings in source and make files and shell scripts
## 
  
─  checking for empty or unneeded directories
## ─  looking to see if a ‘data/datalist’ file should be added
## 
  
─  building ‘temperature_0.0.1.tar.gz’
## 
  
   
## 
## Installing package into '/home/timothee/R/x86_64-pc-linux-gnu-library/3.6'
## (as 'lib' is unspecified)

More information

R packages book by Hadley Wickham: http://r-pkgs.had.co.nz/intro.html