2017-01-14 4 views
0

shinyggplot2のインタラクティブプロットを作成したいと思います。私はgeom_pointと他のgeomsでこれを成功させることができました。明らかにxyの軸を持っています。 geom_barのようなものを使用すると、yという変数がないため、これは難しくなります。Rシャイニープロットジオムバーとファセットでクリック

clickイベントから変数xを抽出して、望ましいフィルタリングを実行するための解決策がありますが、これらのどちらもファセットを使用しないプロットです。私はファセットでggplotのクリックオプションを使用したいと思います。最初のリンクでコードを修正しようとしましたが、これは成功しませんでした。バーのいずれかに

library(ggplot2) 
library(shiny) 

ui <- fluidPage(
    fluidRow(
    plotOutput("plot1", height = 300, width = 300, 
       click = "plot1_click", 
    ) 
), 
    verbatimTextOutput("x_value"), 
    verbatimTextOutput("selected_rows") 
) 

server <- function(input, output) { 
    output$plot1 <- renderPlot({ 
    ggplot(ToothGrowth, aes(supp)) + geom_bar(stat = "count") + facet_wrap(~dose) 
    }) 

    # Print the name of the x value 
    output$x_value <- renderPrint({ 
    if (is.null(input$plot1_click$x)) return() 

    lvls <- levels(ToothGrowth$supp) 
    lvls[round(input$plot1_click$x)] 
    }) 

    # Print the rows of the data frame which match the x value 
    output$selected_rows <- renderPrint({ 
    if (is.null(input$plot1_click$x)) return() 

    keeprows <- round(input$plot1_click$x) == as.numeric(ToothGrowth$supp) 
    ToothGrowth[keeprows, ] 
    }) 
} 

shinyApp(ui, server) 

クリックx値にフィルタリングすることもありませんが、すべての面からの結果が含まれています。私はクリックされたファセットからの結果だけを含めることを望みます。

enter image description here

私はpannelvar1nearPointsを使用して試してみましたが、私はそれに渡すy変数を持っていないので、それはエラーをスローします。

どのような考えですか?

答えて

2

私はあなたの最後の機能を変更し、あなたが望むと思う結果を得ました。

output$selected_rows <- renderPrint({ 
    if (is.null(input$plot1_click$x)) return() 
    panel = input$plot1_click$panelvar1 

    keeprows <- round(input$plot1_click$x) == as.numeric(ToothGrowth$supp) & ToothGrowth$dose==panel 
    ToothGrowth[keeprows, ] 
    }) 

enter image description here

私はあなたがpanelvar1にアクセスしようとしているからなっていたものをエラーわからないが、私は(下の追加メッセージをログ)パネルの変数にアクセスする方法をチェックアウトする設定オプションoptions(shiny.trace=TRUE)を使用。おそらくあなたは間違った位置から値を引き出そうとしていただけです。

RECV {"method":"update","data":{"plot1_click":{"x":1.1651034873830555,"y":4.268063179119527,"panelvar1":2,"mapping":{"x":"supp","y":null,"panelvar1":"dose"},"domain":{"left":0.4,"right":2.6,"bottom":-0.5,"top":10.5},"range":{"left":213.995433789954,"right":294.520547945205,"bottom":268.013698630137,"top":23.4383561643836},"log":{"x":null,"y":null},".nonce":0.39996409229934216}}} 
+0

私は2度upvoteできます。 'options(shiny.trace = TRUE)'の巨大な助け。ありがとう、@ダニエルソン。 –

関連する問題