Reading objects from shiny output object not allowed?

前端 未结 1 1836
礼貌的吻别
礼貌的吻别 2020-12-28 09:37

I\'m trying to write a little app that will allow the user to make a scatterplot, select a subset of points on the plot, then output a table in .csv format with just those s

相关标签:
1条回答
  • 2020-12-28 10:30

    The issue is that the output object is generating all of the web display stuff as well. Instead, you need to pull the data separately for the download. You could do it with a second call to brushedPoints in the download code. Better, however, is to use a reactive() function to do it just once, then call that everywhere that you need it. Here is how I would modify your code to make that work:

    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)
    

    (Note, with ggplot2, you do not need to explicitly set xvar and yvar in brushedPoints. So, I removed it here to increase the flexibility of the code.)

    I am not aware of any "lasso" style free drawing ability in shiny (though, give it a week -- they are constantly adding fun tools). However, you can mimic the behavior by allowing user to select multiple regions and/or to click on individual points. The server logic gets a lot messier, as you need to store the results in a reactiveValues object to be able to use it repeatedly. I have done something similar to allow me to select points on one plot and highlight/remove them on other plots. That is more complicated than what you need here, but the below should work. You may want to add other buttons/logic (e.g., to "reset" the selections), but I believe that this should work. I did add a display of the selection to the plot to allow you to keep track of what has been selected.

    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)
    
    0 讨论(0)
提交回复
热议问题