Skip to content

Instantly share code, notes, and snippets.

View maurolepore's full-sized avatar

Mauro Lepore maurolepore

View GitHub Profile
# A custom error with base
f <- function() {
  message <- paste(c("Oh no", "Messed up", "Need a hint?"), collapse = "\n")
  # Construct a custom class via `errorCondition()`
  stop(errorCondition(message, class = "my_error"))
}

# How it prints
f()
library(shiny)

ui <- fluidPage(
  textInput("name", "What's your name?"),
  textOutput("greeting")
)

server <- function(input, output, session) {
 output$greeting &lt;- renderText(paste("Hello", input$name))
#' Calculate the nevitable Policy Response (IPR)
#'
#' @param plan_x What is this?
#' @param scen_start  What is this?
#' @param scen_end  What is this?
#' @param year_x  What is this?
#' @param year_start  What is this?
#' @param year_end  What is this?
#'
library(dplyr, warn.conflicts = FALSE)

scenarioSelector::scenarios %>%
  select(variable, unit, technology) %>%
  distinct()
#> # A tibble: 29 x 3
#>    variable        unit                                         technology
#>    <chr>           <chr>                                        <chr>     
#>  1 emission_factor tonnes of CO2 per tonne of cement            <NA>      
# Scenario data source:
# https://github.com/2DegreesInvesting/CapitalMarketsPlatform/blob/master/data/scenario_data.rds
# commit: a72c75b

library(tidyverse)
set.seed(123)

raw <- readr::read_rds("data-raw/scenario_data.rds")
# if (interactive()) glimpse(raw)
library(dplyr, warn.conflicts = FALSE)
devtools::load_all(quiet = TRUE)

# Helper?
order_lines <- function(data, order) {
  data %>% 
    mutate(metric = factor(.data$metric, levels = order)) %>% 
    arrange(.data$metric)
}
library(testthat)

# R/utils.R
source_all <- function(paths, ...) {
  lapply(paths, source, ...)
  invisible(paths)
}

# test/testthat/test-utils.R
library(ggplot2, warn.conflicts = FALSE)

ggplot(iris) + 
  geom_histogram(aes(Sepal.Length, fill = Species), bins = 15, alpha = 0.5)

Created on 2021-05-03 by the reprex package (v2.0.0)

library(dplyr, warn.conflicts = FALSE)
library(testthat, warn.conflicts = FALSE)
library(r2dii.match)
library(r2dii.analysis)

.production <- c(1, 10)
.year <- 2022
.company <- "toyota motor corp"
.sector <- "automotive"