Editable DataTables for Shiny Applications

November 26, 2018
By

(This article was first published on R on Jason Bryer, and kindly contributed to R-bloggers)

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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Sponsors

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)