Skip to content

Instantly share code, notes, and snippets.

View sharlagelfand's full-sized avatar
💅
#rstats

Sharla Gelfand sharlagelfand

💅
#rstats
View GitHub Profile
@sharlagelfand
sharlagelfand / r_to_redshift
Created February 16, 2018 18:54
Inserting data into redshift from R
library(glue)
library(RPostgreSQL)
library(DBI)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv,
host=Sys.getenv("JDBCURL"),
port = Sys.getenv("PORT"),
dbname = Sys.getenv("DBNAME"),
user = Sys.getenv("USER"),
---
title: "Plotly and floating TOC"
output:
html_document:
toc: true
toc_float: true
---
# h1
``` r
# tidyeval for multiple arguments that can take multiple variables
library(dplyr)
# in the case where the verbs' arguments are ... e.g. group_by(), select(), use !!! within the function to expand the variables back out
# no enquo() or enquos() is needed because you will pass in a vars() call
group_select <- function(df, group_vars, select_vars){
df %>%
not_equal <- function(a, b){
ifelse(a != b | is.na(a) | is.na(b),
ifelse(is.na(a) & is.na(b),
NA,
TRUE),
FALSE)
}
not_equal("A", "A")
#> [1] FALSE
@sharlagelfand
sharlagelfand / shiny_modules_reactlog_not_independent
Created July 12, 2019 03:55
shiny modules + reactlog + not independent
options(shiny.reactlog = TRUE)
library(shiny)
library(reactlog)
mod_iris_ui <- function(id) {
ns <- NS(id)
tagList(
fluidRow(
column(
@sharlagelfand
sharlagelfand / testthat_analysis
Last active August 15, 2019 13:11
using testthat on data cleaning/derivation steps to ensure that they work as expected
# Deriving the "overall working status" of someone's employment positions
# in Ontario, where it is the highest of Full Time > Part Time > Casual.
# e.g. if someone has a full time and a part time position, their overall
# working status is full time. If they have two part time positions, it's part time.
library(dplyr)
sample_df <- tibble::tribble(
~id, ~working_status, ~province,
1, "Full Time", "Ontario",
1, "Casual", "Ontario",
@sharlagelfand
sharlagelfand / listcol_vs_tibble_col
Created September 23, 2019 22:38
list-col vs tibble-col?
library(tibble)
library(tidyr)
x <- tibble(a = 1,
b = 1:2,
c = 3:4)
x
#> # A tibble: 2 x 3
#> a b c
library(httr)
library(dplyr)
resp <- GET("https://summary-api.datamermaid.org/v1/sites/")
content <- jsonlite::fromJSON(content(resp, "text", encoding = "UTF-8"), simplifyDataFrame = TRUE)
as_tibble(content[["features"]])
library(ggplot2)
plot_diamonds <- function() {
p <- ggplot(head(diamonds), aes(x = cut, y = price)) +
geom_jitter()
res <- list(
name = "diamonds",
p = p
)
class(res) <- "diamonds"
sharla_diff <- function(df, expected_df) {
data_as_expected <- dplyr::all_equal(expected_df, df)
if (!isTRUE(data_as_expected)) {
data_diffs <- janitor::compare_df_cols(expected_df, df)
cols_mismatch <- dplyr::filter(data_diffs, is.na(expected_df) | is.na(df))
extra_cols <- cols_mismatch %>%
dplyr::filter(is.na(expected_df)) %>%