Updating dataset (to be used later) within Shiny app

I need a dataset to be continuously updated once the user clicks on a button. I was hoping the below code to achieve that but it simply ignores the updating part in my code. Here is the code:

...

library(shiny)
rm(list = ls())

x = 0.1
y = 0.1
df = data.frame(x,y) #Just define a simple dataset


ui <- shinyUI(fluidPage(
  
  actionButton("run", "Run"),
  
        tableOutput('table')  
))

server <- shinyServer(function(input, output, session) {
  
  df_new = eventReactive(input$run, {
    
 
      
      z = runif(2)

      if(isFALSE(exists("df_new()"))){ #Check if there is new data

        return(rbind(df,z)) #1st update
      }
      else{
        
        return(rbind(df_new(),z)) #Update the dataset
        
        }
      
    })
    
    
   
    output$table = renderTable({
      
              
             df_new()
              
    })

  
  
  
})

shiny::shinyApp(ui, server)

I want the app to add a new row to the previous ones each time we run it, and so the number of rows should be always #clicks + 1. Any idea if that's possible?

df_new should be a reactiveVal

1 Like

It is solved:

library(shiny)

x = 0.1
y = 0.1
df = data.frame(x,y) #Just define a simple dataset


ui <- shinyUI(fluidPage(
  
  actionButton("run", "Run"),
  
  tableOutput('table')  
))

server <- shinyServer(function(input, output, session) {
  
  df_new = reactiveVal(df)
  
  observeEvent(input$run, {
    z = runif(2)
    df_new(rbind(df_new(),z))
  })

  output$table = renderTable({
    df_new()
  })
})

shiny::shinyApp(ui, server)
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.