Three R Shiny tricks to make your Shiny app shines (3/3): Buttons to delete, edit and compare Datatable rows

March 1, 2017

[This article was first published on R – Enhance Data, 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.

In this tutorials sequence, we are going to see three tricks to do the following in a Shiny app:

  1. Add Next and Previous buttons to navigate in a tabBox
  2. Build a non completely collapsible sidebar to keep the icon visible on collapse
  3. Add button on a datatable output to delete/modify/ do an action on a given row.

Today, we are going to see how to add buttons in each rows of a datatable to delete, edit or compare it with other rows.
The app is live here.

3. Buttons to delete, edit and compare Datatable rows

Here is what we want to achieve:

The final application

As you can seen, the final application offer a lot of functionalities, like selecting row with checkboxes, comparing, deleting them and also modifying them.
Even though you can select the rows via the classical datatable functions, checkboxes make it more visual for the users (he knows there is something it can do). In addition to this, the button are a clear call to action so that the users do not have to serach how to do the different actions.

a.It all begins with a data.table (or a datatable)

To begin with, we will only display a classic data.table, generate some fake datas in our app.



dashboardPage(dashboardHeader(disable = T),
 dashboardSidebar(disable = T),

The ui is very classic, since most of the content is dynamic, we will generate the page and elements in the server.



shinyServer(function(input, output) {

###Creating reactive values since we will need to modify the table with events and triggers

##The Body is classic, we just used the group button to improve the render
##And to show the user the actions are related
     h3(strong("Actions on datatable with buttons"),align="center"),
     column(6,offset = 6,
##Grouped button
       HTML('') ), 
##Rendering the datatable 
column(12,dataTableOutput("Main_table")) }) 
##The code may seem weird but we will modify it later 
output$Main_table=renderDataTable({ DT=vals$Data datatable(DT)} )


Data generation: The apps create fakes yearly sales data for 10 brands, with a name, an email and a fake estimated growth. The data is purely cosmetic.

Main Body: The body contains a title, three buttons which are grouped using the html div btn-group. When Shiny generates the html code, the div will encapsulate the action buttons which will be showed as grouped.

Storing the table in a reactive value: Since we will want to perform action on the data with buttons and eventObserver the modifications of the data will be done with side effects. Hence reactiveValues are a must.

b. On which we added some buttons

Now, lets add the buttons to our datatable. Basically, they will be added through html code and the datatble will be asked not to escape strings.



') datatable(DT, escape=F)} )

Checkboxes: The checkboxes will be used to select the rows on which the user want to perform some actions (Deletion, comparison). Since they all have the same name it will be easy to access their value through JS. We will get the value of the row by deleting the ‘Row_’ from the checkbox value.

Actions Buttons: Each row will contain a group of two buttons, one to delete the row and the other one to modify it. Again, the app will access the row to delete and the action to do through the button ID.

Escape: Perhaps you noticed the escape=F option, by using it, datatable will render the buttons according to their HTML codes instead of strings.

c. But these buttons were doing nothing.

No matter you click on the buttons or not, nothing will happen. They need to be linked with the Shiny App. First, let’s link the new row button.

We will just add an event observer in the server which will add a new row to vals$Data

 Contact="[email protected]")



The observer is just generating a new row and appending it to vals$Data.

d. Getting the selected row/ticked box in Shiny.

Now, to be able to compare the selected row and to delete them, we need to create a new Shiny input with the list of selected checkbox.

This can be done adding a js script at the end of output$MainBody

 tags$script(HTML('$(document).on("click", "input", function () {
var checkboxes = document.getElementsByName("row_selected");
var checkboxesChecked = [];
for (var i=0; i

Line by line:
First we are adding a new script, the HTML() is used to avoid escaping, which may lead to some weird stuff with the ‘>’.
Then when the user is clicking on an input, the script runs a function.
We are storing all the checkboxes and we create an empty arrow that will be filled with the id of ticked checkbox.
The last line is assigning the id of checked boxes to input$checked rows.

e. Now, let’s delete and compare rows

Now, the app can access the list of checked rows. Let’s delete these ones through an observe event (to be added in the shinyServer function)



Nothing fancy in it, the value of rows to delete is gotten by deleting the “Row” part of the input. Then, the value of vals$Data is updated.
To compare the rows, the code is similar.



As previously, the observer get the selected rows, then some fakes data are generated and the modal containing the plot is shown. The modal is generate using the code below:

h3(strong("Monthly sales of selected brands"),align="center"),


The plot needs to be outside of the modal since it is dependent on vals$fake_sales which is reactive.

Now when you click on the compare or delete selected rows, the actions should happen correctly.

f. Inrow button deletion.

Now, let’s focus on deleting a row with the inrow button. As previously, we first need to link the button with a Shiny Input, this will be done through a small JS script  to add at the end of output$MainBody

tags$script("$(document).on('click', '#Main_table button', function () {
 Shiny.onInputChange('lastClick', Math.random())

When a button in output$Main_Table is clicked, input$lastClickId got assigned the id of this button and input$lastClick  got assigned a random value.
The last click is used to detect the click (for instance when a button is clicked twice, the id won’t change and hence cannot be observed).
The app will observe input$lastClick:

 if (input$lastClickId%like%"delete")
 else if (input$lastClickId%like%"modify")

Below is the structure of the modal, it is basically a page with a data.table and an action button to trigger the save of the modification.

h3(strong("Row modification"),align="center"),
actionButton("save_changes","Save changes"),


The process to delete the row is the same as with checkbox selection. We are only keeping the end of the id which contains the number of the row to delete.

g.One modal to modify them all.

Now, we want to be able to modify a given line of the data.table. To do see, we will pop a modal with text field when the user click on the modify button.

The modal to modify a row

The datatable inside has the following structure:

for (i in colnames(old_row))
if (is.numeric(vals$Data[[i]]))
rownames(DT)<-c("Current values","New values")



So, we are putting the old values in the first row and the second is composed of numerical and text input. Their id is new_name_of_the_col, we will use this id to match it with the colnames later on.
Again, Shiny should not escape the characters,  the other options are just here to improve the render (not search field).

h.How to catch these new fields ?

Now that the fiels are created Shiny need to be able to detect them and use them as an input. As previously, we’ll create a custom shiny input with js:

 tags$script(HTML("$(document).on('click', '#save_changes', function () {
var list_value=[]
for (i = 0; i < $( '.new_input' ).length; i++)
list_value.push($( '.new_input' )[i].value)
Shiny.onInputChange('newValue', list_value)

What is the script doing ? When the user click on the action button save_changes, it will collect all the input of class .new_input (the class we set for the new fields) and push them in a list. This list will be assigned to input$newValue .

Input$new_value will hence be observed in Shiny:

newValue=lapply(input$newValue, function(col) {
if (suppressWarnings(all(! {
} else {
DF=data.frame(lapply(newValue, function(x) t(data.frame(x))))


The first lapply is used to cast the input to the right type (in our case numeric or character). Then, it is put in a data.frame with appropriate colnames. Finally, we can get the proper row to change by using the last modify button the user clicked on.

Here we are, the apps should work perfectly now !

You can find the code HERE.

Thanks for reading the tutorial,

You can follow me on Twitter:


To leave a comment for the author, please follow the link and comment on their blog: R – Enhance Data. 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.

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


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)