<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/editable-datatables-for-shiny-applications/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.bryer.org/post/2018-22-26-dtedit/"> R on Jason Bryer</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p><a rel="nofollow" href="https://rstudio.com/" target="_blank">RStudio</a> recently updated <a rel="nofollow" href="https://shiny.rstudio.com/" target="_blank">Shiny</a> to allow for <a rel="nofollow" href="https://blog.rstudio.com/2018/03/29/dt-0-4/" target="_blank">editable <code>DataTables</code></a>. Their implementations allows for editing cells direclty with in the <code>DataTable</code> 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 <code>DTedit</code> package takes the editing out of the table view and instead presents the user with a <a rel="nofollow" href="https://shiny.rstudio.com/articles/modal-dialogs.html" target="_blank">modal dialog</a> for editing table contents (see screenshot below).</p>
<p>To get started, use the <code>devtools</code> package to install the latest development version of <code>DTedit</code>:</p>
<pre><code>devtools::install_github('jbryer/DTedit')
</code></pre>
<p>The <code>dtedit_demo</code> will run a sample <code>shiny</code> app with to editable data tables.</p>
<pre><code>DTedit::dtedit_demo()
</code></pre>
<p><img alt="DTedit Screen Shot" src="https://i1.wp.com/raw.githubusercontent.com/jbryer/DTedit/master/inst/screens/dtedit_books_edit.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h2>Getting Started with <code>DTedit</code></h2>
<p>You can download a simple shiny app using <code>DTedit</code> <a rel="nofollow" href="https://github.com/jbryer/DTedit/blob/master/inst/template/app.R" target="_blank">from Github</a>.</p>
<p>There are three steps to using <code>DTedit</code> in your shiny application.</p>
<h4>1. Define callback function for inserting, updating, and deleting data.</h4>
<pre><code>my.insert.callback &lt;- function(data, row) {
	mydata &lt;- rbind(data, mydata)
	return(mydata)
}

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

my.delete.callback &lt;- function(data, row) {
	mydata[row,] &lt;- NULL
	return(mydata)
}
</code></pre>
<p>Typically these functions would interact with a database. As written here, the data would be lost between shiny sessions.</p>
<h4>2. Create the <code>dtedit</code> object within your <code>server</code> function.</h4>
<pre><code>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)
</code></pre>
<p>The <code>input</code> and <code>output</code> are passed from the <code>server</code> function. The <code>name</code> parameter will define the name of the object available to the <code>uiOutput</code>. The <code>thedata</code> is a <code>data.frame</code> for the initial view of the data table. This can be an empty (i.e. no rows) <code>data.frame</code>. The structure of the <code>data.frame</code> will define the inputs (e.g. <code>factor</code>s will be drop down, <code>Date</code> will use <code>dateInput</code>, <code>numeric</code>s will use <code>numericInput</code>, etc.). There are many other parameters to custom the behavior of <code>dtedit</code>, see <code>?dtedit</code> for the full list.</p>
<h4>3. Use <code>uiOutput</code> in your UI to display the editable data table.</h4>
<p>The <code>name</code> you will pass to <code>uiOutput</code> is the name you passed to the <code>dtedit</code> created on the server side.</p>
<pre><code>uiOutput('mycontacts')
</code></pre>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.bryer.org/post/2018-22-26-dtedit/"> R on Jason Bryer</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/hqARBDS1L9c" height="1"></a></div>