我想在交互式绘图闪亮应用程序中添加一个重置按钮。该应用程序执行以下操作:在鼠标单击时捕获鼠标的位置,在该位置绘制一个红点,并用蓝线连接所有先前的位置。如果按下重置按钮,则应移除所有红点和蓝线,并开始新的绘图过程。但是,实际上,当我单击重置按钮时,最后一个红点仍在绘图区域上。我不确定出了什么问题。谢谢您的帮助。
PS:感谢jcheng5提供剧情鼠标事件!
server.R 如下:
library(shiny)
N = 30 # sample size
x = sort(runif(N, 0, 10)); y = x + rnorm(N)
xval=NULL
yval=NULL
shinyServer(function(input, output) {
get.coords <- reactive({
data.frame(x=input$coords$x, y=input$coords$y)
})
actiontype <- reactiveValues()
actiontype$lastAction <- 'draw'
observe({
if (input$reset != 0)
actiontype$lastAction <- 'reset'
})
observe({
if (input$closepolygon != 0)
actiontype$lastAction <- 'closepolygon'
})
output$diagPlot = renderPlot({
plot(x, y, xlim = range(x), ylim = range(y))
grid()
if (identical(actiontype$lastAction, 'reset')) {
xval <<- NULL
yval <<- NULL
actiontype$lastAction <- 'draw'
} else if (identical(actiontype$lastAction, 'draw')){
temp <- get.coords()
xval <<- c(xval,temp$x)
yval <<- c(yval,temp$y)
points(xval, yval, pch = 19, col = 'red', cex = 1.5)
for (i in 1:(length(xval)-1))
lines(c(xval[i],xval[i+1]),c(yval[i],yval[i+1]),type="l",col="blue")
if(identical(actiontype$lastAction, 'closepolygon'))
lines(c(xval[1],xval[length(xval)]),c(yval[1],yval[length(yval)]),type="l",col="blue")
}
}, width = 700, height = 600)
})
和 ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel('iPED: The interactive ChIP-PED analysis platform'),
sidebarPanel(
helpText("graph"),
actionButton('closepolygon', 'Close the Polygon'),
actionButton('reset', 'Reset')
),
mainPanel(
plotOutput('diagPlot', clickId="coords", height="auto", width="100%")
)
))