inst/shiny/plotTwo.R

# output$plotTwo <- renderPlot({
#   plotTwo()
# })

plotTwo <- reactive({
  validate(
    need(input$state, 'Choose a State'),
    need(input$area, 'Choose an Area')
  )
  
  w.use <- w.use()
  
  data.elements <- df[["data.element"]]
  areas.p2 <- df[["area"]]#input$area
  
  if(all(df[["areas"]] %in% areas.p2)){
    areas.p2 <- NA
  }
  legend <- input$legendOn
  
  pctDiff <- input$pctDiff
  valDiff <- input$valDiff
  log.oper <- input$logOperSelect
  
  area.column <- df[["area.column"]]
  year.x.y <- c(input$year_x,input$year_y)

  plotTwo <- compare_two_years(w.use, data.elements, year.x.y, area.column,
    areas = areas.p2, legend=legend, pctDiff = pctDiff, valDiff = valDiff, log.oper = log.oper)
  
  write.csv(x = plotTwo$data, file = "plotTwoYears.csv", row.names = FALSE)
  
  return(plotTwo)
})

output$plotTwo <- renderPlotly({
  plotTwo <- plotTwo()+
    theme(plot.margin=unit(c(10,10,50, 50), "points"))
  
  ggplotly(plotTwo, width = 700, height = 400)
})

output$downloadPlotTwo <- downloadHandler(
  filename = function() { "plotTwoYears.png" },
  content = function(file) {
    ggsave(file, plot = plotTwo(), device = "png")
  }
)

output$downloadPlotTwoPDF <- downloadHandler(
  filename = function() { "plotTwoYears.pdf" },
  content = function(file) {
    ggsave(file, plot = plotTwo(), device = "pdf")
  }
)

output$downloadPlotTwoData <- downloadHandler(
  filename = function() { "plotTwoYears.csv" },
  content = function(file) {
    file.copy("plotTwoYears.csv", file)
  }
)

output$plotTwoCode <- renderPrint({
  
  data.elements <- input$data.elements
  areas.ptC <- df[["area"]]
  legend <- input$legendOn
  pctDiff <- input$pctDiff
  valDiff <- input$valDiff
  log.oper <- input$logOperSelect
  areasOptions <- df[["areas"]]
  
  if(all(areasOptions %in% areas.ptC)){
    areas.ptC <- NA
  } else {
    areas.ptC <- paste0('c("',paste(areas.ptC, collapse = '","'),'")')
  }
  
  area.column <- df[["area.column"]]
  year.x.y <- c(input$year_x,input$year_y)
  
  outText <- paste0(
    'data.elements <- "',data.elements, '"\n',
    "areas <- ", areas.ptC, "\n",
    'area.column <- "', area.column, '"\n',
    "year.x.y <- c(",paste0(year.x.y,collapse = ","),")\n",
    "legend <- ", legend, "\n",
    "pctDiff <- ", pctDiff, "\n",
    "valDiff <- ", valDiff, "\n",
    "log.oper <- ", log.oper, "\n",
    "compare_two_years(w.use, data.elements, year.x.y, area.column, areas, legend, pctDiff, valDiff, log.oper)"
    
  )
  
  HTML(outText)
  
})
USGS-R/wateRuse documentation built on May 9, 2019, 9:35 p.m.