我在
shiny
+ leaflet
的地图中有可能的着色选项列表,例如
vars <- c("Difference" = "diff",
"Index" = "index",
"City Cargo" = "cid_carga",
"City score" = "carga_mun_score")
从上面的变量中,我创建了一个选择列表,它将用于选择要在地图中着色的所需变量
selectInput("color", "Color", vars, selected = "carga_mun_score")
但是,显然,当我在
leaflet
地图中创建图例标题时,标题仍为变量的实际名称,例如,“cid_cargo”而不是“City Cargo”。
addLegend("bottomleft",pal = pal, values = df[[input$color]], title = input$color,
layerId = "colorLegend")
有没有办法使用我的 alias(在上面的示例中为“city Score”)名称而不是实际的变量名称(“carga_mun_score”),而不使用很长的
if
子句?
简单地说:
addLegend("bottomleft",pal = pal, values = df[[input$color]],
layerId = "colorLegend", title="City Score")
或者将您的选择保存到可以翻译名称的变量中:
varname<-switch(input$color,
"diff" = "Difference",
"index"="Index",
"cid_carga"="City Cargo",
"carga_mun_score"="City score")
然后将该变量传递给您的传单图例:
addLegend("bottomleft",pal = pal, values = df[[input$color]],
layerId = "colorLegend", title=varname)