R 如何在图形中显示图形单击
我希望用户能够点击一个绘图,当他们录制时,在他们点击的地方留下一个标记或消息 我在绘图环境中使用反应值。这似乎是在重新设定情节。几乎是在消息出现之后 这里有一个最小的不完全工作的例子R 如何在图形中显示图形单击,r,shiny,R,Shiny,我希望用户能够点击一个绘图,当他们录制时,在他们点击的地方留下一个标记或消息 我在绘图环境中使用反应值。这似乎是在重新设定情节。几乎是在消息出现之后 这里有一个最小的不完全工作的例子 library(shiny) ## ui.R ui <- fluidPage( shinyjs::useShinyjs(), column(12, plotOutput("Locations", width=500, height=500, click
library(shiny)
## ui.R
ui <- fluidPage(
shinyjs::useShinyjs(),
column(12,
plotOutput("Locations", width=500, height=500,
click="plot_click") )
)
## server.R
server <- function( input, output, session){
## Source Locations (Home Base)
source_coords <- reactiveValues(xy=c(x=1, y=2) )
## Dest Coords
dest_coords <- reactive({
if (is.null(input$plot_click) ){
list( x=source_coords$xy[1],
y=source_coords$xy[2])
} else {
list( x=floor(input$plot_click$x),
y=floor(input$plot_click$y))
}
})
## Calculate Manhattan Distance from Source to Destination
DistCost <- reactive({
list( Lost=sum( abs(
c(dest_coords()$x, dest_coords()$y) - source_coords$xy
) ) )
})
## RenderPlot
output$Locations <- renderPlot({
par(bg=NA)
plot.new()
plot.window(
xlim=c(0,10), ylim=c(0,10),
yaxs="i", xaxs="i")
axis(1)
axis(2)
grid(10,10, col="black")
box()
## Source
points( source_coords$xy[1], source_coords$xy[2], cex=3, pch=intToUtf8(8962))
## Destination
text(dest_coords()$x, dest_coords()$y, paste0("Distance=", DistCost() ))
})
}
### Run Application
shinyApp(ui, server)
库(闪亮)
##用户界面
ui问题在于,input$plot\u click
从用户单击中获取值后立即刷新自身,并返回到NULL
。您可以通过创建空列表存储来测试是否存在问题,input$plot\u click
从用户单击中获取值后立即刷新,并返回到NULL
。您可以通过创建空列表storaged来测试自己是否只显示最近单击的点,还是显示所有单击的点。由于Pawel的答案涵盖了前一个案例(并且已经是一个被接受的答案,这意味着它可能是目的),我将发布一个解决方案给前一个案例,以备将来参考,以防它有帮助
library(magrittr)
library(shiny)
## ui.R
ui <- fluidPage(
shinyjs::useShinyjs(),
column(12,
plotOutput("Locations", width=500, height=500,
click="plot_click") )
)
## server.R
server <- function( input, output, session){
initX <- 1
initY <- 2
## Source Locations (Home Base)
source_coords <- reactiveValues(xy=c(x=initX, y=initY) )
## Dest Coords
dest_coords <- reactiveValues(x=initX, y=initY)
observeEvent(plot_click_slow(), {
dest_coords$x <- c(dest_coords$x, floor(plot_click_slow()$x))
dest_coords$y <- c(dest_coords$y, floor(plot_click_slow()$y))
})
## Don't fire off the plot click too often
plot_click_slow <- debounce(reactive(input$plot_click), 300)
## Calculate Manhattan Distance from Source to Destination
DistCost <- reactive({
num_points <- length(dest_coords$x)
list( Lost= lapply(seq(num_points), function(n) {
sum( abs(
c(dest_coords$x[n], dest_coords$y[n]) - source_coords$xy
) )
}) )
})
## RenderPlot
output$Locations <- renderPlot({
par(bg=NA)
plot.new()
plot.window(
xlim=c(0,10), ylim=c(0,10),
yaxs="i", xaxs="i")
axis(1)
axis(2)
grid(10,10, col="black")
box()
## Source
points( source_coords$xy[1], source_coords$xy[2], cex=3, pch=intToUtf8(8962))
## Destination
text(dest_coords$x, dest_coords$y, paste0("Distance=", DistCost()$Lost ))
})
}
### Run Application
shinyApp(ui, server)
库(magrittr)
图书馆(闪亮)
##用户界面
ui我不确定目的是只显示最近单击的点,还是显示所有单击的点。由于Pawel的答案涵盖了前一个案例(并且已经是一个被接受的答案,这意味着它可能是目的),我将发布一个解决方案给前一个案例,以备将来参考,以防它有帮助
library(magrittr)
library(shiny)
## ui.R
ui <- fluidPage(
shinyjs::useShinyjs(),
column(12,
plotOutput("Locations", width=500, height=500,
click="plot_click") )
)
## server.R
server <- function( input, output, session){
initX <- 1
initY <- 2
## Source Locations (Home Base)
source_coords <- reactiveValues(xy=c(x=initX, y=initY) )
## Dest Coords
dest_coords <- reactiveValues(x=initX, y=initY)
observeEvent(plot_click_slow(), {
dest_coords$x <- c(dest_coords$x, floor(plot_click_slow()$x))
dest_coords$y <- c(dest_coords$y, floor(plot_click_slow()$y))
})
## Don't fire off the plot click too often
plot_click_slow <- debounce(reactive(input$plot_click), 300)
## Calculate Manhattan Distance from Source to Destination
DistCost <- reactive({
num_points <- length(dest_coords$x)
list( Lost= lapply(seq(num_points), function(n) {
sum( abs(
c(dest_coords$x[n], dest_coords$y[n]) - source_coords$xy
) )
}) )
})
## RenderPlot
output$Locations <- renderPlot({
par(bg=NA)
plot.new()
plot.window(
xlim=c(0,10), ylim=c(0,10),
yaxs="i", xaxs="i")
axis(1)
axis(2)
grid(10,10, col="black")
box()
## Source
points( source_coords$xy[1], source_coords$xy[2], cex=3, pch=intToUtf8(8962))
## Destination
text(dest_coords$x, dest_coords$y, paste0("Distance=", DistCost()$Lost ))
})
}
### Run Application
shinyApp(ui, server)
库(magrittr)
图书馆(闪亮)
##用户界面
用户界面和一个附加说明。你应该避免这种反应性结构,其中有些C反应性地依赖于A和B,同时B反应性地依赖于A。因为C总是有可能因为A中的变化而反应性地运行代码,它会断开,因为在C需要B之前,B无法完成。你应该总是把B放在C里面,或者把A和B合并成一个反应式。你说我应该避免哪种被动结构——你的答案还是我原来的帖子。如果你是指你的答案,你能告诉我什么会更好。哦,对不起,我指的是你原来的帖子。你的目的地坐标是一个反应性的,在那之后,DistCost是另一个反应性的,它依赖于目的地坐标,所以在目的地坐标发生任何变化时,它都会重新评估它的代码,最后,你的情节反应性地依赖于它们:目的地坐标和DistCost。在我的例子中,你的图是C,DistCost是B,dest_coords是A。很抱歉,在这个例子中没有明确说明。还有一个补充说明。你应该避免这种反应性结构,其中有些C反应性地依赖于A和B,同时B反应性地依赖于A。因为C总是有可能因为A中的变化而反应性地运行代码,它会断开,因为在C需要B之前,B无法完成。你应该总是把B放在C里面,或者把A和B合并成一个反应式。你说我应该避免哪种被动结构——你的答案还是我原来的帖子。如果你是指你的答案,你能告诉我什么会更好。哦,对不起,我指的是你原来的帖子。你的目的地坐标是一个反应性的,在那之后,DistCost是另一个反应性的,它依赖于目的地坐标,所以在目的地坐标发生任何变化时,它都会重新评估它的代码,最后,你的情节反应性地依赖于它们:目的地坐标和DistCost。在我的例子中,你的图是C,DistCost是B,dest_coords是A。很抱歉,在这个例子中没有明确说明。
library(magrittr)
library(shiny)
## ui.R
ui <- fluidPage(
shinyjs::useShinyjs(),
column(12,
plotOutput("Locations", width=500, height=500,
click="plot_click") )
)
## server.R
server <- function( input, output, session){
initX <- 1
initY <- 2
## Source Locations (Home Base)
source_coords <- reactiveValues(xy=c(x=initX, y=initY) )
## Dest Coords
dest_coords <- reactiveValues(x=initX, y=initY)
observeEvent(plot_click_slow(), {
dest_coords$x <- c(dest_coords$x, floor(plot_click_slow()$x))
dest_coords$y <- c(dest_coords$y, floor(plot_click_slow()$y))
})
## Don't fire off the plot click too often
plot_click_slow <- debounce(reactive(input$plot_click), 300)
## Calculate Manhattan Distance from Source to Destination
DistCost <- reactive({
num_points <- length(dest_coords$x)
list( Lost= lapply(seq(num_points), function(n) {
sum( abs(
c(dest_coords$x[n], dest_coords$y[n]) - source_coords$xy
) )
}) )
})
## RenderPlot
output$Locations <- renderPlot({
par(bg=NA)
plot.new()
plot.window(
xlim=c(0,10), ylim=c(0,10),
yaxs="i", xaxs="i")
axis(1)
axis(2)
grid(10,10, col="black")
box()
## Source
points( source_coords$xy[1], source_coords$xy[2], cex=3, pch=intToUtf8(8962))
## Destination
text(dest_coords$x, dest_coords$y, paste0("Distance=", DistCost()$Lost ))
})
}
### Run Application
shinyApp(ui, server)