简体   繁体   English

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

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

I have a randomly generated data.frame. 我有一个随机生成的data.frame。 The user can modify a slider to choose the number of points. 用户可以修改滑块以选择点数。 Then I plot this data.frame. 然后,我绘制此data.frame。

I want to add a button than when clicked, it performs a modification in the previous randomly generated data.frame (but without regenerating the data.frame). 我想添加一个按钮,而不是单击按钮,它会在先前随机生成的data.frame中执行修改(但不重新生成data.frame)。 The modification is a voronoid relaxation, and it should be performed once per each time the button is clicked and the graph generated. 修改是一种类涡流松弛,每次单击按钮并生成图形时,都应执行一次。

Until now, I have not achieved anything similar... 到目前为止,我还没有取得任何类似的成就...

ui.R 用户界面

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

server.R 服务器

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))
  })

  })
})

Of course there is something that I must be doing wrong, but I am quite new into shiny I can't figure it out what I am doing wrong... 当然,有些事情我一定做错了,但是我很新,无法弄清楚自己做错了什么...

This should work (even though I am pretty sure this could be improved): 这应该工作(即使我很确定这可以改善):

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))
  })
})

So first I initialize the points to plot. 因此,首先我初始化要绘制的点。 I use runif(2^9, 1, 1E6) because the starting value of the sliderInput is 9 all the time. 我使用runif(2^9, 1, 1E6)因为sliderInput都是9。

I also removed the observeEvent from the sliderInput and moved it to the GenPoints actionButton . 我还从sliderInput移除了observeEvent ,并将其移至GenPoints actionButton

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

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