Skip to content

Instantly share code, notes, and snippets.

@pepijn-devries
pepijn-devries / gif_to_iff.r
Last active February 19, 2019 22:11
GIF to Amiga IFF
## use the magick package to read a GIF file:
library(magick)
## use AmigaFFH to convert into Amiga IFF file,
## make sure to use version 0.2.0 or newer:
library(AmigaFFH)
## Read the GIF animation we created in an earlier post:
earth <- image_read("http://3.bp.blogspot.com/-CCurVbm1ChI/VdC2NrqeeaI/AAAAAAAAx_8/lyO9mFyVhU8/s1600/globe.gif")
@pepijn-devries
pepijn-devries / dither.r
Created March 27, 2018 20:04
dither image
## Library to handle Amiga files
library(AmigaFFH)
## Library to handle png-images
library(png)
## Download an image of the Amiga boing ball:
con <- url("http://www.f1-software.com/images/icons/amiga-boing-ball.png", "rb")
logo <- readBin(con, "raw", 100000L)
close(con)
logo <- as.raster(readPNG(logo))
@pepijn-devries
pepijn-devries / Trick-event-server.r
Created June 30, 2017 20:12
Trick-event-server
# This part of the code runs on a Shiny server and interacts with the
# user interface shown at the client's side.
# For details and tutorials go to http://shiny.rstudio.com
server <- shinyServer(function(input, output, server) {
# Make these character strings 'reactive'. That way
# the output text is automatically update when these
# values are changed.
values <- reactiveValues(state = c("Interface1: Waiting for clicks",
"Interface2: Waiting for clicks"))
# This part of the code generates the user interface show at the client's side.
# For details and tutorials go to http://shiny.rstudio.com
ui <- shinyUI(fluidPage(
# This adds a tiny bit of javascript to the client side's web page.
# It defines a global variable 'negator', which is negated when
# the javascript function negate() is called.
# This can be used to trick Shiny's event observers.
tags$head(tags$script("var negator=1;
function negate() {
@pepijn-devries
pepijn-devries / Fill_SpatialPolygons_with_image.r
Last active January 6, 2017 11:03
An example of how to fill a SpatialPolygons object with a png image
## Required libraries for this example
library(png)
library(sp)
library(maptools)
library(raster)
## A function as a wrapper around 4 of the 5 steps
## described in the main text of the blog....
## As input it requires a SpatialPolygons object
## and the URL to the png image that will be used
@pepijn-devries
pepijn-devries / SegmentSpatialPolygons.r
Created December 18, 2016 21:49
Segement a SpatialPolygons or SpatialPolygonsDataFrame
## package for handling spatial data:
library(sp)
## package that contains simple world map data
library(maptools)
## package for modifying spatial data:
library(rgeos)
## package used for setting up clipping areas
library(raster)
## function that segments the polygons of SpatialPolygons(DataFrame) object
require(pscl)
# the vuong test returns NULL. Lines below will modify the function to return the output table:
# Turn the vuong-function into text:
vuong2 <- deparse(vuong)
# remove the current return statement:
vuong2 <- vuong2[!grepl("return", vuong2)]
# instead of printing the output, return it!:
vuong2 <- gsub("print(out)", "return(out)", vuong2, fixed = T)
# now change the text back into an actual function that can be called:
## This package is used to retrieve data from openstreetmap.org:
require(osmar)
## This pacakge is used to process the spatial data:
require(rgeos)
## This package is used to convert the data from openstreetmap into something workable:
require(maptools)
## function to retrieve a relation from openstreetmap and convert it
## into a SpatialPolygons object:
OSM_relation_to_SP <- function(rel) {
require(raster)
require(KernSmooth)
require(maptools)
## load the simulation results. See the previous blog post
## how this file was created. This data file contains
## an object named 'part.results', which is a list
## of SpatialPointsDataFrame objects.
load("output/out.RData")
## load required packages:
require(sp)
require(raster)
require(ncdf4)
require(ncdf.tools)
require(rgeos)
require(maptools)
###############################################################
## STEP 1: load the water currents and wind data