Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use an editable DataTable in Shiny as input for another DataTable

Tags:

r

dt

shiny

I want the user to be able to edit the already-loaded DataTable, click a button and then have the edited version used as input to do stuff. So in this example, how could I make the new user-edited versions appear in the "New" tabs upon clicking of the "Change Dataframes" button?

User Interface

shinyUI(fluidPage(

  titlePanel(),


  sidebarLayout(


    sidebarPanel(
      actionButton("runButton","Change Dataframes")
    ),

    mainPanel(
      tabsetPanel(
        tabPanel("OldIrisTab",
                 DT::dataTableOutput("OldIris")),
        tabPanel("OldPetrolTab",
                 DT::dataTableOutput("OldPetrol")),
        tabPanel("NewIrisTab",
                 DT::dataTableOutput("NewIris")),
        tabPanel("NewPetrolTab",
                 DT::dataTableOutput("NewPetrol"))
      )
    )
  )
))

Server file

shinyServer(function(input,output){


  output$OldIris <- DT::renderDataTable({
    datatable(iris,editable=T)
  })

  output$OldPetrol <- DT::renderDataTable({
    datatable(petrol,editable=T)

  })

  ######
  # HERES WHERE I'M NOT REALLY SURE WHAT TO DO 

  change_data1 <- eventReactive(input$runButton, {
    withProgress(message="Generating new dataframes",{

      newdf1 <- datatable(output$OldIris)
      newdf1

    })
  })

  change_data2 <- eventReactive(input$runButton, {
    withProgress(message="Generating new dataframes",{

      newdf2 <- datatable(output$OldPetrol)
      newdf1

    })
  })


  output$NewIris <- DT::renderDataTable({
    datatable(change_data1())
  })

  output$NewPetrol <- DT::renderDataTable({
    datatable(change_data2())
  })

  #######
  ######

})
like image 366
John F Avatar asked Jan 03 '23 12:01

John F


1 Answers

Using rhandsontable package:

library(shiny)
library(rhandsontable)
ui <- fluidPage(
  titlePanel("Ttile"),
  sidebarLayout(
    sidebarPanel(
      actionButton("runButton","Change Dataframes")
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("OldIrisTab", rHandsontableOutput('OldIris')),
        tabPanel("NewIrisTab", DT::dataTableOutput("NewIris"))
        ))))

server <- function(input,output,session)({
  values <- reactiveValues()
  output$OldIris <- renderRHandsontable({
    rhandsontable(iris)
  })

  observeEvent(input$runButton, {
    values$data <-  hot_to_r(input$OldIris)
  })

  output$NewIris <- DT::renderDataTable({
    values$data
  })

}) 
shinyApp(ui, server)

I hope it helps. You can also check my answer at Rstudio community here.

like image 146
A. Suliman Avatar answered Apr 12 '23 23:04

A. Suliman