(Name and initial idea from robertzk.)
Assignment
number = 42 --> RSPRESSO! --> number <- 42
Functions
square = (x) -> x * x --> RSPRESSO! --> square <- function(x) x * x
Vectors
[10..1] --> RSPRESSO! --> seq(10,1)
[1, 2, 3] --> RSPRESSO! --> c(1, 2, 3)
Vector operations
[1, 2, 3] << 4 --> RSPRESSO! --> c(c(1, 2, 3), 4)
[a, b] = 2 --> RSPRESSO! --> a <- b <- 2
]a, b, c] = [2, 3, 4] --> RSPRESSO! --> a <- 2, b <- 3, c <- 4
Lists
{one: 1, two: 2, three: 3} --> RSPRESSO! --> list("one" = 1, "two" = 2, "three" = 3)
ll = {one: 1}; ll.one --> RSPRESSO! --> ll <- list("one" = 1); ll$one
{one: 1, two: 2, three: 3} << {four: 4} --> RSPRESSO! --> c(list("one" = 1, "two" = 2, "three" = 3), list("four" = 4)
Other Rubyisms
variable ||= 2 --> RSPRESSO! --> variable <- if (!(exists("variable")) variable else 2
"This is a string with a #{number} in it!" --> RSPRESSO! --> paste0("This is a string with a ", number, " in it!")
Conditionals
number = -42 if opposite --> RSPRESSO! --> if (opposite) number <- -42
number = 42 unless opposite --> RSPRESSO! --> if (!(opposite)) number <- 42
if happy and you_know_it --> RSPRESSO! --> if (happy & you_know_it)
if variable == 'value' --> RSPRESSO! --> if (identical(variable, 'value'))
date = if friday sue else jill --> RSPRESSO! --> date <- friday ? sue : jill
Indentation over brackets
if x == 5
function_one
function_two
function_three
else
function_a
function_b--> RSPRESSO! -->
if (x == 5) {
function_one()
function_two()
function_three()
} else {
function_a()
function_b()
}Chained Comparisons
healthy = 200 > cholesterol > 60 --> RSPRESSO! --> healthy <- (cholesterol > 60 & cholesterol < 200)
Loops
eat food for food in ['toast', 'cheese', 'wine'] --> RSPRESSO! --> sapply(['toast', 'cheese', 'wine'], function(food) { eat(food) })
evens = (x for x in [0..10] by 2) --> RSPRESSO! --> evens <- sapply(seq(0, 10, by=2), function(x) x)
Accessing
var[>20] --> RSPRESSO! --> var[var > 20 & !is.na(var)]
var[>10&<20] --> RSPRESSO! --> var[var > 10 & var < 20 & !is.na(var)]
var[$data==2, 3] --> RSPRESSO! --> var[var$data == 2, 3]
Try Catch
try
17 / 0
catch error
print error--> RSPRESSO! -->
tryCatch({
17/0
}, error = function(error) {
print(error)
}Existential Operator
variable? --> RSPRESSO! --> exists("variable")