Skip to content

Instantly share code, notes, and snippets.

@AlexArcPy
Last active August 12, 2016 16:21
Show Gist options
  • Save AlexArcPy/ef32de1032f3582ed682e641aceff215 to your computer and use it in GitHub Desktop.
Save AlexArcPy/ef32de1032f3582ed682e641aceff215 to your computer and use it in GitHub Desktop.
arcgisbinding R package for running R scripts as ArcGIS geoprocessing script tools
options(scipen=999)
tool_exec <- function(in_param, out_param)
{
#load feature class into dataset
#can use 'C:\\Program Files (x86)\\ArcGIS\\Desktop10.4\\TemplateData\\TemplateData.gdb\\USA\\states'
data = arc.open(path = in_param[[1]])
#get subregion name from user
sub_region = in_param[[2]]
#load feature class into R data frame
df = arc.select(data)
#filter df based on the sub region provided
filtered_df = df[df$SUB_REGION == sub_region,]
max_val = max(filtered_df$POP2000) + 10000000
# write output to a PDF file
output.pdf = out_param[[1]]
result = tryCatch({
#export to .pdf file
pdf(output.pdf)
#build a barplot
barplot(height = filtered_df$POP2000, horiz = FALSE,
ylab = 'Population, mln', names.arg = filtered_df$STATE_NAME,
beside = TRUE, col = '#00ffcc',axes = TRUE,
ylim = c(0,max_val), las = 3, cex.names=0.5, cex.axis = 0.75 )
}, finally = {
# turn off plotting output
dev.off()
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment