Created
April 28, 2015 10:53
-
-
Save scuerda/d87f9328f4b3deacf869 to your computer and use it in GitHub Desktop.
Ckan Helper File
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require(RCurl) | |
require(rjson) | |
require(httr) | |
require(yaml) | |
# Load api key | |
ckanapi <- getOption('CkanAPIKey') | |
# slugger function for generating title | |
slugger <- function(name) { | |
temp <- tolower(name) | |
title <- gsub(" ", "-", temp) | |
return(title) | |
} | |
# makeExtras | |
makeExtras <- function(ds, dims, dom="", subdom="", desc, longdesc, numerator, denominator, tech, default, dssource, geography) { | |
newExtras <- list() | |
dimsToAdd <- names(ds)[dims] | |
newExtras[[1]] <- list(key="Dimensions", value=paste(dimsToAdd, collapse=",")) | |
newExtras[[2]] <- list(key="Domain", value=dom) | |
newExtras[[3]] <- list(key="Subdomain", value=subdom) | |
newExtras[[4]] <- list(key="Description", value=desc) | |
newExtras[[5]] <- list(key="Full Description", value=longdesc) | |
newExtras[[6]] <- list(key="Numerator", value=numerator) | |
newExtras[[7]] <- list(key="Denominator", value=denominator) | |
newExtras[[8]] <- list(key="Technical Notes", value=tech) | |
newExtras[[9]] <- list(key="Default", value=toJSON(default)) | |
newExtras[[10]] <- list(key="Source", value=dssource) | |
newExtras[[11]] <- list(key="Geography", value=geography) | |
# Iterate over the dataset and generate an extras field that contains the unique factor levels for each column | |
# Note that we need to keep the starting point of this (11) the same as the number of extras we add in the lines above | |
for(i in 1:length(dimsToAdd)) { | |
results <- as.vector(na.omit(unique(eval(substitute(ds$a, list(a = dimsToAdd[i])))))) | |
if (length(results) > 1) { | |
resultsString <- paste(results, collapse=",") | |
} else { | |
resultsString <- results | |
} | |
newExtras[[i+11]] <- list(key=dimsToAdd[i], value = resultsString) | |
} | |
return(newExtras) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source("ckanBasics.R") | |
datasetFileName = "../raw/file.csv" | |
# Specify col types | |
cc <- c("character", "character", "numeric", "character", "character", "character") | |
dataset <- read.csv(datasetFileName, stringsAsFactors=FALSE, check.names=FALSE) | |
# Load metadata file | |
theYaml = yaml.load_file("../_metadata//file.yml") | |
# Setup Variables | |
dimensions = c(3:5) | |
# Grab values from YAML | |
domain = theYaml$Dataset$Domain | |
subdomain = theYaml$Dataset$Subdomain | |
datasetName = theYaml$Dataset$Name | |
datasetTitle = slugger(theYaml$Dataset$Name) | |
datasetDescription = theYaml$Dataset$Description | |
datasetLongDescription = theYaml$Dataset$'Full Description' | |
datasetNumerator = theYaml$Dataset$Numerator | |
datasetDenominator = theYaml$Dataset$Denominator | |
datasetTechNotes = theYaml$Dataset$'Technical Notes' | |
datasetDefault = theYaml$Dataset$Default | |
datasetSource = theYaml$Dataset$Source | |
datasetGeography = theYaml$Dataset$Geography | |
# Using the function in the helper file, create the extra metadata fields | |
datasetExtras <- makeExtras(dataset, dimensions, domain, subdomain, | |
datasetDescription, datasetLongDescription, | |
datasetNumerator, datasetDenominator, datasetTechNotes, | |
datasetDefault, datasetSource, datasetGeography) | |
datasetMetadataJSON <- toJSON(list(name=datasetName, title=datasetTitle, | |
maintainer="Sasha Cuerda", | |
maintainer_email="[email protected]", | |
owner_org="org", extras=datasetExtras)) | |
### Upload to Production ### | |
## Create organization if doesn't exist | |
# POST(orgCreate, | |
# config=add_headers(Authorization = ckanapi, "Content-Type"="charset=utf-8"), | |
# body=orgMetaDataJSON) | |
# Create Dataset | |
# POST(create, | |
# config=add_headers(Authorization = ckanapi, "Content-Type"="charset=utf-8"), | |
# body=datasetMetadataJSON) | |
# Update Dataset | |
POST(updatePackage, | |
config=add_headers(Authorization = ckanapi, "Content-Type"="charset=utf-8"), | |
body=datasetMetadataJSON) | |
# Upload Data | |
postForm(uploadResource, | |
.params = list(package_id=slugger(datasetName), | |
format="CSV", | |
name=slugger(datasetName), | |
upload = fileUpload(filename=(datasetFileName))), | |
.opts = list(httpheader = c(Authorization = ckanapi)) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment