-
Notifications
You must be signed in to change notification settings - Fork 0
/
makepackagehere.R
209 lines (187 loc) · 7.45 KB
/
makepackagehere.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Make a package automatically.
# author: Joost Kuckartz, jkuckartz1984@hotmail.com
# modified: July 2023
# version: 1.0
#########
# USAGE #
#########
# Before using this file, it's recommended you update existing packages.
#
# Step 1. Place this file plus "makepackagevariables.R" in the folder containing
# the R files to make a package out of it, or place it in the directory
# containing the folder named "R".
#
# Sample 1:
# C:/
# C:/code/ <-- place in this folder
# C:/code/R/
# C:/code/R/script1.R
# C:/code/R/script2.R
#
# Sample 2:
# C:/
# C:/code/ <-- place in this folder
# C:/code/script1.R
# C:/code/script2.R
#
# Step 2: Source this file
#############
# VARIABLES #
#############
rm(list=ls())
# To edit the package variables, edit the 'makepackagevariables.R' file.
# Keeping one file for each package is the most useful.
miktexinstalled = FALSE #set to true if you have MiKTeX installed to make a pdf manual
##########
# SCRIPT #
##########
#' Load a library. If it is not yet installed, it will install it.
#' @param x The library to load
loadlibrary <- function(x) {
if (!require(x,character.only = TRUE)) {
install.packages(x,dependencies=TRUE)
if(!require(x,character.only = TRUE)) stop("Package not found")
}
}
#' Detach all package versions based on specific name. From https://stackoverflow.com/a/6979989/2710064
#' @param pkg The package or its string name to unload
#' @param character.only Set to \code{TRUE} if the provided name is a string. Default=\code{FALSE}.
#' @examples \dontrun{
#' detach_package(vegan)
#' detach_package("vegan", TRUE)
#' }
detach_package <- function(pkg, character.only = FALSE) {
if(!character.only) {
pkg <- deparse(substitute(pkg))
}
search_item <- paste("package", pkg, sep = ":")
while(search_item %in% search()) {
detach(search_item, unload = TRUE, character.only = TRUE)
}
}
loadlibrary("devtools")
loadlibrary("roxygen2")
loadlibrary("stringr")
loadlibrary("data.table")
rm(list = c("loadlibrary")) #no need for the function anymore so no conflict warning message at the end
#prepare move files settings
copydelete=TRUE
curdir=getSrcDirectory(function(x) {x}) #only works if this file is run or sourced!
setwd(curdir)
if (basename(getwd())=="R") { #if this directory is "R"
if (file.exists("../DESCRIPTION")) { #and there is a DESCRIPTION file
setwd("..") #we already have a package directory structure
copydelete=FALSE
}
}
curdir=getwd()
#load package variables
source("makepackagevariables.R")
#move source files to right directory
newdir=paste0(curdir,"/R")
dir.create(newdir, showWarnings = FALSE)
if (copydelete) {
rfiles=list.files(pattern=".R", include.dirs=FALSE) #list R files
locs=is.na(str_locate(rfiles,"makepackagehere")[,1])
rfiles=rfiles[locs] #avoid makepackagehere file
locs=is.na(str_locate(rfiles,"makepackagevariables")[,1])
rfiles=rfiles[locs] #avoid makepackagevariables file
locs=is.na(str_locate(rfiles,"installthispackage")[,1])
rfiles=rfiles[locs] #avoid installthispackage file
message(paste0("Moving ",length(rfiles)," source files..."))
file.rename(paste0(curdir,"/",rfiles),paste0(newdir,"/",rfiles)) #move files to "./R" directory
}
#clean up existing package files for recreation (if present)
isnewpackage=TRUE
chkfile=paste0(curdir,"/DESCRIPTION")
if (file.exists(chkfile)) {
isnewpackage=FALSE
file.remove(chkfile) #remove DESCRIPTION file to update it
}
chkfile=paste0(curdir,"/NAMESPACE")
if (file.exists(chkfile)) {
isnewpackage=FALSE
file.remove(chkfile) #remove NAMESPACE file to update it
}
chkfile=paste0(curdir,"/",packagename,".Rproj")
if (file.exists(chkfile)) {
file.remove(chkfile) #remove Rproj file
}
#prepare description information
if (length(packageauthors)==1) {
authorstring=format(packageauthors[[1]], style="R")
} else if (length(packageauthors)> 1) {
authorstring="c(" #start with list item
for (i in 1:length(packageauthors)) {
if (i>1) {
authorstring=paste0(authorstring,",") #add comma's to add the next person to list
}
rformatauthor=paste0(format(packageauthors[[i]], style="R"),collapse="") #add the person as R executable text
authorstring=paste0(authorstring,rformatauthor)
}
authorstring=paste0(authorstring,")") #close the list
}
existingpackage=as.data.table(installed.packages()) #extract build version from package info (if exists)
existingpackage=existingpackage[Package==packagename,]
packageversion_build = 1
if (nrow(existingpackage)) {
packageversion = existingpackage[,Version]
packageversion = str_split(packageversion,"[.]")[[1]]
curversionmajor=as.numeric(packageversion[1])
if (is.na(curversionmajor)) {
curversionmajor=0
}
curversionminor=as.numeric(packageversion[2])
if (is.na(curversionminor)) {
curversionminor=0
}
if (curversionmajor==packageversion_major & curversionminor==packageversion_minor) {
packageversion_build=as.numeric(packageversion[3])
if (is.na(packageversion_build)) {
packageversion_build=0
}
packageversion_build = packageversion_build + 1
}
}
packagedesc=list( #create package description items
Package = packagename,
Title = packagetitle,
Version = paste0(packageversion_major,".",packageversion_minor,".",packageversion_build),
Date = strftime(Sys.Date(),"%Y-%m-%d"),
`Authors@R` = authorstring,
Description = packagedesctext,
Depends = paste0("R (>= ", as.character(getRversion()), ")"),
License = packagelicense,
Encoding = "UTF-8",
LazyData = "true"
)
#prepare package directory and settings and create it
if (isnewpackage) { #if this is a new package
message(paste0("Creating package ",packagename,"..."))
} else {
message(paste0("Updating package ",packagename,"..."))
}
#trick to avoid question for nesting
challenge_nested_project <- function(path, name) return()
rlang::env_unlock(env = asNamespace('usethis'))
rlang::env_binding_unlock(env = asNamespace('usethis'))
assign('challenge_nested_project', challenge_nested_project, envir = asNamespace('usethis'))
rlang::env_binding_lock(env = asNamespace('usethis'))
rlang::env_lock(asNamespace('usethis'))
#create package
usethis::create_package(curdir,packagedesc,open=FALSE)
#install and document package
message(paste0("Installing package ",packagename,"..."))
document(".") #create documentation for all R files in the folder
detach_package(packagename,TRUE) #ensure the package is not loaded just in case.
install.packages(".", repos = NULL, dependencies = TRUE, type = "source") #install this package and its dependencies
#make pdf documentation file
if (miktexinstalled) {
message(paste0("Creating documentation pdf for package ",packagename,"..."))
if (file.exists(paste0(packagename,".pdf"))) {
file.remove(paste0(packagename,".pdf"))
}
path <- find.package(packagename)
system(paste(shQuote(file.path(R.home("bin"), "R")), "CMD", "Rd2pdf", shQuote(path)))
}
message("All done! Best to close and reopen the R application.")