Skip to content

Instantly share code, notes, and snippets.

View hadley's full-sized avatar

Hadley Wickham hadley

View GitHub Profile

Motivation

The base R API for file and directory manipulation is idiosyncratic, and scattered over multiple packages. This package provide an API with:

  • consistent input arguments and return values
  • wrappers for common tasks that are currently painful

Common API

Functions are named according to their unix equivalents (except for ls, which has a different meaning in R). Generally favour a smaller set of function with not too many number of options. But there's no need to capture all options from equivalent command line tools, and generally long argument names are preferred to cryptic abbreviations.

XXX establishes a common way to store and retrieve values across runs of an R package. This makes it easy to configure a package once for a user and continue to use the same settings afterwards.

A typical use case (inside a package), might look like:

s <- settings("my_settings.rdata")
s$set("a", 1)
s$get("a")
# [1] 1

New R session

library(memoise)
library(stringr)
git_prompt <- function() {
git <- find_git()
if (is.null(git)) stop("Git not installed", call. = FALSE)
if (!in_git_repo(git)) stop("Not in git repo", call. = FALSE)
update <- function(...) update_prompt(git)
> test_package("devtools")
Dependencies : .............
Documentation checks : ...
Compiled DLLs : Error in file(filename, "r", encoding = encoding) :
cannot open the connection
Calls: local ... eval.parent -> eval -> eval -> eval -> eval -> source -> file
In addition: Warning message:
In file(filename, "r", encoding = encoding) :
cannot open file 'startup.Rs': No such file or directory
Execution halted
roc_title <- roccer("@title",
parse_tag = parse_string(),
output_tag = output_rd("title"),
)
roc_description <- roccer("@description",
parse_tag = parse_string(),
output_tag = output_rd("description"),
)
roc_details <- roccer("@details",
parse_tag = parse_string(),
# try_con("svn://svn.r-forge.r-project.org/svnroot/depmix/")
# try_con("svn://svn.rforge.net/Rserve/trunk")
# try_con("svn://hedgehog.fhcrc.org/bioconductor/trunk/madman/Rpacks")
try_con <- function(url, debug = TRUE) {
pieces <- httr::parse_url(url)
s <- make.socket(pieces$hostname, 3690)
on.exit(close.socket(s))

[Important terminology: roc is short for roxygen comment block, rocblock and for combination of roc and object that's being documented]

Roxygen design

The roccer is the object in charge of processing a tag. It is made up of two components:

  • A parser, which is in charge of converting the raw text string into an intermediate object that can be used by other roccers and is turned into output

  • An rocout object, which takes the intermediate format (after it's potentially been modified by other roccers) and turns it into an output format

@hadley
hadley / subset2.r
Created October 12, 2012 17:55
subset deconstruction
SUBSET2 <- function(dat, ex) {
ex <- substitute(ex)
frames <- rev(sys.frames())
for(f in frames) {
message("---------------------------------------------")
cat("Vars: ", paste(ls(f), sep = ","), "\n")
call <- substitute(substitute(x, f), list(x = ex))
ex <- eval(call)
cat("Expr: ", deparse(ex), "\n")
# ruby imap.rb > mail.csv
require 'net/imap'
username = ''
password = ''
imap = Net::IMAP.new('imap.gmail.com','993',true)
imap.login(username, password)
# imap.list("", "*").map{|i| i.name}
clusters <- read.csv("clusters.csv", stringsAsFactors = FALSE)
x <- clusters$clean_text
non_ascii <- function(x) {
any(charToRaw(x) > 0x7F)
}
bad <- x[vapply(x, non_ascii, logical(1), USE.NAMES = FALSE)]
get <- function(x) {