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 plot character at the specified point (conditioned on info from the sidebar), and add the coordinates (with info from the sidebar) to a data frame. Here's what I've got in terms of code so far:
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)
I'm confused on how to add the clicked x and y points from plot_click
to df
so that I can add the data to a bigger database. Any help would be appreciated, and I'd be happy to give more info about the project if needed!
Here's a general framework that you could use:
reactiveValues()
to set up a reactive data.frame with columns forx
,y
,inputs
input
observeEvent
actionButton
to remove the last added pointA simplified example based on your code is below. The table is based on this answer.