Creating a reactive dataframe with shiny apps

守給你的承諾、 提交于 2019-12-01 08:12:29

I recommend storing the dataframes in a so-called reactiveValues list. They then have the same reactive properties as things calculated using reactive functions (in that other reactives that depend on them will be triggered when the originals change), but you can get at all of their elements to manipulate them.

In general, it is recommended that simple programs that can get away with only using reactive functions should do so, but I do find that frequently leads to a dead end when things get more complex because there is no logical place to store and update your data.

Note that the reactiveValues is a list, you can store more than one thing in there, and thus keep related things together. All of the members of that list will be reactive.

Here is a very simple example using plotly:

library(plotly)
library(shiny)

ui <- shinyUI(fluidPage(
  plotlyOutput("myPlot"),
  actionButton("regen","Generate New Points")
))

server <- shinyServer(function(input, output) {

  n <- 100  
  rv <- reactiveValues(m=data.frame(x=rnorm(n),y=rnorm(n)))

  observeEvent(input$regen,{
    rv$m <- data.frame(x=rnorm(n),y=rnorm(n))
  })

  output$myPlot <- renderPlotly({
     plot_ly() %>%  add_markers(data=rv$m,x=~x,y=~y  )
  })
})
shinyApp(ui, server)

Here is a screen shot to help visualize it:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!