問題は、出力オブジェクトがすべてのWeb表示オブジェクトも生成していることです。代わりに、ダウンロードのためにデータを個別に取得する必要があります。あなたはダウンロードコードのbrushedPoints
への2回目の呼び出しでそれを行うことができます。しかし、より良いのは、reactive()
関数を使用して一度だけ実行し、必要な場所で呼び出すことです。ここで私はその仕事をするためにコードを変更する方法を次のとおりです。
data(iris)
ui <- basicPage(
plotOutput("plot1", brush = "plot_brush"),
verbatimTextOutput("info"),mainPanel(downloadButton('downloadData', 'Download'))
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(iris,aes(x=Sepal.Width,y=Sepal.Length)) + geom_point(aes(color=factor(Species))) + theme_bw()
})
selectedData <- reactive({
brushedPoints(iris, input$plot_brush)
})
output$info <- renderPrint({
selectedData()
})
output$downloadData <- downloadHandler(
filename = function() {
paste('SelectedRows', '.csv', sep='') },
content = function(file) {
write.csv(selectedData(), file)
}
)
}
shinyApp(ui, server)
(ggplot2
で、明示的brushedPoints
にxvar
とyvar
を設定する必要はありません、注意してくださいので、私はの柔軟性を高めるために、ここでそれを削除しました。コード)
shiny
には "lasso"スタイルの無料描画能力はありません(ただし、1週間与える - 楽しいツールを追加しています)。ただし、複数の領域を選択したり、個々の点をクリックしたりすることで、動作を模倣することができます。サーバーのロジックは、繰り返し使用できるように、結果をreactiveValues
オブジェクトに格納する必要があるため、かなり複雑になります。私は1つのプロット上の点を選択し、他のプロット上でそれらをハイライト/除去することを可能にするために同様のことをしました。それはあなたがここで必要とするものより複雑ですが、以下はうまくいくはずです。他のボタンやロジックを追加することもできます(たとえば、選択を「リセット」するなど)が、これはうまくいくはずです。選択したものを表示して、選択したものを追跡できるようにしました。
data(iris)
ui <- basicPage(
plotOutput("plot1", brush = "plot_brush", click = "plot_click")
, actionButton("toggle", "Toggle Seletion")
, verbatimTextOutput("info")
, mainPanel(downloadButton('downloadData', 'Download'))
)
server <- function(input, output) {
output$plot1 <- renderPlot({
ggplot(withSelected()
, aes(x=Sepal.Width
, y=Sepal.Length
, color=factor(Species)
, shape = Selected)) +
geom_point() +
scale_shape_manual(
values = c("FALSE" = 19
, "TRUE" = 4)
, labels = c("No", "Yes")
, name = "Is Selected?"
) +
theme_bw()
})
# Make a reactive value -- you can set these within other functions
vals <- reactiveValues(
isClicked = rep(FALSE, nrow(iris))
)
# Add a column to the data to ease plotting
# This is really only necessary if you want to show the selected points on the plot
withSelected <- reactive({
data.frame(iris
, Selected = vals$isClicked)
})
# Watch for clicks
observeEvent(input$plot_click, {
res <- nearPoints(withSelected()
, input$plot_click
, allRows = TRUE)
vals$isClicked <-
xor(vals$isClicked
, res$selected_)
})
# Watch for toggle button clicks
observeEvent(input$toggle, {
res <- brushedPoints(withSelected()
, input$plot_brush
, allRows = TRUE)
vals$isClicked <-
xor(vals$isClicked
, res$selected_)
})
# pull the data selection here
selectedData <- reactive({
iris[vals$isClicked, ]
})
output$info <- renderPrint({
selectedData()
})
output$downloadData <- downloadHandler(
filename = function() {
paste('SelectedRows', '.csv', sep='') },
content = function(file) {
write.csv(selectedData(), file)
}
)
}
shinyApp(ui, server)
マーク、これは完全に機能しました!リアクティブ機能は、私の注意を引くのに感謝しています。本当にありがとう。 – KrisF
Mark、brushedPointsオプションでは、四角形内の点のみを選択できるようになりました。可能な "ラッソ"オプションはありますか?私は周りを検索したが、上記のスクリプトを大量書き直す必要があるPlot.lyの "lasso"オプション以外は何も見つかりませんでした。 – KrisF
私が知っていることは直接ではありません。しかし、私は、複数の選択を行う方法を示す編集を追加しました。これは、最終的に同じ動作をする可能性があります。 –