Permalink
Browse files

Init

  • Loading branch information...
jennybc committed Jul 10, 2018
0 parents commit ad4b7e2a49a169cea14e689c6cfff41146bdcefb
@@ -0,0 +1,6 @@
.Rhistory
.RData
.Rproj.user
developer_survey_2018
Refactoring_improving_the_design_of_existing_code.pdf
keynote*
@@ -0,0 +1,9 @@
x <- 1:5
#x <- c(TRUE, FALSE, FALSE, TRUE, FALSE)

cat(
"The bizarro version of x is",
-x,
#!x,
"\n"
)
@@ -0,0 +1,12 @@
x <- 1:5
#x <- c(TRUE, FALSE, FALSE, TRUE, FALSE)

cat(
"The bizarro version of x is",
if (is.numeric(x)) {
-x
} else {
!x
},
"\n"
)
@@ -0,0 +1,11 @@
bizarro <- function(x) {
if (is.numeric(x)) {
-x
} else {
!x
}
}

bizarro(1:5)

bizarro(c(TRUE, FALSE, FALSE, TRUE, FALSE))
@@ -0,0 +1,17 @@
bizarro <- function(x) {
if (class(x)[[1]] == "numeric" || class(x)[[1]] == "integer") {
-x
} else if (class(x)[[1]] == "logical") {
!x
} else {
stop(
"Don't know how to make bizzaro <", class(x)[[1]], ">",
call. = FALSE)
}
}

bizarro(c(TRUE, FALSE, FALSE, TRUE, FALSE))

bizarro(1:5)

bizarro(c("abc", "def"))
@@ -0,0 +1,17 @@
bizarro <- function(x) {
if (is.numeric(x)) {
-x
} else if (is.logical(x)) {
!x
} else {
stop(
"Don't know how to make bizzaro <", class(x)[[1]], ">",
call. = FALSE)
}
}

bizarro(c(TRUE, FALSE, FALSE, TRUE, FALSE))

bizarro(1:5)

bizarro(c("abc", "def"))
@@ -0,0 +1,19 @@
## https://github.com/tidyverse/googledrive/blob/b6e97999d0781c31c69ef6fa93d1091a04b44d20/R/drive_cp.R#L67

drive_cp <- function(file, ...) {
file <- as_dribble(file)
file <- confirm_single_file(file)
if (is_parental(file)) {
stop_glue("The Drive API does not copy folders or Team Drives.")
}
...
}

https://github.com/tidyverse/googledrive/blob/b6e97999d0781c31c69ef6fa93d1091a04b44d20/R/dribble.R#L253

is_parental <- function(d) {
stopifnot(inherits(d, "dribble"))
kind <- purrr::map_chr(d$drive_resource, "kind")
mime_type <- purrr::map_chr(d$drive_resource, "mimeType", .default = NA)
kind == "drive#teamDrive" | mime_type == "application/vnd.google-apps.folder"
}
@@ -0,0 +1,15 @@
bizarro <- function(x) {
stopifnot(is.numeric(x) || is.logical(x))

if (is.numeric(x)) {
-x
} else {
!x
}
}

bizarro(c(TRUE, FALSE, FALSE, TRUE, FALSE))

bizarro(1:5)

bizarro(c("abc", "def"))
@@ -0,0 +1,22 @@
get_some_data <- function(config, outfile) {
if (config_ok(config)) {
if (can_write(outfile)) {
if (can_open_network_connection(config)) {
data <- parse_something_from_network()
if(makes_sense(data)) {
data <- beautify(data)
write_it(data, outfile)
return(TRUE)
} else {
return(FALSE)
}
} else {
stop("Can't access network")
}
} else {
## uhm. What was this else for again?
}
} else {
## maybe, some bad news about ... the config?
}
}
@@ -0,0 +1,22 @@
get_some_data <- function(config, outfile) {
if (config_bad(config)) {
stop("Bad config")
}

if (!can_write(outfile)) {
stop("Can't write outfile")
}

if (!can_open_network_connection(config)) {
stop("Can't access network")
}

data <- parse_something_from_network()
if(!makes_sense(data)) {
return(FALSE)
}

data <- beautify(data)
write_it(data, outfile)
TRUE
}
@@ -0,0 +1,15 @@
## https://github.com/tidyverse/googledrive/blob/94c8c01cbb9fd96fe59920cf58e39263a3135337/R/process_response.R#L8

process_response <- function(res) {
if (httr::status_code(res) == 204) {
return(TRUE)
}

if (httr::status_code(res) >= 200 && httr::status_code(res) < 300) {
return(res %>%
stop_for_content_type() %>%
httr::content(as = "parsed", type = "application/json"))
}

## 20+ more lines of error handling ...
}
@@ -0,0 +1,35 @@
str_reverse <- function(x) {
vapply(
strsplit(x, ""),
FUN = function(z) paste(rev(z), collapse = ""),
FUN.VALUE = "")
}
str_reverse(c("abc", "def"))

bizarro <- function(x) {
if (is.numeric(x)) {
-x
} else if (is.logical(x)) {
!x
} else if (is.character(x)) {
str_reverse(x)
} else if (is.factor(x)) {
levels(x) <- rev(levels(x))
x
} else {
stop(
"Don't know how to make bizzaro <", class(x)[[1]], ">",
call. = FALSE)
}
}

bizarro(1:5)

bizarro(c(TRUE, FALSE, FALSE, TRUE, FALSE))

bizarro(c("abc", "def"))

(m <- factor(month.abb, levels = month.abb))
bizarro(factor(m))

bizarro(iris)
@@ -0,0 +1,60 @@
## snippets on slides created with lots of selective reprex()ing and toggling of
## the chunk options

#+ include = FALSE
str_reverse <- function(x) {
vapply(
strsplit(x, ""),
FUN = function(z) paste(rev(z), collapse = ""),
FUN.VALUE = "")
}

#+ include = FALSE
bizarro <- function(x) {
UseMethod("bizarro")
}

bizarro.default <- function(x) {
stop(
"Don't know how to make bizzaro <",
class(x)[[1]], ">",
call. = FALSE
)
}

#+ include = FALSE, eval = FALSE
bizarro(1:5)

bizarro(TRUE)

bizarro("abc")

#+ include = FALSE
bizarro.numeric <- function(x) -x

bizarro.logical <- function(x) !x

bizarro.character <- function(x) str_reverse(x)

bizarro.factor <- function(x) {
levels(x) <- rev(levels(x))
x
}

bizarro.data.frame <- function(x) {
names(x) <- bizarro(names(x))
x[] <- lapply(x, bizarro)
x
}

#+ include = TRUE
bizarro(1:5)

bizarro(c(TRUE, FALSE, FALSE, TRUE, FALSE))

bizarro(c("abc", "def"))

(m <- factor(month.abb[1:3], levels = month.abb[1:3]))
bizarro(m)

bizarro(head(iris, 3))
@@ -0,0 +1,28 @@
#+ include = FALSE
str_reverse <- function(x) {
vapply(
strsplit(x, ""),
FUN = function(z) paste(rev(z), collapse = ""),
FUN.VALUE = "")
}

#+ include = TRUE
bizarro <- function(x) {
cls <- class(x)[[1]] ## not a great idea, in general
switch(
cls,
logical = !x,
integer = ,
numeric = -x,
character = str_reverse(x),
stop("Don't know how to make bizzaro <", cls, ">", call. = FALSE)
)
}

bizarro(1:5)

bizarro(c(TRUE, FALSE, FALSE, TRUE, FALSE))

bizarro(c("abc", "def"))

bizarro(iris)
@@ -0,0 +1,14 @@
## https://github.com/tidyverse/stringr/blob/e775974d2822ae0de90ab33cf9f02273c22a3801/R/pad.r#L30

str_pad <- function(string,
width,
side = c("left", "right", "both"),
pad = " ") {
side <- match.arg(side)

switch(side,
left = stri_pad_left(string, width, pad = pad),
right = stri_pad_right(string, width, pad = pad),
both = stri_pad_both(string, width, pad = pad)
)
}
@@ -0,0 +1,25 @@
library(tidyverse)

tibble(
age_yrs = c(0, 4, 10, 15, 24, 55),
age_cat = case_when(
age_yrs < 2 ~ "baby",
age_yrs < 13 ~ "kid",
age_yrs < 20 ~ "teen",
TRUE ~ "adult"
)
)

age_yrs <- c(0, 8, 15, 24, 55)

cat(
ifelse(age_yrs < 2, "baby",
ifelse(age_yrs < 13, "kid",
ifelse(age_yrs < 20, "teen",
"adult"
)
)
)
,sep="\n")


@@ -0,0 +1,19 @@
## https://github.com/r-lib/devtools/blob/b01edfbfa1fd0e3965a24188805c5e55f0d7376f/R/build-manual.R

#' Create package pdf manual
#'
#' @param pkg package path or name
#' @param path path in which to produce package manual
build_manual <- function(pkg = ".", path = NULL) {
pkg <- as.package(pkg)
path <- path %||% dirname(pkg$path)
...
}

if (is.null(path)) {
path <- dirname(pkg$path)
}

`%||%` <- function(x, y) {
if (is_null(x)) y else x
}
@@ -0,0 +1,21 @@
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

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
Oops, something went wrong.

0 comments on commit ad4b7e2

Please sign in to comment.