Skip to content

Instantly share code, notes, and snippets.

@fkeck
fkeck / emdash.R
Last active July 8, 2025 09:14
Em dash ecology
library(tidyverse)
library(ggtext)
library(openalexR)
library(httr2)
##### OpenAlex #####
query_url <-
Finding text pattern in files on Linux
https://stackoverflow.com/questions/16956810
grep --include=\*.R -rnw '/home/francois/' -e "tidyverse"
@fkeck
fkeck / rgpt
Created March 28, 2023 18:18
Experiment with ChatGPT to generate R code from natural language
describe_data_df <- function(x) {
res_1 <- paste0("data is a dataframe with ", ncol(x), " colums", ":")
res_2 <- sapply(x, class)
res_2 <- sapply(1:length(res_2), function(x) {
paste0("- Column ", x, " is named '", names(res_2)[x], "' and is of class ", res_2[x], ".")
})
res_2 <- paste(res_2, collapse = "\n")
res <- paste(res_1, res_2, sep = "\n")
@fkeck
fkeck / tidy_specaccum.R
Created December 17, 2021 14:21
tidy vegan specaccum plot for ggplot2
#' Convert vegan specaccum results to tidy format
#'
#' @param x result returned by specaccum
#'
#' @return A data.frame in "long format".
#'
tidy_specaccum <- function(x) {
data.frame(
site = x$sites,
richness = x$richness,
@fkeck
fkeck / tidy_procrustes_ggplot2.R
Created April 25, 2020 17:38
tidy vegan procrustes errors plot for ggplot2
library(vegan)
library(tidyverse)
data(varespec)
vare.dist <- vegdist(wisconsin(varespec))
mds.null <- monoMDS(vare.dist, y = cmdscale(vare.dist))
mds.alt <- monoMDS(vare.dist)
x <- protest(mds.alt, mds.null)
rot <- x$rotation
#' Convert vegan rarecurve results to tidy format
#'
#' @param x a list of rarefy results returned by rarecurve
#' @param sites a vector of site names
#'
#' @return A data.frame in "long format".
#'
tidy_rarecurve <- function(x, sites) {
col_bar_v <- function(col, data.min, data.max){
plot(c(0, 10), c(data.min, data.max), type = "n", bty = "n", xaxt = "n", yaxt = "n", xlab = "", ylab = "")
tk <- pretty(c(data.min, data.max), 7)
tk <- tk[c(-which(tk < data.min), -which(tk > data.max))]
axis(2, las = 1, at = tk, col = NA, col.ticks = 1)
inc <- (data.max - data.min)/ length(col)
for(i in seq(1, length(col))){
rect(0, data.min + inc * (i- 1), 10, data.min + inc * i, col = col[i], border = NA)
}
}
@fkeck
fkeck / jean_graph_parser.R
Created March 23, 2018 19:17
Load jean.dat in R
# Read and parse "jean.dat" from the Stanford GraphBase (C) 1993 Stanford University
library(tidyverse)
library(stringr)
library(igraph)
library(tidygraph)
jean <- readLines("http://ftp.cs.stanford.edu/pub/sgb/jean.dat")
jean <- jean[!str_detect(jean, "^\\*")]
jean_sep <- which(jean == "")