[英]dynamic colored polygons in leaflet shiny r
I am attempting to create an r shiny leaflet app that can dynamically color shade polygons with a pal spectrum depending on user input. 我正在尝试创建一个r闪亮的传单应用程序,可以根据用户输入动态着色多边形与pal光谱。 Note I am not just coloring the polygons, I also want to use a color range to illustrate a range of values across a variable. 注意我不只是着色多边形,我还想使用颜色范围来说明变量中的一系列值。 (Hence the need for the pal spectrum) (因此需要pal谱)
The code below works fine - but the pal and fillColor inputs are not dynamic here. 下面的代码工作正常 - 但pal和fillColor输入在这里不是动态的。
output$mymap <- renderLeaflet({
map <- readShapeSpatial(paste(input$state,"Zip.shp",sep=""))
pal <- colorNumeric(
palette = "YlGnBu",
domain = map$college)
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data=map,
fillColor=~pal(college),
fillOpacity = 0.7,
weight = 1,
smoothFactor = 0.2)
})}
But the version below fails. 但是下面的版本失败了。 It creates a "no method for coercing this S4 class to a vector" Error. 它创建了“无法将此S4类强制转换为向量”的错误。 Why does this error occur and how can I fix this problem? 为什么会出现此错误,如何解决此问题?
Code that fails: 代码失败:
output$mymap <- renderLeaflet({
map <- readShapeSpatial(paste(input$state,"Zip.shp",sep=""))
pal <- colorNumeric(
palette = "YlGnBu",
domain = paste(map,"input$fill",sep=""))
leaflet() %>%
addProviderTiles("CartoDB.Positron") %>%
addPolygons(data=map,
fillColor=~pal(input$fill),
fillOpacity = 0.7,
weight = 1,
smoothFactor = 0.2)
Why can't I use the dynamic shiny input of input$fill to set the pal spectrum and fillColor for the Polygons? 为什么我不能使用输入$ fill的动态光泽输入来设置多边形的pal光谱和fillColor? Any possible work arounds? 任何可能的工作?
Thanks for any help! 谢谢你的帮助!
I think your case can be solved by this suggestion . 我认为你的案子可以通过这个建议来解决。 Hence you should use fillColor = ~pal()(input$fill)
. 因此你应该使用fillColor = ~pal()(input$fill)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.