Create data set from clicks in Shiny ggplot

后端 未结 1 1944
不思量自难忘°
不思量自难忘° 2020-12-09 14:07

I\'m a Shiny novice, but I\'m trying to use it in a project that I\'m working on. I\'d like to be able to do two things from clicking on a point on a ggplot graph: add a plo

1条回答
  •  攒了一身酷
    2020-12-09 14:36

    Here's a general framework that you could use:

    1. Use reactiveValues() to set up a reactive data.frame with columns for x, y, inputs
    2. Create a plot using the reactive data.frame with plotting characteristics based on input
    3. Upon clicking plot, add a new row to the reactive data.frame using observeEvent
    4. (Optional) Add an actionButton to remove the last added point

    A simplified example based on your code is below. The table is based on this answer.

    library(shiny)
    library(ggplot2)
    
    ui <- pageWithSidebar(
        headerPanel("Example"),
        sidebarPanel(
            radioButtons("color", "Pick Color", c("Pink", "Green", "Blue")),
            selectInput("shape", "Select Shape:", c("Circle", "Triangle"))
        ),
        mainPanel(
            fluidRow(column(width = 6,
                            h4("Click plot to add points"),
                            actionButton("rem_point", "Remove Last Point"),
                            plotOutput("plot1", click = "plot_click")),
                     column(width = 6,
                            h4("Table of points on plot"),
                            tableOutput("table")))
        )
    )
    
    server = function(input, output){
    
        ## 1. set up reactive dataframe ##
        values <- reactiveValues()
        values$DT <- data.frame(x = numeric(),
                                y = numeric(),
                                color = factor(),
                                shape = factor())
    
        ## 2. Create a plot ##
        output$plot1 = renderPlot({
           ggplot(values$DT, aes(x = x, y = y)) +
                geom_point(aes(color = color,
                               shape = shape), size = 5) +
                lims(x = c(0, 100), y = c(0, 100)) +
                theme(legend.position = "bottom") +
                # include so that colors don't change as more color/shape chosen
                scale_color_discrete(drop = FALSE) +
                scale_shape_discrete(drop = FALSE)
        })
    
        ## 3. add new row to reactive dataframe upon clicking plot ##
        observeEvent(input$plot_click, {
            # each input is a factor so levels are consistent for plotting characteristics
            add_row <- data.frame(x = input$plot_click$x,
                                  y = input$plot_click$y,
                                  color = factor(input$color, levels = c("Pink", "Green", "Blue")),
                                  shape = factor(input$shape, levels = c("Circle", "Triangle")))
            # add row to the data.frame
            values$DT <- rbind(values$DT, add_row)
        })
    
        ## 4. remove row on actionButton click ##
        observeEvent(input$rem_point, {
            rem_row <- values$DT[-nrow(values$DT), ]
            values$DT <- rem_row
        })
    
        ## 5. render a table of the growing dataframe ##
        output$table <- renderTable({
            values$DT
        })
    }
    
    shinyApp(ui, server)
    

    0 讨论(0)
提交回复
热议问题