This is an example of C code calling an R function, by building a function call and then evaluating it.
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(rlang) | |
library(pryr) | |
# =========================== | |
# Normal quosures | |
# =========================== | |
# as_function() with a quosure: OK | |
a <- 1 | |
x <- quo(a + 10) | |
a <- 2 |
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(digest) | |
library(promises) | |
library(cache) | |
# Given a list with items named `value` and `visible`, return `x$value` either | |
# visibly, or invisibly, depending on the value of `x$visible`. | |
valueWithVisible <- function(x) { | |
if (x$visible) x$value else invisible(x$value) | |
} |
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(profvis) | |
library(microbenchmark) | |
library(memoise) | |
library(digest) | |
# Bare function | |
f <- function(a, b) { | |
a + b | |
} |
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(httpuv) | |
# Create a web server app which returns the time and prints out the value of | |
# a header named "test-header". | |
handle_request <- function(req) { | |
list( | |
status = 200L, | |
headers = list('Content-Type' = 'text/plain'), | |
body = paste0( | |
"The time is: ", Sys.time(), "\n", |
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
> install.packages("Rcpp") | |
Installing package into ‘/Users/winston/R/3.6’ | |
(as ‘lib’ is unspecified) | |
trying URL 'https://cloud.r-project.org/bin/macosx/el-capitan/contrib/3.6/Rcpp_1.0.4.tgz' | |
Content type 'application/x-gzip' length 3124401 bytes (3.0 MB) | |
================================================== | |
downloaded 3.0 MB | |
The downloaded binary packages are in |
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
# First, need to install crandb | |
# devtools::install_github('r-hub/crandb') | |
# Memoize crandb::package because we end up making a lot of repetitive calls. | |
package <- memoise::memoise(crandb::package) | |
# Get a list of built-in packages. We don't need to check the version | |
# information for these packages. | |
builtin_pkgs <- memoise::memoise(function() { | |
pkgs <- installed.packages() |
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(httpuv) | |
PORT <- 7000 | |
app <- list( | |
call = function(req) { | |
page(req) | |
}, | |
onWSOpen = function(ws) { | |
ws$onMessage(function(binary, message) { |
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
## Modifying a vector in a parent environment | |
## This tests whether it's faster to use `self` or not, and whether it's faster to use `<-` vs `<<-`. | |
foo <- local({ | |
x <- integer() | |
self <- environment() | |
# Four different ways of setting a value: | |
# * With and without using `self` |
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
# get_symbols() returns all symbols that are registered in R's symbol table. | |
# | |
# new_symbols() returns all symbols that have been added since the last time | |
# new_symbols() was run. If you want to test whether your code causes the symbol | |
# table to grow, run new_symbols(), then run your code, then run new_symbols() | |
# again. | |
get_symbols <- inline::cfunction( | |
includes = " | |
#define HSIZE 49157 /* The size of the hash table for symbols */ |