This file contains hidden or 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(testthat) | |
| library(dplyr, warn.conflicts = FALSE) | |
| # expect_equal() returns silently if they're the same | |
| expect_equal(iris, iris) | |
| # but with an error if they're not | |
| expect_equal(iris, mtcars) | |
| #> Error: `iris` not equal to `mtcars`. | |
| #> Names: 5 string mismatches |
This file contains hidden or 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
| divide_by_two <- function(x, call. = FALSE) { | |
| if(!is.numeric(x)) { | |
| stop("IT'S NOT NUMERIC", call. = call.) | |
| } else { | |
| x/2 | |
| } | |
| } | |
| divide_by_two(2) | |
| #> [1] 1 |
This file contains hidden or 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
| 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)) %>% |
This file contains hidden or 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(ggplot2) | |
| plot_diamonds <- function() { | |
| p <- ggplot(head(diamonds), aes(x = cut, y = price)) + | |
| geom_jitter() | |
| res <- list( | |
| name = "diamonds", | |
| p = p | |
| ) | |
| class(res) <- "diamonds" |
This file contains hidden or 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(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"]]) |
This file contains hidden or 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(tibble) | |
| library(tidyr) | |
| x <- tibble(a = 1, | |
| b = 1:2, | |
| c = 3:4) | |
| x | |
| #> # A tibble: 2 x 3 | |
| #> a b c |
This file contains hidden or 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
| # 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", |
This file contains hidden or 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
| options(shiny.reactlog = TRUE) | |
| library(shiny) | |
| library(reactlog) | |
| mod_iris_ui <- function(id) { | |
| ns <- NS(id) | |
| tagList( | |
| fluidRow( | |
| column( |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| ``` 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 %>% |