Editable DataTables for Shiny Applications

[This article was first published on R on Jason Bryer, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

RStudio recently updated Shiny to allow for editable DataTables. Their implementations allows for editing cells direclty with in the DataTable view. This is fine for many advanced applications, however, for many applications more fine tuned control of what the user can edit is necessary. For example, you may want to only allow a subset of columns to be editable. Or you want to view a subset of columns in a spreadsheet view but allow other columns to be editable. The DTedit package takes the editing out of the table view and instead presents the user with a modal dialog for editing table contents (see screenshot below).

To get started, use the devtools package to install the latest development version of DTedit:

devtools::install_github('jbryer/DTedit')

The dtedit_demo will run a sample shiny app with to editable data tables.

DTedit::dtedit_demo()

DTedit Screen Shot

Getting Started with DTedit

You can download a simple shiny app using DTedit from Github.

There are three steps to using DTedit in your shiny application.

1. Define callback function for inserting, updating, and deleting data.

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

Typically these functions would interact with a database. As written here, the data would be lost between shiny sessions.

2. Create the dtedit object within your server function.

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'),
	   callback.update = my.update.callback,
	   callback.insert = my.insert.callback,
	   callback.delete = my.delete.callback)

The input and output are passed from the server function. The name parameter will define the name of the object available to the uiOutput. The thedata is a data.frame for the initial view of the data table. This can be an empty (i.e. no rows) data.frame. The structure of the data.frame will define the inputs (e.g. factors will be drop down, Date will use dateInput, numerics will use numericInput, etc.). There are many other parameters to custom the behavior of dtedit, see ?dtedit for the full list.

3. Use uiOutput in your UI to display the editable data table.

The name you will pass to uiOutput is the name you passed to the dtedit created on the server side.

uiOutput('mycontacts')

To leave a comment for the author, please follow the link and comment on their blog: R on Jason Bryer.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)