library(tools) library(shiny) library(rgdal) library(spsurvey) library(RColorBrewer) library(latticeExtra) source("stratumdesign.R") source("grts2.R") # Define server logic shinyServer(function(input, output, session) { observe({ x <- input$controller }) mySHPdata <- reactive({ x<- 0 updateNumericInput(session, "pts", value = x) updateNumericInput(session, "oversamppts", value = x) inFile <- input$file1 if (is.null(inFile)) return(NULL) # unzipping upload is a sys call and depends on platform switch(Sys.info()[['sysname']], Windows = { origdir=getwd() setwd(dirname(inFile$datapath)) system(sprintf(" cd %s", dirname(inFile$datapath))) print(getwd()) system(sprintf("7z e -aoa %s", inFile$datapath)) setwd(origdir) print(getwd()) }, Linux = { system(sprintf("unzip -tq %s", inFile$datapath)) }) shapename <- list.files(dirname(inFile$datapath), pattern="\\.shp$") shapename <- file_path_sans_ext(shapename, compression=FALSE) print(shapename) require(rgdal) return ( readOGR(dirname(inFile$datapath), shapename) ) }) output$plot <- renderPlot({ # a variable we can play with, vs the true reactive? theSHP<-mySHPdata() if(is.null(theSHP)) return(NULL) print(proj4string(theSHP)) # ok so whereabouts are we limitspoly=bbox(theSHP) minx=limitspoly[1] miny=limitspoly[2] maxx=limitspoly[3] maxy=limitspoly[4] #factor strata values print(theSHP$n) print(theSHP$fid) theSHP$n=as.factor(theSHP$n) layer1<-spplot(theSHP, c("n"), xlim=c(minx,maxx), ylim=c(miny,maxy), col.regions=brewer.pal(nlevels(theSHP$n), "Accent")) if (input$pts >= 1) { # if(1==1){ gimmie<-plotpoints() layer2<-spplot(gimmie, c("n"), col.regions="black") print(layer1 + layer2) } else print(layer1) }) # make our sample design and invoke grts from spsurvey grtsPointData <- reactive({ if(input$pts == 0) return(NULL) # non-reactive shp we can manipulate theSHP<-mySHPdata() if(is.null(theSHP)) return(NULL) print(length(theSHP$strata)) numstrata<-unique(theSHP$strata) print("numstrata is ") print(numstrata) # make dummy first entry for list we will concat to wholedesign=list(NULL) for (i in 1:length(numstrata)) { print(numstrata[i]) temp<-list(stratumdesign(numstrata[i], isolate(input$pts), isolate(input$oversamppts))) names(temp) <- numstrata[i] wholedesign<-c(wholedesign, temp) } wholedesign[[1]]<-NULL print(wholedesign) # diagnostic save(wholedesign, file="wholedesign") # create numbered list of entries in shapefile howmany<-length(theSHP) ids<-1:howmany dat<-theSHP@data dat$ID<-ids theSHP@data<-dat save(theSHP, file="shapedata") writeOGR(theSHP, ".", "rgdaltest2", driver="ESRI Shapefile", overwrite_layer=TRUE) test.attframe<- read.dbf("rgdaltest2") result <- grts2(design=as.list(wholedesign), src.frame="sp.object", sp.object=theSHP, type.frame="area", stratum="strata", id="ID", att.frame=test.attframe, prjfilename="rgdaltest2", out.shape="grtstest") # x<- 0 # updateNumericInput(session, "pts", value = x) # updateNumericInput(session, "oversamppts", value = x) save(result, file="result") writeOGR(result, ".", "results", driver="ESRI Shapefile", overwrite_layer=TRUE) print("DONE creating sample locations!") file.copy("rgdaltest2.prj", "results.prj", overwrite=TRUE) # create zip fle in case user wants the points types<-list.files(pattern="results") types_as_string = as.character(types[[1]]) if (length(types) > 1) for (j in 2:length(types)) types_as_string = paste(types_as_string," ",as.character(types[[j]]),sep="") # unzipping upload is a sys call and depends on platform switch(Sys.info()[['sysname']], Windows = { # origdir=getwd() # setwd(dirname(inFile$datapath)) # system(sprintf(" cd %s", dirname(inFile$datapath))) # print(getwd()) system(sprintf("7z a -tzip results.zip %s", types_as_string)) # setwd(origdir) # print(getwd()) }, Linux = { allshp<-list.files(pattern="results") system(sprintf("zip results %s %s %s", types_as_string)) }) return(result) }) output$pointdata <- renderTable({ temp<- as.data.frame(grtsPointData()) temp2<- temp[,c("siteID", "xcoord", "ycoord", "stratum")] }) plotpoints <- reactive({ return(grtsPointData()) }) output$downloadData <- downloadHandler( filename = function() { paste('result-', Sys.Date(), '.zip') }, content = function(file) { file.copy("results.zip", file) } ) })