2016-07-01 10 views
0

私はggvisを使用していて、UI側でselectInputを使用して次のコードを使用して、ユーザーが塗りと形を指定する変数を選択できるようにしました(inputIdはそれぞれ塗りつぶしと形です)。私は、プロットが、ユーザが選択した場合に、一定の塗りつぶしまたは形状を有する能力を有することを望む。このコードの他の部分は、私がしたい正確にどのように動作しますが、私はif文でオプションを選択すると、次のエラーでアプリがクラッシュ:ユーザー指定の塗りつぶし

Error in eval: could not find function ":=" 

は、私は伝説を抑制するためならば、私は正しい構文を持って知っていますとif/elseステートメントを指定し、fillを定数として指定します(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") 
+0

ggvisステートメントの前にif/elseロジックを2つの変数に展開することができれば、それはうまくいくはずです。 –

+0

@warmoverflowこれはうまくいかなかった。 if else文をggvisの外に置くと、constantオプションが選択されても同じエラーが返されます。 – User247365

+0

ggvisの外でロジックを試してみるかもしれません。また、 'prop'を使う前にロジックを試してみるかもしれません。あなたのロジックに基づいて、マッピング変数である 'as.name(input $ fill)'または '' black "のいずれかの変数を作ります。この新しい変数を、例えば 'prop(" fill "、newvar)'のような 'layer_points'内の' prop'に渡し、 ':='を一緒に避けてください。 – aosmith

答えて

1

私はコメントで述べたように、あなたはif文を使用してpropための変数を作成することができます。これにより、:=の問題は、propで直接定数または変数を使用してバイパスできます。

自動的に凡例が表示されます。 2つの凡例(オーバーラップにつながる)があるときの配置を制御するには、ggvisグラフの名前を付けることができます。これにより、ロジックに基づいて追加されたときと、shapevarfillvarという値に基づいて2番目の凡例を下に移動するために、要素をグラフィックに追加することを参照できます。

リアクティブ機能のコードです。

vis <- reactive({ 
    fillvar = "black" 
    if(input$fill != "All Points Black") { 
     fillvar = as.name(input$fill) 
    } 

    shapevar = "circle" 
    if(input$shape != "All Points Circles") { 
     shapevar = as.name(input$shape) 
    } 

    p1 = filteredData() %>% 
     ggvis(x = xvar, y = yvar) %>% 
     layer_points(size.hover := 200, 
        fillOpacity:= 0.5, fillOpacity.hover := 1, 
        prop("fill", fillvar), 
        prop("shape", shapevar), 
        key := ~ID 
     ) %>% 

     # 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) 


    # Control addition of second legend using if() on p1 object 
    if(fillvar != "black" & shapevar != "circle") { 
     p1 %>% add_legend("shape", properties = legend_props(legend = list(y=300))) 
    } 
    else { 
     p1 
    } 

}) 
+0

これは完璧に機能しました!ありがとうございました – User247365

0

凡例が表示されないようにするため、コードは@aosmithからの入力で機能するようになりました。しかし、私がこれを行うと、記入と形状の伝説はこの投稿アドレスと重複しています。

legends on ggvis graph are overlaping when using tooltip

そのための修正は、一定のデータの可視化オプションが選択されている場合、プロットは消えます凡例に追加することです。この問題を解決するために新しい質問を投稿します。

更新:以下の答えは元の問題を解決しますが、@ aosmithの回答は最初の問題を修正した後に発生した2番目の問題を修正しました。

オリジナルの問題は修正されていますが、重複する凡例(@ aosmithの回答で修正)を含むコードは次のとおりです。

vis <- reactive({ 

    # Allows for points to be consistent if the user desires 
    if (input$fill == "All Points Black") { 
    fillvar = "black"} 
    else { 
    fillvar <- as.symbol(input$fill)} 

    if (input$shape == "All Points Circles") { 
    shapevar = "circle"} 
    else { 
    shapevar <- as.symbol(input$shape)} 

#Plot Data with Visualization Customization 
xvar <- prop("x", as.symbol(input$x)) 
yvar <- prop("y", as.symbol(input$y)) 

filteredData() %>% 
    ggvis(x = xvar, y = yvar) %>% 
    layer_points(size.hover := 200, 
       fillOpacity:= 0.5, fillOpacity.hover := 1, 
       prop("fill", fillvar), 
       prop("shape", shapevar), 
       key := ~Shot_ID 
) %>% 

    # Adds the previously defined tool_tip my_tooltip 
    add_tooltip(my_tooltip, "hover") %>% 

    # Specifies the size of the plot 
    set_options(width = 800, height = 450, duration = 0) 
}) 

#Actually plots the data 
vis %>% bind_shiny("plot1") 
関連する問題