简体   繁体   English

R Shiny:如何根据CSV和用户输入(滑块)的值执行计算?

[英]R Shiny: 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. 我正在尝试从CSV和选定的滑块输入值中进行值的简单计算(乘法)。 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. 谢谢。

Here is a minimal example of what you are trying to achieve. 这是您要达到的目标的最小示例。 I have created a data fame with Slope having the value. 我创建了具有价值的Slope数据名人堂。 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. 希望这有助于调试您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM