Skip to content

Instantly share code, notes, and snippets.

View simonrolph's full-sized avatar

Simon Rolph simonrolph

View GitHub Profile
@simonrolph
simonrolph / _targets.R
Created April 8, 2024 15:14
Static branching targets pipeline
library(targets)
library(tarchetypes)
#setup
#here a user sets out what parameters they want to generate data using
#each parameter as a separate dataframe
par1 <- data.frame(par1_col = c(1, 2))
par2 <- data.frame(par2_col= c(10, 20))
@simonrolph
simonrolph / _targets.R
Created August 21, 2023 13:55
R targets workflow with static branches per species and combined trend analysis
# This is an example r {targets} workflow it does the following:
#
# (0. (Generate some example data)
# 1. Statically branch to create data subsets for each species
# 2. Fit a linear model to each of the species subset
# 3. Produce and save a plot of each species trend (with a line from the linear model) using tar_map()
# 4. Produce and save a plot with all of the species combined and their linear models using tar_combine()
#this is combined into a single file for sharability but normally you'd have the R functions in another .R file
---
title: "MAMBO sim framework tests"
author: "Simon Rolph"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
@simonrolph
simonrolph / NBN_data_provider_summary.R
Created June 1, 2023 10:56
An R script to generate summary data about data providers on the NBN Atlas using the faceting functionality in the occurrence API endpoint
library(httr)
library(jsonlite)
library(dplyr)
# Get a list of data providers
response <- GET("https://registry.nbnatlas.org/ws/dataProvider")
content <- content(response, "text")
data_providers <- fromJSON(content) # Parse the JSON response into a dataframe
#define query
@simonrolph
simonrolph / generate_ro_card.ipynb
Created May 9, 2023 08:31
generate_ro_card.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
// Load a landsat image and select three bands.
var landsat = ee.Image('LANDSAT/LC08/C02/T1_TOA/LC08_123032_20140515')
.select(['B4', 'B3', 'B2']);
// Create a geometry representing an export region.
var geometry = ee.Geometry.Rectangle([-4.477366, 56.740975, -2.800270, 57.410659 ]);
Map.addLayer(geometry, {palette: 'FF0000'}, "Study Area");
// predictor variables
# this script provides 1km gridded data on how accessible the grid square is by the proportion of 100m grid cells within it are accessible by footpath.
#rationale:
#OSM data is really rich and detailed but sometimes you need gridded summaries in raster format. This example shows to generate a raster with the proportional coverage within 1km grid squares containing footpaths. And a second example with the save proportional coverage but for car parks as an indication of how much parking there is in a 1km grid cell.
#These are metrics/indices and act as coarser approximations
#load packages
library(osmextract)
library(magrittr) # for %>%
@simonrolph
simonrolph / percent_progress.R
Created September 21, 2022 08:47
Percentage progress in a loop for R
n <- 1004
for(i in 1:n){
Sys.sleep(0.2
cat("\r",round(i/n*100,2),"%")
}
@simonrolph
simonrolph / lonlat_to_1km_grid.R
Created September 15, 2022 15:12
Reproject longitude and latitude to OSGB grid using {sf} and produce a column referring to unique 1km grid squares
#load pacakges
library(sf)
library(magrittr) #for the pipe
#create a test dataframe
test <- expand.grid(latitude = 50:52,longitude = -1:1)
#load as sf, transform, round to 1km
test2 <- st_as_sf(test,coords =c("longitude","latitude"),crs = 4326) %>%
st_transform(27700) %>%
@simonrolph
simonrolph / attempt_to_load_all_R_packages.R
Last active October 27, 2022 14:57
Attempt to load all R packages
packages <- as.data.frame(installed.packages())
for (i in 1:length(packages$Package)){
try({
library(packages$Package[i], character.only = TRUE)
})
print(packages$Package[i])
}