我是一个闪亮的新手,但是我正在尝试将其用于我正在从事的项目中。我希望能够通过单击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感到困惑,以便可以将数据添加到更大的数据库中。任何帮助将不胜感激,如果需要,我很乐意提供有关该项目的更多信息!

最佳答案

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


使用reactiveValues()设置反应性data.frame,其中包含xyinputs的列
使用反应性data.frame创建具有基于input的绘图特征的绘图
单击绘图后,使用observeEvent将新行添加到反应性data.frame
(可选)添加actionButton以删除最后添加的点


下面是一个基于您的代码的简化示例。该表基于this answer

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

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)

关于r - 通过Shiny ggplot中的点击创建数据集,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49190820/

10-12 17:16
查看更多