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

时间:2017-04-10 03:29:14

标签: r dynamic shiny leaflet reactive

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, but any help would be greatly appreciated.

Thank you.

1 个答案:

答案 0 :(得分:1)

以下是您尝试实现的最小示例。我用Slope创建了一个具有该值的数据名声。我正在显示与文本输出中的滑块值相乘的输出。

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)

由于我没有您的数据,我无法弄清楚您到底出错的地方。希望这有助于调试代码。