Skip to content

Instantly share code, notes, and snippets.

View briatte's full-sized avatar

François Briatte briatte

View GitHub Profile
@briatte
briatte / .htaccess
Created January 6, 2019 11:32
Show RSS feeds on a Web page via pure JavaScript, incl. code to bypass Chrome's CORS policy.
# enable CORS policy
# https://gist.github.com/maxparm/3105526
<IfModule mod_rewrite.c>
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Methods: "GET,POST,OPTIONS,DELETE,PUT"
Header add Access-Control-Allow-Headers: "Content-Type"
RewriteEngine on
RewriteBase /
</IfModule>
name keywords
Felicitas Development, Corruption, Housing, Transportation, Religion
Lucile Inequality, Migration, Race and Gender, Community/Neighborhood
Corentin Inequality, Religion, Migration, Poverty, Gender
Charlotte Energy, Environment, Smart Cities, Conflict, Gender
Alice Environment, Gender, Migration, Urban Studies
Miranda Politics, Gender, Human Rights
Margaux Inequality, Urban Studies, Migration, Human Rights
Marina Critical Theory, Social Inequality, Race and Gender
Cosima Race and Gender and Sexuality, Spatial Inequality, Migration
@briatte
briatte / 00_README.md
Last active June 22, 2022 12:15
from networkx (Python) to igraph (R)
@briatte
briatte / get_points.r
Last active July 8, 2021 11:36
R: Create tibbles from Points and Polygons in a Google Maps KML file – not particularly efficient code, but should do the trick; answer to http://stackoverflow.com/q/19497652/635806
#' Read Points out of a KML file.
#'
#' @param x A KML file exported from Google Maps.
#' @param layer The name of the layer to extract from: defaults to \code{"d1"}.
#' @param verbose Whether to report invalid coordinates and/or altitudes below
#' sea level; defaults to \code{TRUE}. See \link{kml_coordinate}.
#' @return A \link[tibble:tibble]{tibble} containing the \code{name},
#' \code{description}, \code{styleUrl} and Point coordinates (\code{longitude},
#' \code{latitude} and \code{altitude}) of each Placemark element contained in
#' the KML file. Placemark elements with no Point coordinates, such as Polygon
@briatte
briatte / gist:ad24bb323d2603dbdd3af35086147c83
Created May 18, 2019 05:43 — forked from conormm/r-to-python-data-wrangling-basics.md
R to Python: Data wrangling with dplyr and pandas
R to python useful data wrangling snippets
The dplyr package in R makes data wrangling significantly easier.
The beauty of dplyr is that, by design, the options available are limited.
Specifically, a set of key verbs form the core of the package.
Using these verbs you can solve a wide range of data problems effectively in a shorter timeframe.
Whilse transitioning to Python I have greatly missed the ease with which I can think through and solve problems using dplyr in R.
The purpose of this document is to demonstrate how to execute the key dplyr verbs when manipulating data using Python (with the pandas package).
dplyr is organised around six key verbs
@briatte
briatte / code.r
Created July 2, 2015 08:23
scrape MEP Twitter accounts listed by Wikipedia 'Freedom of Panorama' campaign 2015: https://meta.wikimedia.org/wiki/Freedom_of_Panorama_in_Europe_in_2015/Contact_your_MEP – note: several accounts are inactive or have been replaced by newer ones
library(dplyr)
library(readr)
library(rvest)
h = html("https://meta.wikimedia.org/wiki/Freedom_of_Panorama_in_Europe_in_2015/Contact_your_MEP")
l = html_nodes(h, xpath = "//a[contains(@href, 'Contact_your_MEP/')]") %>%
html_attr("href") %>%
unique
name keywords
Felicitas Development, Corruption, Housing, Transportation, Religion
Lucile Inequality, Migration, Race and Gender, Community/Neighborhood
Corentin Inequality, Religion, Migration, Poverty, Gender
Charlotte Energy, Environment, Smart Cities, Conflict, Gender
Alice Environment, Gender, Migration, Urban Studies
Miranda Politics, Gender, Human Rights
Margaux Inequality, Urban Studies, Migration, Human Rights
Marina Critical Theory, Social Inequality, Race and Gender
Cosima Race and Gender and Sexuality, Spatial Inequality, Migration
@briatte
briatte / pubmed_ask.r
Last active October 19, 2020 13:14
pubmed scraper
#' Get a PubMed search index
#' @param query a PubMed search string
#' @return the XML declaration of the search
#' @example
#' # Which articles discuss the WHO FCTC?
#' pubmed_ask("FCTC OR 'Framework Convention on Tobacco Control'")
pubmed_ask <- function(query) {
# change spaces to + and single-quotes to URL-friendly %22 in query
query = gsub("'", "%22", gsub(" ", "+", query))
# ==============================================================================
# SESSION_INFO
#
# A script to deal with package dependencies that will
#
# - detach all packages except base ones
# - install its own package dependencies
# - look for session_info.txt and parse it for packages
# - ensure the packages are installed and up to date
#
@briatte
briatte / cleaning_characters.r
Last active August 12, 2020 18:13
a few lines of code to load packages, download and scrape data, convert, sort, etc.
# removes characters from multiple data frame columns
# example removes * , % characters
dw <- data.frame(gsub("\\*|,|%", "", as.matrix(dw)), stringsAsFactors = FALSE)