Search code examples
rshinyreactiver-leaflet

How to perform calculation based on values from a CSV and user input (slider)?


I am trying to perform a simple calculation (multiplication) of values from a CSV and slider input values selected. I think this has to take place in a reactive expression, but I think I am missing some part of how this has to work.

library(shiny)
library(leaflet)
library(rgdal)
library(dplyr)
library(sp)
library(ggplot2)
library(foreign)
library(maptools)
setwd("C:/Users/Jared/Dropbox/InteractiveMap/Data/Shapefiles_CSVs")
test1 <- readOGR("BuffaloParcels2015_VacantTest.shp", "BuffaloParcels2015_VacantTest")
test2 <- spTransform(test1, "+proj=longlat")

test_CSV <- read.csv("BuffaloTestSpreadsheet.csv")

test2@data$OBJECTID <- as.integer(test2@data$OBJECTID)

test2@data <- left_join(test2@data, test_CSV, "OBJECTID")

test2$NewField = test2$DEPTH - test2$FRONT

ui <- bootstrapPage(
  
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("Buff_map", width = "100%", height = "100%"),
  absolutePanel(bottom = 10, left = 10,
                #headerPanel("Test"),
                #sidebarPanel(
                checkboxInput("green", "Green Space", FALSE),
                uiOutput("greenOut"),
                
                checkboxInput("slope", "Slope", TRUE),
                uiOutput("slopeOut"), 
                
                checkboxInput("location", "Location", FALSE),     
                uiOutput("locationOut")
                
      )
)

server <- function(input, output, session) {

    output$greenOut <- renderUI({
    if (input$green == TRUE){
      sliderInput("greenIn", "Modifier", min=1, max=10, value=5)
    }
  })
  
  output$slopeOut <- renderUI({
    if (input$slope == TRUE){
      sliderInput("slopeIn", "Modifier", min=1, max=10, value=5)
    }
  }) 
  
  output$locationOut <- renderUI({
    if (input$location == TRUE){
      sliderInput("LocationIn", "Modifier", min=1, max=10, value=5)
    }
  }) 
  values <- reactiveValues()
    observe({
      values$slopeModder <- isolate ({
      test2$NewFieldReactive = test2$Slope * input$slopeIn
      })
    })
  
  
    pal <- colorNumeric(
    palette = "Greens",
    domain = log1p(test2$NewField) 
  )
  

  
  bounds <- bbox(test2)
  output$Buff_map <- renderLeaflet({
    leaflet(test2) %>%
      addProviderTiles("CartoDB.Positron") %>%
      #fitBounds(bounds[1,1], bounds[2,1], bounds[1,2], bounds[2,2]) %>%
      setView(-78.8, 42.85, zoom = 13) %>%
      addPolygons(color = ~pal(log1p(TOTAV)), stroke=FALSE, 
                  fillOpacity = .8,
                  popup=~paste("<b>Name of Parcel:</b>", ADDNAME, "<br/>", "<b>Depth:</b>", DEPTH,
                               "<b>%Green:</b>", as.integer(Greenspace), "<br/>",
                               "<b>Slope:</b>",as.integer(Slope), "<br/>",
                               "<b>NewField:</b>",as.integer(NewField), "<br/>",
                               #"<b>NewFieldReactive:</b>",as.integer(NewFieldReactive), "<br/>",
                               "<b>Location:</b>", as.integer(Location)))
  })
  
}

shinyApp(ui, server)

Eventually, I'd like to use the calculated values to classify based on color. Above what I've tried so far with no success. Mostly, I think I need some tips on how user inputs and reactive expressions work.


Solution

  • Here is a minimal example of what you are trying to achieve. I have created a data fame with Slope having the value. I am displaying the output that is multiplied by the slider value in the text output.

    library(shiny)
    
      test2 <- data.frame("Slope" = 1:5)
    
    
      ui <- bootstrapPage(
    
        checkboxInput("slope", "Slope", TRUE),
    
        uiOutput("slopeOut"),
    
        textOutput("NewVals")
    
      )
    
      server <- function(input, output, session) {
    
        output$slopeOut <- renderUI({
          if (input$slope == TRUE){
            sliderInput("slopeIn", "Modifier", min=1, max=10, value=5)
          }
        }) 
    
        values <- reactiveValues()
    
        observe({
          values$slopeModder <- test2$Slope * input$slopeIn
        })
    
        output$NewVals <- renderText({values$slopeModder})
    
      }
    
      shinyApp(ui, server)
    

    Since I don't have your data I can't figure out where exactly you are going wrong. Hope this helps to debug your code.