Skip to content

Instantly share code, notes, and snippets.

View alfcrisci's full-sized avatar

Alfonso alfcrisci

View GitHub Profile
viewwhere<-function(object,zoom = 17){
require(leaflet)
if (is.null(object$lon) && is.null(object$lat))
{ stop("Object must be have coordinates lat and lon slot." )};
m = leaflet() %>% addTiles()
m = m %>% setView(object$lon, object$lat, zoom = zoom)
m %>% addPopups(object$lon,object$lat, paste0('The location of the object is here!'))
return(m)
listfiles=gsub(".shp","",Sys.glob("*.shp"))
read_project=function(x)
{
require(maptools)
require(rgdal)
temp=readShapePoly(x)
proj4string(temp)=CRS("+init=epsg:32632")
return(spTransform(temp, CRS("+init=epsg:4326")))
}
@alfcrisci
alfcrisci / read_tweet_json.r
Last active August 29, 2015 14:17
to read in Mearelli style json tweet object
ls_files=Sys.glob("*.json")
tweetdf(jsonfile)
tweetdf=function(jsonfile) {
require(jsonlite)
ex=jsonlite::fromJSON(jsonfile)
library(rvest)
meteo_site <- html("http://www.altostratus.it/Meteo_Site_Italy.htm")
html_nodes(meteo_site, xpath = "//li//a") %>% html_text()
html_nodes(meteo_site, xpath = "//li//a") %>% html_attr("href")
saveRDS(meteo_site,"meteo_sites_italy.rds")
library(tm)
library(tm.plugin.mail)
read_file_string=function(filer) {
con <- file(filer, "r", blocking = FALSE)
singleString <- readLines(con)
singleString <-paste(singleString, sep = " ", collapse = " ")
close(con)
return(singleString)
outersect <- function(x, y) {
sort(c(x[!x%in%y],
y[!y%in%x]))
}
outersect <- function(x, y, ...) {
big.vec <- c(x, y, ...)
duplicates <- big.vec[duplicated(big.vec)]
setdiff(big.vec, unique(duplicates))
}
# from a char vector
extracts_tweet=function(text) {
require(stringr)
res=list()
res$hashtags_text=unique(unlist(str_extract_all(text,'(#[[:alnum:]_]*)')))
res$links_text=unique(unlist(str_extract_all(text,'http[^[:blank:]]+')))
res$mentions_text=unique(unlist(str_extract_all(text,'(@[[:alnum:]_]*)')))
return(res)
}
import io, json,csv,sys,os
from TwitterAPI import TwitterAPI
if len(sys.argv) < 2:
sys.exit('Usage: %s IDcsvfile' % sys.argv[0])
if not os.path.exists(sys.argv[1]):
sys.exit('ERROR: filenot %s was not found!' % sys.argv[1])
#!/usr/bin/python
import io, json
from TwitterAPI import TwitterAPI
CONSUMER_KEY = ''
CONSUMER_SECRET = ''
ACCESS_TOKEN_KEY = ''
ACCESS_TOKEN_SECRET = ''