【问题标题】:Putting reset button in Rshiny interactive plotting在 Rshiny 交互式绘图中放置重置按钮
【发布时间】:2013-11-05 15:33:56
【问题描述】:

我想在交互式绘图闪亮应用程序中添加一个重置按钮。该应用程序执行以下操作:在鼠标单击时捕获鼠标的位置,在该位置绘制一个红点,并用蓝线连接所有先前的位置。如果按下重置按钮,则应删除所有红点和蓝线,并开始新的绘图过程。但是,实际上,当我单击重置按钮时,最后一个红点仍在绘图区域上。我不确定出了什么问题。感谢您的帮助。

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%")
)
))

【问题讨论】:

    标签: r plot shiny


    【解决方案1】:

    问题在于,当您测试 actiontype$lastAction 是否为 'reset' 时,您会立即将 actiontype$lastAction 设置为 'draw',因此,您输入了 else 部分,其中创建了 temp,最后,绘制。即使您将xvalyval 设置为NULL,上一次单击时仍存在input$coords。因此,解决方案应该在于“重置”input$coords

    经过多次不同的尝试(例如,将 input$coords 设置为 NULL 等),我想出了以下内容(我在任何更改的地方都带有评论):

    #server.R
    library(shiny)
    N = 30
    x = sort(runif(N, 0, 10)); y = x + rnorm(N)
    xval=NULL
    yval=NULL
    checker <- 1 #### CHANGE
    
    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
            checker <<- 0 ####CHANGE
            actiontype$lastAction <- 'draw'
    
        } else if (identical(actiontype$lastAction, 'draw')){
            temp <- get.coords()
            xval <<- c(xval,temp$x)
            yval <<- c(yval,temp$y)
    
            ########### CHANGE...
            if(identical(checker, 0))
             {
               points(xval, yval, pch = 19, col = rgb(1,0,0,0), cex = 1.5)
               xval <<- NULL
               yval <<- NULL
               checker <<- 1
             }else
             {
              points(xval, yval, pch = 19, col = 'red', cex = 1.5)
             }
            ############# ...CHANGE
    
            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)
    })
    

    我的想法是实际绘制最后存储的input$coords,但透明度为 100%,此外,删除上一次点击的input$coordschecker 的作用是在 10 之间波动,并做到这一点。

    这可能更容易实现,但我现在想不出更清晰的方法。

    【讨论】:

    • 这真的很有帮助。非常感谢。我正在进一步开发应用程序。
    【解决方案2】:

    最后一点没有消失的原因是,一旦点击,它就会一直存在,直到它被新点击的数据替换。据我所知,此功能无法修改。但是处理这些数据的方式可以。

    事实证明,clickID 实际上向 Shiny 会话发送了 3 个数值,而不仅仅是 2:x、y 和一个唯一标识符 double。我发现解决方案实际上在于充分利用第三个标识符。我存储在一个reactiveValue对象中,我们称之为“reactive$uniqueIdentifier”,该标识符只更新了第二个reactiveValue对象,我们称之为“reactive$clickedCoordinates”,当新的clickID的标识符与以前的不同时,clickID坐标.然后我使用 reactive$clickedCoordinates 作为我想要绘制的点的坐标。然后,当我想重置单击的坐标并取消绘制所有内容时,我只需将 reactive$clickedCoordinates 的值设置为 NULL。这使得所有绘制的点都消失了! :-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      • 2014-03-27
      • 2016-01-22
      • 2022-10-08
      • 2019-01-04
      相关资源
      最近更新 更多