根据Shiny ggplot中的点击创建数据集

时间:2018-03-09 09:44:23

标签: r ggplot2 shiny

我是一个闪亮的新手,但我正在尝试在我正在进行的项目中使用它。我希望能够通过点击ggplot图表上的点来做两件事:在指定点添加一个绘图字符(以侧边栏中的信息为条件),并添加坐标(来自侧边栏的信息)到数据框。到目前为止,这就是我所拥有的代码:

library(shiny)
library(ggplot2)

df = data.frame()


ui = pageWithSidebar(
  headerPanel("Test"),

  sidebarPanel(
    radioButtons("orientation", "Pick", c("L", "P", "H")),

    selectInput(
      "select1",
      "Select Here:",
      c("Option 1", "Option 2")
    ),

    selectInput(
      "select2",
      "Select Here:",
      c("Option 3", "Option 4"),
    ),

    radioButtons("type", "Type:", c("P", "S")),

    radioButtons("creator", "Creator?", c("H", "A"))
  ),

  mainPanel(
    plotOutput("plot1", click = "plot_click"),
    verbatimTextOutput("info"),
    actionButton("update", "Add Event")
  )
)

server = function(input, output){

  output$plot1 = renderPlot({
    ggplot(df) + geom_rect(xmin = 0, xmax = 100, ymin = 0, ymax = 50, fill = "red")
  })

  output$info = renderText({
    paste0("x = ", input$plot_click$x, "\ny = ", input$plot_click$y)
  })
}

shinyApp(ui, server)

我对如何将点击的x和y点从plot_click添加到df感到困惑,这样我就可以将数据添加到更大的数据库中。任何帮助将不胜感激,如果需要,我很乐意提供有关该项目的更多信息!

1 个答案:

答案 0 :(得分:4)

以下是您可以使用的一般框架:

  1. 使用reactiveValues()设置包含xyinputs
  2. 列的被动数据框。
  3. 使用基于input
  4. 绘制特征的反应数据框架创建绘图
  5. 点击图表后,使用observeEvent
  6. 向反应数据框架添加新行
  7. (可选)添加actionButton以删除最后添加的点
  8. 基于您的代码的简化示例如下。该表基于this answer

    enter image description here

    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)