Skip to content

Instantly share code, notes, and snippets.

View bfatemi's full-sized avatar
🎯
In my zone

Bobby Fatemi bfatemi

🎯
In my zone
View GitHub Profile
@bfatemi
bfatemi / zcomps.R
Created September 17, 2019 18:12
get comparable homes given a zillow home ID
getCompChain <- function(id, n = 100, asDT=FALSE, store = TRUE){
fn_comp <- function(id){
r <- ZillowR::GetComps(zpid = id, count = 25, zws_id = zws)
xll <- XML::xmlToList(r$response)
count <- ncol(xll["comparables", ][[1]])
res <- lapply(1:count, function(iter){
list(
zpid = xll['comparables', ][[1]]['zpid', ][[iter]],
address = xll['comparables', ][[1]]['address', ][[iter]]
)
require("googleAuthR")
require("formatR")
secret_file <- "/My Project 94698-b17961ee0d57.json"
Sys.setenv("GAR_AUTH_FILE"=secret_file)
tk <- googleAuthR::gar_auth_service(
json_file = Sys.getenv("GAR_AUTH_FILE")
)
# fun names- c("f")
obj <- unlist(stringr::str_extract_all(
ls(envir = .GlobalEnv, all.names = TRUE),
pattern = paste0(pat, "+_?"),
simplify = FALSE
))
obj[sapply(obj, function(i) rlang::is_callable(get(i)))]
library(rlang)
# Symbolic objects and functions are callable:
is_callable(quote(foo))
is_callable(base::identity)
# node_poke_car() lets you modify calls without any checking:
lang <- quote(foo(10))
node_poke_car(lang, current_env())
library(rstudioapi)
# insert text at the start of the document
insertText(c(1, 1), "# Hello\n")
# insert text at the end of the document
insertText(Inf, "# Hello\n")
# comment out the first 5 rows
pos <- Map(c, 1:5, 1)
library(rstudioapi)
# insert text at the start of the document
insertText(c(1, 1), "# Hello\n")
# insert text at the end of the document
insertText(Inf, "# Hello\n")
# comment out the first 5 rows
pos <- Map(c, 1:5, 1)
fname <- tempfile(pattern = "wks_", tmpdir = ".", fileext = ".RData")
save.image(file = fname, safe = TRUE)
@bfatemi
bfatemi / rskeybindings.R
Last active August 15, 2019 16:04
Pull up keybinding files
Sys.setenv("KEYB_DIR_PATH" = paste0(Sys.getenv("HOME"), "/.R/rstudio/keybindings"))
wd <- getwd()
if(file.exists(paste0(wd, ".Rprofile")))
rpLines <- tryCatch(
readLines(paste0(wd, "/.Rprofile")),
error = function(c) "## default .Rprofile lines here"
)
set_sample_view <- function(){
selectText <- rstudioapi::primary_selection(rstudioapi::getSourceEditorContext())$text
exprList <- rlang::parse_exprs(selectText)
w <- get("(")
exprDF <- exprList[[which(sapply(exprList, function(x) is.data.frame(w(eval(x)))))]]
invisible(eval(exprDF))
DT <- data.table::as.data.table(w(eval(exprDF)))
vdf <- DT[sample(seq_len(.N), size = min(100, .N), replace = FALSE)]
return(vdf)
}
###
### Scenario Index Table
###
..AND <- function(..., .env = rlang::caller_env()){
dd <- pryr::dots(...)
Reduce("&", lapply(dd, eval, envir = .env))
}
..OR <- function(..., .env = rlang::caller_env()){
dd <- pryr::dots(...)