簡體   English   中英

R Shiny:如何根據CSV和用戶輸入(滑塊)的值執行計算?

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

我正在嘗試從CSV和選定的滑塊輸入值中進行值的簡單計算(乘法)。 我認為這必須以一種反應性的表達方式進行,但是我想我缺少這種工作方式的某些部分。

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)

最終,我想使用計算出的值基於顏色進行分類。 到目前為止,我還沒有嘗試過任何成功的方法。 通常,我想我需要一些有關用戶輸入和反應式表達式如何工作的技巧,但是任何幫助將不勝感激。

謝謝。

這是您要達到的目標的最小示例。 我創建了具有價值的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)

由於我沒有您的數據,因此我無法弄清楚您到底在哪里出錯。 希望這有助於調試您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM