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

ぐ巨炮叔叔 提交于 2020-01-23 09:50:09

问题


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())
  })

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

})

回答1:


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.



来源:https://stackoverflow.com/questions/49956418/how-to-use-an-editable-datatable-in-shiny-as-input-for-another-datatable

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