0

I'm trying to include a dataset in a Shiny app that first gets read in and used as is, e.g. by displaying it as a table. I would like to allow the user to then be able to manipulate this dataset and update the same table output with the updated dataset.

I can get both parts to work separately - I can display the original data, and I can display reactive updated data. But I can't figure out how to get both to work using the same dataset? The below code is a simple example using iris, with an attempt to display the original dataset and then rbinding it so there are twice as many rows to display in the updated dataset when you hit 'Run'. Note that I've converted the data to data.table because my actual code will be using data.table a lot.

library(shiny)
library(data.table)

iris <- as.data.table(iris)

ui <- fluidPage(
    fluidRow(column(4, actionButton("run", "Run"))),
    fluidRow(column(12, tabPanel(title = "tab1",
        DT::dataTableOutput("table1"))))
    )

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

    irisdata <- reactive({
        irisdata <- iris
    })

    irisdata <- eventReactive(input$run, {
        rbind(irisdata(), iris, fill = TRUE)
    })

    output$table1 <- DT::renderDataTable({
        irisdata()
    })

}

shinyApp(ui, server)

The rbind results in: Error in : evaluation nested too deeply: infinite recursion / options(expressions=)?

Which is to be expected I suppose as it's self-referencing, but I can't figure out how to write the code otherwise?

PeterJ
  • 79
  • 5
  • Hi, the solution is to use `reactiveValues` with `observe` instead of `reactive`. See [this thread](https://stackoverflow.com/questions/56946014/previous-input-in-shiny/56951840#56951840) for more info. It's really similar to your problem – GyD Jul 10 '19 at 13:06
  • That was helpful, thanks for the link. This thread was also helpful: https://stackoverflow.com/questions/50251813/how-to-update-datatable-in-shiny-with-button – PeterJ Jul 11 '19 at 10:33

1 Answers1

0

Working code of the above example, based on the linked threads in the comments:

library(shiny)
library(data.table)

iris <- as.data.table(iris)

ui <- fluidPage(
    fluidRow(column(4, actionButton("run", "Run"))),
    fluidRow(column(12, tabPanel(title = "tab1",
                           DT::dataTableOutput("table1"))))
)

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

    irisdata <- reactiveValues(data = iris)

    observeEvent(input$run, {
        irisdata$data <- rbind(irisdata$data, iris, fill = TRUE)
    })

    output$table1 <- DT::renderDataTable({
        irisdata$data
    })

}

shinyApp(ui, server)
PeterJ
  • 79
  • 5