Skip to content

Instantly share code, notes, and snippets.

library(readr)
library(suncalc)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(lubridate)
library(tidyr)
quake10yr <- c("https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2020-02-01&enddate=2021-03-01T0:00:00",
"https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2019-02-01&enddate=2020-02-01",
library(rvest)
target_url <- "https://www.publicservice.govt.nz/resources/proactive-releases/"
webpage <- target_url %>% read_html()
links <- webpage %>% html_nodes("a") %>% html_attr("href")
link_text <- webpage %>% html_nodes("a") %>% html_text()
link_set <- data.frame(links, link_text, stringsAsFactors = FALSE)
if(file.exists("linkcurrent.csv")){
linkcurrent <- read.csv("linkcurrent.csv", stringsAsFactors = FALSE)
newlinks <- link_set[!link_set$links %in% linkcurrent$links,]
} else {
# works through
# https://www.health.govt.nz/news-media/media-releases?page=1 and subsequent pages
library(rvest)
library(dplyr)
library(stringr)
table_fetch <- function(x){
url_loc <- paste0("https://www.health.govt.nz",x)
page_obj <- url_loc %>% read_html()
library(readr)
library(suncalc)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(lubridate)
library(tidyr)
quake10yr <- c("https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2020-02-01&enddate=2021-03-01T0:00:00",
"https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2019-02-01&enddate=2020-02-01",
############ collection
## assumes the working directory is the folder the script is in, and is set to the same for future runs of the file.
# This also assumes you have twitter developer credientals, and have run the create_token() function in the rtweet package
# to authorise r to access Twitter. This stores the credentials in an environmental variable loaded at startup so they are
# not exposed in the script
# as an alternative, if using a Mac or PC, and the httpuv package is installed, you can interactively authorise the script
# at run time
# these packages need to be already installed in order to be loaded and used
library(readr)
library(suncalc)
library(dplyr)
library(ggplot2)
library(ggthemes)
library(lubridate)
quake10yr <- c("https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2020-02-01&enddate=2021-03-01T0:00:00",
"https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2019-02-01&enddate=2020-02-01",
"https://quakesearch.geonet.org.nz/csv?bbox=165.5420,-49.1817,-178.9893,-32.2871&minmag=0&mindepth=0&startdate=2018-01-01&enddate=2019-02-01",
@thoughtfulbloke
thoughtfulbloke / NZMiQ_postivity.R
Created January 10, 2021 23:13
New Zealand MiQ covid positivity rate calculation
library(readxl)
library(readr)
library(dplyr)
library(lubridate)
library(ggplot2)
library(ggthemes)
# arrivals from provisional daily arrivals stats NZ
# postive MiQ data from MoH case demographics
# example code for processing the XML New Zealand elections final data
# party and cadidate vote.
# source: https://electionresults.govt.nz/electionresults_2020/xml/
#
# preceding this code was downloading a local copy of the NZ election
# XML provisional results using wget.
# Before that, since I am on a Mac, was using homebrew to install wget
# then I ran the terminal command
# wget -r -np -k https://electionresults.govt.nz/electionresults_2020/xml/
library(dplyr)
library(ggplot2)
library(ggthemes)
library(maps)
library(mapdata)
combined <- read.csv("combined.csv", stringsAsFactors = FALSE)
top_support <- max(combined$vote)
# keep in mind one polling place can provide for multiple electorates
@thoughtfulbloke
thoughtfulbloke / process_nz_2020_election.R
Created November 4, 2020 02:10
R code showing processing the xml data for the NZ 2020 general election
# example code for processing the XML New Zealand elections provisional data
# party vote only, but is extendable to cadidate vote.
# source: https://electionresults.govt.nz/electionresults_2020_preliminary/xml/
#
# preceding this code was downloading a local copy of the NZ election
# XML provisional results using wget.
# Before that, since I am on a Mac, was using homebrew to install wget
# then I ran the terminal command
# wget -r -np -k https://electionresults.govt.nz/electionresults_2020_preliminary/xml/