我正在使用ggvis并在UI端使用selectInput进行以下代码,允许用户选择哪个变量指示填充和形状(inputId分别是填充和形状) . 我希望绘图能够在用户选择时具有恒定的填充或形状 . 这段代码的else部分正是我想要的,但当我在if语句中选择该选项时,应用程序崩溃时出现以下错误:
Error in eval: could not find function ":="
我知道我的语法是正确的,因为如果我抑制了图例和if / else语句并将填充指定为常量(fill:=“black”),它就会按照我想要的方式工作 .
任何帮助,将不胜感激 .
vis <- reactive({
fillvar <- prop("fill", as.symbol(input$fill))
shapevar <- prop("shape", as.symbol(input$shape))
filteredData() %>%
ggvis(x = xvar, y = yvar) %>%
layer_points(size.hover := 200,
fillOpacity:= 0.5, fillOpacity.hover := 1,
# Allows for points to be consistent if the user desires
if (input$fill == "All Points Black") {
fill := "black"}
else {
fill = fillvar}
,
if (input$shape == "All Points Circles") {
shape := "circle"}
else {
shape = shapevar}
,
key := ~ID
) %>%
# Adds legends to the Plot in designated locations
add_legend("fill", title = as.character(fillvar)) %>%
add_legend("shape", title = as.character(shapevar), properties = legend_props(legend = list(y=300))) %>%
# Adds the previously defined tool_tip my_tooltip
add_tooltip(my_tooltip, "hover") %>%
# Specifies the size of the plot
set_options(width = 800, height = 400, duration = 0)
})
#Actually plots the data
vis %>% bind_shiny("plot1")
2 回答
正如我在评论中提到的,您可以使用
if
语句为prop
创建变量 . 这允许您通过在prop
中直接使用常量或变量来绕过:=
的问题 .你会自动获得传说 . 要在有两个图例(这将导致重叠)时控制放置,您可以命名
ggvis
图形 . 这允许您引用向图形添加元素,以便仅在基于逻辑和shapevar
和fillvar
值添加第二个图例时将其向下移动 .这是反应函数的代码 .
现在代码可以使用来自@aosmith的输入,因为我想要它是否被抑制 . 但是,当我这样做时,填充和形状的图例重叠,因为这篇文章解决了 .
legends on ggvis graph are overlaping when using tooltip
解决方法是添加一个图例,如果选择了常量数据可视化选项,则会使图形消失 . 我将发布一个新问题,尝试解决此问题 .
更新:下面的答案解决了原始问题,但@ aosmith的答案修正了第二个问题,该问题也是在纠正第一个问题后出现的 .
我的代码带有更正的原始问题,但包含重叠的图例(用@ aosmith的答案更正)如下 .