Last active
November 15, 2019 15:51
-
-
Save AkselA/1b141fb44b6a8b982b5d80cb0dd48290 to your computer and use it in GitHub Desktop.
Assistance for initializing an R project. Start by doing devtools::source_gist("1b141fb44b6a8b982b5d80cb0dd48290", filename="proj_init.r"), then supply the appropriate arguments to proj_init()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
Dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
cd $Dir | |
echo "Enter commit message" | |
read Message | |
if [ -z "$Message" ] | |
then | |
echo "aborting due to missing commit message" | |
exit 1 | |
fi | |
git add -A && git commit -m "$Message" | |
echo "Push? (y/n)" | |
read Push | |
case "$Push" in ([yY]) git push ;; esac | |
$SHELL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Collection of functions and commands useful when writing documentation for a package | |
# load required libraries | |
library(roxygen2) | |
library(devtools) | |
# set project directory and name | |
setwd("enter_projdir") | |
projname <- "enter_projname" | |
githubname <- "enter_githubname" | |
# Inspect package object sizes | |
objsizes <- function(projname, load.installed=FALSE) { | |
if (load.installed) { | |
ppath <- find.package(projname, lib.loc=.libPaths()) | |
ppathr <- paste0(, "/R/", projname) | |
lazyLoad(ppathr) | |
} | |
pkgls <- ls(paste0("package:", projname)) | |
ll <- t(sapply(pkgls, function(x) { | |
o <- get(x) | |
c(bytes=object.size(o), class=class(o)) | |
})) | |
dtf <- type.convert(data.frame(ll)) | |
dtf$"SI size" <- sapply(dtf[,1], | |
utils:::format.object_size, units="auto", standard="SI") | |
tot <- sum(dtf[,1]) | |
tot <- utils:::format.object_size(tot, units="auto", standard="SI") | |
dtf <- dtf[order(-dtf[,1]), c(1, 3, 2)] | |
print(dtf) | |
cat(paste("\nTotal:", tot)) | |
invisible(list(dtf, total=tot)) | |
} | |
# turns objects found in "projname"/data.R (project root) | |
# into data files available through data() | |
# by saving them as .rda files in "projname"/data | |
add_data <- function(projname) { | |
if (!dir.exists(projname)) { | |
stop(paste("Could not find", projname, "in current directory")) | |
} | |
datapath <- file.path(projname, "data.R") | |
if (!file.exists(datapath)) { | |
stop(paste("Could not find", datapath)) | |
} | |
dir.create(file.path(projname, "data"), showWarnings=FALSE) | |
tmp.env <- new.env() | |
source(datapath, local=tmp.env) | |
tmp.list <- as.list(tmp.env, sorted=TRUE) | |
if (length(tmp.list) == 0) { | |
message("No objects found in data.R") | |
return(NULL) | |
} | |
files <- file.path(projname, "data", paste0(names(tmp.list), ".rda")) | |
obj <- mapply(save, list=names(tmp.list), file=files, | |
MoreArgs=list(compress="xz", envir=tmp.env)) | |
if (length(files) == 1) { | |
cat("File added:") | |
} else { | |
cat("Files added:") | |
} | |
dtf <- data.frame(x=paste0(files), | |
y=paste(sprintf("%.1f", file.size(files)/1000), "kB")) | |
names(dtf) <- c(" ", " ") | |
dtf | |
} | |
# Create and show documentation PDF | |
show_pdf <- function(package, lib.loc=NULL, opt="--force") { | |
owd <- getwd() | |
setwd(package) | |
path <- find.package(package, lib.loc) | |
system(paste(shQuote(file.path(R.home("bin"), "R")), | |
"CMD", "Rd2pdf", paste(opt, collapse=" "), | |
shQuote(path))) | |
setwd(owd) | |
} | |
# Get a list of function arguments ready for Roxygen2 use | |
params <- function(fun) { | |
cat(paste("@param", names(formals(fun))), sep="\n") | |
} | |
# Write .Rbuildignore file | |
buildignore <- function(projname, pat=c("^data\\.R", "documenting\\.R", | |
"commit\\.command", "\\.pdf$", "\\.png$", "\\.Rproj$", "^__.*", | |
"^\\.DS_Store$")) { | |
bignore.path <- file.path(projname, ".Rbuildignore") | |
if (!file.exists(bignore.path)) { | |
file.create(bignore.path) | |
} | |
pat <- union(scan(bignore.path, ""), pat) | |
cat(pat, file=bignore.path, sep="\n") | |
} | |
buildignore(projname) | |
# detach(paste0("package:", projname), character.only=TRUE) | |
document(projname) | |
load_all(projname) | |
add_data(projname) | |
check(projname, manual=FALSE) | |
objsizes(projname) | |
show_pdf(projname) | |
# run convenience script to add, commit and maybe push change | |
Sys.chmod(paste0(projname, "/commit.command")) # make executable | |
system(paste0("open ", projname, "/commit.command")) | |
# dev_example(projname) | |
install_github(paste0(githubname, "/R-", projname)) | |
library(projname, character.only=TRUE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(roxygen2) | |
library(devtools) | |
url_gists <- function(id, filename=".*") { | |
files <- devtools:::github_GET(sprintf("gists/%s", id))$files | |
files <- files[grepl(filename, names(files))] | |
if (length(files) == 0) { | |
stop("No matching files found in gist", call. = FALSE) | |
} | |
unlist(lapply(files, `[`, "raw_url")) | |
} | |
proj_init <- function(projname, projdir="~/Documents/R/prosjekter", | |
documenting=url_gists(id="1b141fb44b6a8b982b5d80cb0dd48290", filename="doc"), | |
commit=url_gists(id="1b141fb44b6a8b982b5d80cb0dd48290", filename="commit"), | |
githubname="AkselA") { | |
setwd(projdir) | |
create(projname) | |
use_build_ignore(c("data.R", "documenting.R", "commit.command"), pkg=projname) | |
setwd(projname) | |
l <- readLines(documenting) | |
l <- gsub("enter_projname", projname, l) | |
l <- gsub("enter_projdir", projdir, l) | |
l <- gsub("enter_githubname", githubname, l) | |
# cat(l, sep="\n") | |
writeLines(l, "documenting.R") | |
i <- c("stats", "utils", "graphics", "datasets", "grDevices") | |
i <- paste0(paste("#' @import", i, collapse="\n"), "\n\nNULL") | |
writeLines(i, "R/0_imports.R") | |
download.file(commit, "commit.command") | |
readmetext <- paste0("# ", projname, "\n", "An R project under development") | |
writeLines(readmetext, "README.md") | |
datatext <- paste0( | |
"# This file will not be included in the build of the package,\n", | |
"# but objects created here will, via add_data(), be available\n", | |
"# from the 'data/' folder\n") | |
writeLines(datatext, "data.r") | |
list.files(all.files=TRUE) | |
f <- function(pn, gn, message="first commit") { | |
system("git init") | |
system("git add .") | |
system(paste("git commit -m", shQuote(message))) | |
system(paste0( | |
"git remote add origin https://github.com/", gn, "/R-", pn, ".git")) | |
system("git remote -v") | |
system("git push -u origin master") | |
} | |
formals(f)[c("pn", "gn")] <- c(projname, githubname) | |
fnam <- paste0("init_R-", projname) | |
assign(fnam, f, envir=.GlobalEnv) | |
message("The function ", fnam, " was exported to the global environment.\n", | |
"If github.com/", githubname, "/R-", projname, ".git exists, you can run `", | |
fnam, "`()\nto automatically itialize, make the first commit and push to ", | |
"the remote repository.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment