繁体   English   中英

R Shiny:创建一个用于更新data.frame的按钮

[英]R Shiny: Create a button that updates a data.frame

我有一个随机生成的data.frame。 用户可以修改滑块以选择点数。 然后,我绘制此data.frame。

我想添加一个按钮,而不是单击按钮,它会在先前随机生成的data.frame中执行修改(但不重新生成data.frame)。 修改是一种类涡流松弛,每次单击按钮并生成图形时,都应执行一次。

到目前为止,我还没有取得任何类似的成就...

用户界面

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("Map Generator:"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      p("Select the power p to generate 2^p points."),
      sliderInput("NumPoints",
                  "Number of points:",
                  min = 1,
                  max = 10,
                  value = 9),
      actionButton("GenPoints", "Generate"),
      actionButton("LloydAlg", "Relaxe")
    ),

    # Show a plot of the generated distribution
    mainPanel(



      plotOutput("distPlot",height = 700, width = "auto")
    )
  )
))

服务器

library(shiny)
library(deldir)

shinyServer(function(input, output) {


  observeEvent(input$NumPoints,{

    x = data.frame(X = runif(2^input$NumPoints,1,1E6),
                   Y = runif(2^input$NumPoints,1,1E6))

    observeEvent(input$LloydAlg, {
        x = tile.centroids(tile.list(deldir(x)))
    })

    output$distPlot <- renderPlot({
        plot(x,pch = 20,asp=1,xlim=c(0,1E6),ylim = c(0,1E6))
  })

  })
})

当然,有些事情我一定做错了,但是我很新,无法弄清楚自己做错了什么...

这应该工作(即使我很确定这可以改善):

shinyServer(function(input, output) {
  library(deldir)  

  data = data.frame(
    X = runif(2^9, 1, 1E6),
    Y = runif(2^9, 1, 1E6)
  )

  rv <- reactiveValues(x = data)

  observeEvent(input$GenPoints, {
    rv$x <- data.frame(
      X = runif(2^input$NumPoints,1,1E6),
      Y = runif(2^input$NumPoints,1,1E6)
    )
  })
  observeEvent(input$LloydAlg, {
    rv$x = tile.centroids(tile.list(deldir(rv$x)))
  })

  output$distPlot <- renderPlot({
    plot(rv$x,pch = 20,asp=1,xlim=c(0,1E6),ylim = c(0,1E6))
  })
})

因此,首先我初始化要绘制的点。 我使用runif(2^9, 1, 1E6)因为sliderInput都是9。

我还从sliderInput移除了observeEvent ,并将其移至GenPoints actionButton

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM