如何使用Shiny中的可编辑数据表作为另一个数据表的输入 [英] How to use an editable DataTable in Shiny as input for another DataTable

查看:177
本文介绍了如何使用Shiny中的可编辑数据表作为另一个数据表的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望用户能够编辑已经加载的DataTable,单击一个按钮,然后将编辑后的版本用作执行操作的输入。因此,在此示例中,如何在单击更改数据框按钮后使新的用户编辑版本出现在新建选项卡中?

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?

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

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

})


推荐答案

使用 rhandsontable 软件包:

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)

我希望它会有所帮助。您也可以在Rstudio社区的此处中查看我的答案。

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

这篇关于如何使用Shiny中的可编辑数据表作为另一个数据表的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆