ifendo
ifendo

Reputation: 46

shiny datatable editable for reactive data source

I just wanted to make the data (displayed in the shiny datatable) selected by user editable and eventually to save the user's updates to a file (e.g. such as an excel file), see the testing code below:

I checked the example/questions below(and a few others), but none of those used the reactive as the data source.

https://yihui.shinyapps.io/DT-edit/

Edit datatable in shiny with dropdown selection (for DT v0.19)

DT: Dynamically change column values based on selectinput from another column in R shiny app

The issue for the code below is: when I try to update the second cell, the updates I made in the first cell disappeared.

library(shiny)
library(DT)
#> 
#> Attaching package: 'DT'
#> The following objects are masked from 'package:shiny':
#> 
#>     dataTableOutput, renderDataTable


ui <- fluidPage(
  selectInput("data", "Select a dataset:", choices = c("iris", "mtcars")),
  DTOutput("t1"),
  actionButton("save", "Save")
)

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

  data <- reactive({
    head(get(input$data))
  })

  output$t1 <- renderDT({
    data()
  }, editable = "cell")

  proxyt1 <- dataTableProxy("t1")

  observeEvent(input$t1_cell_edit, {
    info <- input$t1_cell_edit
    str(info)
    tempdf <<- editData(data(), info)
    # tempdf <<- editData(isolate(data()), info)

    replaceData(proxyt1, tempdf, resetPaging = FALSE)
  })

  # observeEvent(input$save, {
  #   writexl::write_xlsx(tempdf, "~/test.xlsx")
  # })
}

shinyApp(ui, server)
#> PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents

Created on 2022-05-23 by the reprex package (v2.0.1)

Upvotes: 1

Views: 165

Answers (0)

Related Questions