通过Shiny ggplot中的点击创建数据集

ros*_*er9 3 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)
Run Code Online (Sandbox Code Playgroud)

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

Hal*_*wan 5

这是您可以使用的通用框架:

  1. 使用reactiveValues()建立一个反应data.frame与列xyinputs
  2. 使用反应性data.frame创建具有基于以下特征的绘图 input
  3. 单击绘图后,使用以下命令向反应性data.frame添加新行 observeEvent
  4. (可选)添加actionButton以删除最后添加的点

下面是一个基于您的代码的简化示例。该表基于此答案

在此处输入图片说明

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)
Run Code Online (Sandbox Code Playgroud)