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)
因为我没有你的数据,所以我无法弄清楚你到底哪里出了问题。希望这有助于调试您的代码。
我正在尝试对所选的 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)
因为我没有你的数据,所以我无法弄清楚你到底哪里出了问题。希望这有助于调试您的代码。