用用户输入更新点击数据的数据框 [英] Update data frame of click data with user input

查看:187
本文介绍了用用户输入更新点击数据的数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新颖,想跟进我之前的一个问题:
将点击事件中的反应值添加到现有数据帧和更新图。$
我已经更新了代码,似乎理解我应该如何进行。直到这一点,我管理一些帮助,使应用程序更新一个现有的数据框架与点击数据,以便它绘制和更新曲线的回归线。现在我想让用户有一个选项(我正在考虑单选按钮),以便他可以为点击添加的点选一个类(-1/1)。我不知道为什么我不能使用第三个变量(该类)更新数据框架,或者即使我正确地使用它。

 
库(ggplot2)

ui< - basicPage(
plotOutput(plot1,click =plot_click),
radioButtons(cls,Clasa:,choices = list(Red= -1,Blue= 1),selected = 1),
actionButton(refreshline )
verbatimTextOutput(info)


服务器< - 函数(输入,输出){

x1< - c 3,10,15,3,4,7,1,12,8,18,20,4,4,5,10)#x
x2< - c(4,10,12,17, 15,20,14,3,4,15,12,5,5,6,2)#y
cls < - c(-1,1,-1,1,1,1,-1 ,1,-1,1,1,1,-1,1)#class

#初始化与现有数据的无效值
val< - reactiveValues(clickx = NULL, clicky = NULL,data = cbind(x1,x2,cls))

观察({
输入$ cls
输入$ plot_click
隔离({
#保存新点添加
val $ clickx = c(val $ clickx,输入$ plot_click $ x)
val $ clicky = c(val $ clicky,输入$ plot_click $ y)

#向数据添加新点
val $ data < - rbind(val $ data,cbind(input $ plot_click $ x,input $ plot_click $ y,as.numeric(input $ cls)))
})
})


输出$ plot1< - renderPlot({
p < - ggplot(data = NULL,aes(x = val $ data [,1],y = val $ data [,2],color = ifelse(val $ data [,3] 0,Class 1,Class -1)))
p< - p + geom_point()
p< - p + xlab(x1)
p< + ylab(x2)
p< - p + scale_color_manual(name =Class Labels,values = c('#f8766d','#00BFC4')
p< - p + (color = guide_legend(override.aes = list(linetype = 0)),
linetype = guide_legend())
p< - p + theme_bw()
p

if(input $ refreshline)
p< - p + stat_smooth(method = lm)
p

})


输出$信息< - renderText({
input $ plot_click
paste0(x =,val $ clickx,y =,val $ clicky,\\\

} )

}

shinyApp(ui,服务器)


解决方案

看看这个解决方案。它可能还有一些工作要做,但它给你一个可能的方向。我认为对两个投入作出反应的观察者并不是一个很好的解决方案,因为任何一个变化都会添加一行来获取$数据。



这是修改后的代码:

 库(闪亮)
库(ggplot2)

ui< - basicPage(
plotOutput(plot1,click =plot_click),
radioButtons(cls,Clasa:,choices = list(Red= -1,Blue= 1),选择= 1),
actionButton(updateData,更新数据),
actionButton(refreshline,Rline),
verbatimTextOutput(info
verbatimTextOutput(data)


服务器< - 函数(输入,输出){

x1 < 10,15,3,4,7,1,12,8,18,20,4,4,5,10)#x
x2 <-C(4,10,12,17,15, 20,14,3,4,15,12,5,5,6,2)#y
cls < - c(-1,1,-1,1,1,1,-1,1 ,-1,1,1,1,1,-1,1)#class

#使用现有数据初始化无效值
val< - reactiveValues(clickx = NULL,clicky = NULL,data = cbind(x1,x2,cls))

observeEvent(输入$ updateData, {
if(input $ updateData> 0){
val $ data < - rbind(val $ data,cbind(input $ plot_click $ x,input $ plot_click $ y,as.numeric(input $ cls)))
}

})
observeEvent(input $ plot_click,{
val $ clickx = c(val $ clickx,input $ plot_click $ x)
val $ clicky = c $ click,输入$ plot_click $ y)
})

输出$ plot1< - renderPlot({
p< - ggplot(data = NULL,aes(x = val $ data [,1],y = val $ data [,2],color = ifelse(val $ data [,3]> 0,Class 1,Class -1)))
p< ; - p + geom_point()
p< - p + xlab(x1)
p< - p + ylab(x2)
p <-p + scale_color_manual class Labels,values = c('#f8766d','#00BFC4'))
p< - p + guides(color = guide_legend(override.aes = list(linetype = 0)),
lnetype = guide_legend())
p< - p + theme_bw()
p

if(input $ refreshline)
p< - p + stat_smooth(method = lm )
p

})


输出$ info< - renderText({
input $ plot_click
paste0( x =,val $ clickx,y =,val $ clicky,\\\

})
输出$ data< - renderPrint({
val $ data
})

}

shinyApp(ui,服务器)


I am new to shiny and would like to follow-up on one of my previous questions: Add reactive values from click events to existing data frame and update plot. I have updated the code but don't seem to understand how I should proceed. Till this point I managed with some help to make the app update an existing data frame with click data so that it plots and updates the regression line in the plot. Now I would like the user to have an option (I was thinking of radiobuttons) so that he can choose a class (-1/1) for those points he added with clicks. I don't know why I cant update the data frame with this third variable (the class) or even if I am going about it the right way.

 library(shiny)
    library(ggplot2)

    ui <- basicPage(
      plotOutput("plot1", click = "plot_click"),
      radioButtons("cls", "Clasa:", choices = list("Red" = -1, "Blue" = 1), selected = 1), 
      actionButton("refreshline", "Rline"),
      verbatimTextOutput("info")
    )

    server <- function(input, output) {

      x1 <- c(3, 10, 15, 3, 4, 7, 1, 12, 8, 18, 20, 4, 4, 5, 10)   #x
      x2 <- c(4, 10, 12, 17, 15, 20, 14, 3, 4, 15, 12, 5, 5, 6, 2) #y
      cls <- c(-1, 1, -1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, 1)   #class

      # initialize reactive values with existing data
      val <- reactiveValues( clickx = NULL, clicky = NULL, data = cbind (x1, x2, cls))

        observe({
        input$cls
        input$plot_click
        isolate({
          # save new points added
          val$clickx = c(val$clickx, input$plot_click$x)
          val$clicky = c(val$clicky, input$plot_click$y)

          # add new points to data                                              
          val$data <- rbind(val$data, cbind(input$plot_click$x, input$plot_click$y, as.numeric(input$cls)))
         })
  })


    output$plot1 <- renderPlot({
      p <- ggplot(data = NULL, aes(x=val$data[,1], y=val$data[,2], color = ifelse(val$data[,3] > 0, "Class 1","Class -1")))
      p <- p + geom_point()
      p <- p + xlab("x1")  
      p <- p + ylab("x2") 
     p <- p + scale_color_manual(name="Class Labels", values=c('#f8766d','#00BFC4'))
      p <- p + guides(color = guide_legend(override.aes = list(linetype = 0 )), 
                      linetype = guide_legend())
      p <- p + theme_bw() 
      p

      if(input$refreshline)
      p <- p + stat_smooth(method=lm)                         
      p

    })


      output$info <- renderText({
        input$plot_click
        paste0("x = ", val$clickx, ", y = ",val$clicky, "\n")
      })

    }

    shinyApp(ui, server)

解决方案

Take a look at this solution. It probably has still some work do to but it gives you a possible direction. I think that the observer that was reacting to two inputs was not a good solution since a change in any of them would add a row to val$data.

This is the modified code:

    library(shiny)
    library(ggplot2)

    ui <- basicPage(
            plotOutput("plot1", click = "plot_click"),
            radioButtons("cls", "Clasa:", choices = list("Red" = -1, "Blue" = 1), selected = 1), 
            actionButton("updateData", "Update data"),
            actionButton("refreshline", "Rline"),
            verbatimTextOutput("info"),
            verbatimTextOutput("data")
    )

    server <- function(input, output) {

            x1 <- c(3, 10, 15, 3, 4, 7, 1, 12, 8, 18, 20, 4, 4, 5, 10)   #x
            x2 <- c(4, 10, 12, 17, 15, 20, 14, 3, 4, 15, 12, 5, 5, 6, 2) #y
            cls <- c(-1, 1, -1, 1, 1, 1, -1, 1, -1, 1, 1, 1, 1, -1, 1)   #class

            # initialize reactive values with existing data
            val <- reactiveValues( clickx = NULL, clicky = NULL, data = cbind (x1, x2, cls))

            observeEvent(input$updateData, {
                    if (input$updateData > 0) {
                            val$data <- rbind(val$data, cbind(input$plot_click$x, input$plot_click$y, as.numeric(input$cls)))
                    }

            })
            observeEvent(input$plot_click, {
                    val$clickx = c(val$clickx, input$plot_click$x)
                    val$clicky = c(val$clicky, input$plot_click$y)  
            })        

            output$plot1 <- renderPlot({
                    p <- ggplot(data = NULL, aes(x=val$data[,1], y=val$data[,2], color = ifelse(val$data[,3] > 0, "Class 1","Class -1")))
                    p <- p + geom_point()
                    p <- p + xlab("x1")  
                    p <- p + ylab("x2") 
                    p <- p + scale_color_manual(name="Class Labels", values=c('#f8766d','#00BFC4'))
                    p <- p + guides(color = guide_legend(override.aes = list(linetype = 0 )), 
                                    linetype = guide_legend())
                    p <- p + theme_bw() 
                    p

                    if(input$refreshline)
                            p <- p + stat_smooth(method=lm)                         
                    p

            })


            output$info <- renderText({
                    input$plot_click
                    paste0("x = ", val$clickx, ", y = ",val$clicky, "\n")
            })
            output$data <- renderPrint({
                    val$data
            })

    }

    shinyApp(ui, server)

这篇关于用用户输入更新点击数据的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆