我在闪亮的r

时间:2019-09-02 04:12:09

标签: r shinydashboard

my.callback.updatemy.callback.insertmy.callback.delete函数不起作用,并且编辑后的表也不会保存。

我试图用闪亮的r制成

app.R

library(shiny)

library(shinydashboard)

library(DTedit)

ui <- dashboardPage(

  dashboardHeader(title = "EditDatatable"),

  dashboardSidebar(

    sidebarMenu(

      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard"))

      )
  ),

  dashboardBody(

    tabItems(

      tabItem(tabName = "dashboard",

       fluidPage(

         fluidRow(

           uiOutput('mycontacts') 

         )

       )

      )

    )     

  )

)

server <- function(input, output) { 

  load("D:\\editDatatable/mydata.RData")

 my.insert.callback <- function(data, row) {
    mydata <- rbind(data, mydata)
    return(mydata)
  }

  my.update.callback <- function(data, olddata, row) {
    mydata[row,] <- data[1,]
    return(mydata)
  }

  my.delete.callback <- function(data, row) {
    mydata[row,] <- NULL
    return(mydata)
  }

  # save(mydata, file = "D:\\editDatatable/mydata.RData")

  output$mycontacts <- renderUI(

    DTedit::dtedit(input, output, 

                  name = 'mycontacts',

                  thedata = mydata,

                  edit.cols = c('name', 'email', 'useR', 'notes'),

                  edit.label.cols = c('Name', 'Email Address', 'Are they an R user?', 'Additional notes'),

                  input.types = c(notes='textAreaInput'),

                  view.cols = c('name', 'email', 'useR', 'notes'),

                  my.callback.update = my.update.callback,

                  my.callback.insert = my.insert.callback,

                  my.callback.delete = my.delete.callback)

)}

shinyApp(ui, server)

结果我必须服用:

https://www.r-bloggers.com/editable-datatables-for-shiny-applications/

请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:0)

不幸的是,目前(2020年6月21日)显示的文档on the README of jbryer/DTedit误导了data / olddata / row的内容,如某些对JBryer's blog page about DTedit的评论。

这里是my current modified version of DTedit中所述的修订文档,描述了实际上包含的data / olddata / row

my.insert.callback <- function(data, row) {
  # 'data' contains the dataframe *after* the row has been inserted/added
  # 'row' is the row number where data has been inserted
  mydata <<- rbind(mydata, data[row,])
  # in this case, 'mydata' should just be the same as 'data'
  return(mydata)
}

my.update.callback <- function(data, olddata, row) {
  # 'data' contains the dataframe *after* the row has been updated
  # 'row' is the row number where data has been updated
  # 'olddata' is the previous version of the data
  mydata[row,] <<- data[row,]
  # in this case, 'mydata' should just be the same as 'data'
  return(mydata)
}

my.delete.callback <- function(data, row) {
  # 'data' contains the dataframe *before* the row has been deleted
  # 'row' is the row number where data is to be deleted
  mydata <<- mydata[-row,]
  # in this case, 'mydata' should just be the same as data[-c(row),]
  return(mydata)
}

dtedit_demo of jbryer/DTedit中显示了正确使用回调函数的示例。

my vignette of DTedit on RPubs中显示了更多示例,但是其中许多示例是特定于我对DTedit所做的修改的。