通过Shiny ggplot中的点击创建数据集 [英] Create data set from clicks in Shiny ggplot
问题描述
我是一个闪亮的新手,但我正在尝试将它用于我正在开发的一个项目中。我希望能够通过点击ggplot图上的一个点来做两件事:在指定点添加一个情节角色(以侧栏中的信息为条件),并将坐标(来自侧栏的信息)添加到一个数据框。以下是代码到目前为止:
库(闪亮)
库(ggplot2)
df = data.frame()
ui = pageWithSidebar(
headerPanel(Test),
sidebarPanel (
radioButtons(orientation,Pick,c(L,P,H)),
selectInput(
select1,
选择此处:,
c(选项1,选项2)
),
selectInput(
select2,
选择此处:,
c(选项3,选项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)
服务器=函数(输入,输出){
输出$ plot1 = renderPlot({
ggplot(df) + geom_rect(xmin = 0,xmax = 100, ymin = 0,ymax = 50,fill =red)
})
输出$ info = renderText({
paste0(x =,input $ plot_click $ x,\\\
y =,输入$ plot_click $ y)
})
}
shinyApp(ui,server)
我很困惑如何将点击的x和y点从 plot_click
添加到 df
,这样我就可以将数据添加到更大的数据库中。任何帮助,将不胜感激,我很乐意提供有关该项目的更多信息,如果需要的话!
您可以使用的框架:
- 使用
reactiveValues()
data.frame包含x
,y
,输入
- 使用反应数据框创建一个图,绘制基于
输入
- 在点击图表后,使用
observeEvent
添加一个新行到被动数据框架中($可选)添加一个 - Use
reactiveValues()
to set up a reactive data.frame with columns forx
,y
,inputs
- Create a plot using the reactive data.frame with plotting characteristics based on
input
- Upon clicking plot, add a new row to the reactive data.frame using
observeEvent
- (Optional) Add an
actionButton
to remove the last added point
actionButton
删除最后添加的点 下面是一个基于代码的简化示例。该表格基于
库(闪亮)
库(ggplot2)
ui< - pageWithSidebar(
headerPanel(示例),
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,点击=plot_click)),
列(宽度= 6,
h4(图表上的点数),
tableO utput(table)))
)
)
server = function(输入,输出){
## 1.设置被动dataframe ##
values< - reactiveValues()
values $ DT< - data.frame(x = numeric(),
y = numeric(),
color = factor (),
shape = factor())
## 2.创建一个plot ##
output $ plot1 = renderPlot({
ggplot(values $ DT ,aes(x = x,y = y))+
geom_point(aes(color = color,$ b $ shape = shape),size = 5)+
lims(x = c(0 ,100),y = c(0,100))+
主题(legend.position =bottom)+
#包含颜色不会随着更多颜色/形状的变化而变化
scale_color_discrete(drop = FALSE)+
scale_shape_discrete(drop = FALSE)
})
## 3.点击图形后向无效数据框添加新行##
ob serveEvent(输入$ plot_click,{
#每个输入是一个因子,因此水平对绘图特征是一致的
add_row < - data.frame(x = input $ plot_click $ x,
y = input $ plot_click $ y,
color = factor(输入$ color,levels = c(Pink,Green,Blue)),
shape = factor(输入$ shape,levels = c (Circle,Triangle)))
#将行添加到data.frame
值$ DT< - rbind(values $ DT,add_row)
})
## 4.移除action on actionButton click ##
observeEvent(输入$ rem_point,{
rem_row < - values $ DT [-nrow(values $ DT),]
values $ DT < - rem_row
})
## 5.渲染一个增长的数据框表##
输出$ table< - renderTable({
值$ DT
))
}
shinyApp(ui,server)
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:
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)
这篇关于通过Shiny ggplot中的点击创建数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!