Skip to content

Instantly share code, notes, and snippets.

@PaulC91
PaulC91 / sf_to_duckdb.R
Created February 18, 2024 10:16
sf to duckdb via gpkg and back again
library(sf)
library(DBI)
library(duckdb)
# sf object
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
nc_crs <- st_crs(nc)
# write it to local gpkg
path_gpkg <- here::here("local.gpkg")
library(ggplot2)

scale_fill_msf <- function(...) {
  ggplot2:::manual_scale(
    "fill",
    values = c(
      "MSF" = "#D02327",
      "OCA" = "#EF5423",
 "OCB" = "#FFC70A",
@PaulC91
PaulC91 / launch_module.R
Last active November 25, 2021 13:32
Example of code to only launch a shiny module when its tab is selected for the first time
tab2_init <- TRUE
observeEvent(input$tabs, { # input$tabs is the menu input
if (all(input$tabs == "tab2", tab2_init)) {
# change to false so it won't run again
tab2_init <<- FALSE
# call server module
tab2Server("id", ...)
}
})
@PaulC91
PaulC91 / app.R
Last active February 24, 2021 04:07
shinyWidgets::pickerInput with images
library(shiny)
library(magrittr)
# images for picker input stored in www/img/ from the root app directory
imgs <- fs::dir_ls("www/img") %>% stringr::str_remove("www/") # remove www/ from filepath
img_name <- fs::path_file(imgs) %>% fs::path_ext_remove() # get image name without extension
select_choice_img <- function(img, text) {
shiny::HTML(paste(
tags$img(src=img, width=30, height=22),
@PaulC91
PaulC91 / shiny_ppt_builder.R
Last active April 22, 2023 05:28
Example of using shiny to allow a user to build several plots interactively and iteratively add them as slides of a ppt deck download
library(shiny)
library(magrittr)
library(ggplot2)
library(officer)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("obs", "Number of observations:", min = 10, max = 500, value = 100),
actionButton("add_plot", "Add to ppt"),
@PaulC91
PaulC91 / addCircleLegend.R
Created March 31, 2020 11:18
Add a custom circle legend to leaflet map in R
#' Add custom circle legend to leaflet map
#'
#' To be used alongside \code{leaflet::addCircleMarkers}
#'
#' @param map a leaflet map
#' @param title title of the legend
#' @param range vector of numeric values you want to scale the legend to (same vector used with addCircleMarkers)
#' @param scaling_fun the scaling function used with addCircleMarkers to scale circle radii appropriately for leaflet
#' @param color stroke color
#' @param weight stroke width in pixels
@PaulC91
PaulC91 / leaflet_labels_on_top.R
Created January 21, 2020 23:23
render place tile labels above polygons in R leaflet
library(leaflet)
world <- rnaturalearth::ne_countries(returnclass = "sf")
pal <- colorFactor("Dark2", levels = unique(world$continent))
leaflet(world) %>%
setView(10, 15, zoom = 3) %>%
addMapPane(name = "polygons", zIndex = 410) %>%
addMapPane(name = "maplabels", zIndex = 420) %>% # higher zIndex rendered on top
@PaulC91
PaulC91 / leaflet_labels_options.R
Created January 21, 2020 11:04
different options for rendering tiles in R leaflet
# OSM tile with no labels
osm_nolabels <- "https://tiles.wmflabs.org/osm-no-labels/{z}/{x}/{y}.png"
# OSM humanitarian tile (just for ref)
osm_hot <- "http://a.tile.openstreetmap.fr/hot/{z}/{x}/{y}.png"
# if you a mapping a choropleth with polygons, this will render place labels above the polygons
leaflet() %>%
addMapPane(name = "polygons", zIndex = 410) %>%
addMapPane(name = "maplabels", zIndex = 420) %>% # higher zIndex rendered on top
@PaulC91
PaulC91 / install_r.sh
Last active September 30, 2021 12:41
install r and tidyverse packages and rstudio server on ubuntu 18
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu bionic-cran35/'
sudo apt update
sudo apt install r-base
sudo apt install libcurl4-openssl-dev libssl-dev libxml2-dev
Sys.setlocale("LC_TIME", "fr_FR.UTF-8")