r - 如何在 Shiny 中使用可编辑的 DataTable 作为另一个 DataTable 的输入

标签 r shiny dt

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

用户界面

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"))
      )
    )
  )
))

服务器文件
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 包裹:

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 社区查看我的回答 here .

关于r - 如何在 Shiny 中使用可编辑的 DataTable 作为另一个 DataTable 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49956418/

相关文章:

R ggplot2 : Setting additional specific axis tick marks

r - 在具有多个条目的 R Shiny 数据表中搜索列

javascript - 如何将 R 包 (DT) 中的数据表包含到另一个 HTML 文件中

R plotly悬停标签文本对齐

r - Shiny 的渲染UI错误

r - 在 R Shiny 应用程序中使用滚动条绘制大图

css - R shiny - 侧边栏面板的背景

r - stateSave 不保留 Shiny 数据表中的过滤器

r - 在 Shiny 的 DataTable 中包含本地 html 文件的链接

r - 增加 ggplot 中的绘图区域以处理绘图边缘的 geom_text