R中的可编辑数据表

时间:2017-12-28 19:26:28

标签: r shiny shinyjs

我正在尝试编辑数据表并在点击操作按钮"更新表"后更新行编辑后的记录。如何在现有数据表记录中修改字段后反复检索/显示新数据表记录?

library(shiny)
library(shinyjs)
library(DT)
library(data.table)

mydata = data.frame(id=letters[1:5], val=sample(10,5,T))
lengthofData <- nrow(mydata)
mydata[["Constraint Type"]] <-  c(">")
))

mydata[["Constraint Value"]] <- c(1)

ui = fluidPage(dataTableOutput("table"),
               actionButton("goButton", "Update Table"),
               dataTableOutput("newtable"))

server = function(input,output){

  x <- mydata
  output$table <- renderDataTable( x,server = FALSE,
                                   escape = FALSE,
                                   selection = 'none')

  proxy = dataTableProxy('table')

  xNew<-reactiveValues()

  observeEvent(input$x1_cell_edit, {
    info = input$x1_cell_edit
    str(info)
    i = info$row
    j = info$col + 1
    v = info$value
    x[i, j] <<- DT:::coerceValue(v, x[i, j])
    replaceData(proxy, x, resetPaging = FALSE, rownames = FALSE)
    xNew<<-x
  })

  observeEvent(input$goButton,{
    output$newtable <- renderDataTable( xNew(),server = FALSE,
                                     escape = FALSE,
                                     selection = 'none')

  })

}

shinyApp(ui,server)

0 个答案:

没有答案