Reputation: 45
I am trying to append rows to a dtedit object using a seperate actionbutton. The button creates a row and I want it appended to the dtedit table. I have it working so that this all happens, but the dtedit function doesn't seem to recognize the data, because when I try using the "Edit" button that's built in, the fields don't pre-populate in the modal as they should.
I have included a reproducible example below. You will see that the "Add Row" functionality works, but when you click on a row and press edit, the modal does not recognize the data.
*Edited with DTedit
package load and the removal of view.label.cols
devtools::install_github('jbryer/DTedit')
library(DTedit)
ui <- fluidPage(
actionButton('addrow', "New Row"),
uiOutput("table")
)
server <- function(input, output) {
df <- data.frame('Order' = integer(), 'Category' = character(), 'Name' = character(), stringsAsFactors = F)
table.reactive <- reactiveVal(df)
dtedit(input, output,
name = 'table',
thedata = df,
edit.cols = c('Order', 'Category', 'Name'),
edit.label.cols = c('Order', 'Category', 'Name'),
input.types = c( Order = 'numericInput',
Category='textInput',
Name='textInput'
),
view.cols = c('Order', 'Category', 'Name'),
callback.update = plandrill.update.callback,
callback.insert = plandrill.insert.callback,
callback.delete = plandrill.delete.callback,
show.copy = F, show.insert = F)
dt.proxy <- DT::dataTableProxy("tabledt")
## append row
observeEvent(input$addrow, {
row <- data.frame(nrow(table.reactive()) + 1, "ABC", "DEF", stringsAsFactors = F)
appended_table = rbind(table.reactive(), row)
table.reactive(appended_table)
replaceData(dt.proxy,
appended_table,
rownames = FALSE)
})
}
shinyApp(ui = ui, server = server)
Upvotes: 0
Views: 288
Reputation: 546
jbryer/DTedit
makes a local copy of the dataframe passed through the thedata
argument, and stores it internally in results$thedata
. results$thedata
is used for subsequent datatable editing and is not changed by replaceData
.
The effect you require can be achieved through a modified version of DTedit
, which I wrote (with the help of others) and is available on Github at DavidPatShuiFong/DTedit
.
library(DTedit) ui <- fluidPage( actionButton('addrow', "New Row"), uiOutput("table") ) server <- function(input, output) { df <- data.frame('Order' = integer(), 'Category' = character(), 'Name' = character(), stringsAsFactors = F) table.reactive <- reactiveVal(df) dtedit(input, output, name = 'table', thedata = table.reactive, edit.cols = c('Order', 'Category', 'Name'), edit.label.cols = c('Order', 'Category', 'Name'), input.types = c( Order = 'numericInput', Category='textInput', Name='textInput' ), view.cols = c('Order', 'Category', 'Name'), # callback.update = plandrill.update.callback, # callback.insert = plandrill.insert.callback, # callback.delete = plandrill.delete.callback, show.copy = F, show.insert = F) dt.proxy <- DT::dataTableProxy("tabledt") ## append row observeEvent(input$addrow, { row <- data.frame(Order = nrow(table.reactive()) + 1, Category = "ABC", Name = "DEF", stringsAsFactors = F) appended_table = rbind(table.reactive(), row) table.reactive(appended_table) }) } shinyApp(ui = ui, server = server)
In this version of DTedit
, the dataframe is passed as a reactiveVal
(table.reactive
), and DTedit
's copy changes when table.reactive
changes.
I needed to disable defining the various callbacks in your example, as the callbacks were not defined. I also need to be explicit about labeling the columns in the row
dataframe.
Further examples can be seen in a vignette, and linked through README.md on Github.
Upvotes: 1