A new RStudio addin to facilitate inserting tables in Rmarkdown documents

[This article was first published on Posts on Lorenzo Busetto Website & Blog, 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 the last months, I started increasingly using Rmd documents for preparing scientific reports, blog posts, etcetera. While I really like the flexibility offered by the system, one thing that I thought could be improved is the support for easily inserting tables. So, “inspired” also by the recent addition of the excellent insert image addin in blogdown, I decided to give it a go and try to implement some kind of addin to facilitate table insertion in Rmd documents.

After struggling a bit due to my rather nonexistent shiny skills, in the end I managed to obtain a “basic but useful” (IMO) addin. Let’s see how it works:

Installation

You can install the addin from GitHub with:

# install.packages("devtools")
devtools::install_github("lbusett/insert_table")

Usage

Upon installing, inserttable registers a new RStudio Addin (Insert Table) that can be used to easily insert a table in a Rmd document. To use it, open a Rmd document and, with the cursor within a r chunk and select “Addins –> Insert Table”.

There are two main use modes:

Launch the addin while the cursor is on a empty line

In this case, a GUI will open allowing you to select the desired output format ( kableExtra, DT and rhandsontable are currently implemented), and to edit the content of the table. After clicking Done the Addin will add in the Rmd document the code needed to generate the table in a nice tribble format (thanks to Miles McBain’s datapasta package!) to allow easier editing, and also the code needed to render it with the selected output format using some default options, as can be seen below:

A useful feature is that, for larger tables, you can also cut and paste content from a spreadsheet :

Obviously, rendering of the table can be tweaked further by changing/adding arguments of the rendering functions in the automatically generated code.

Launch the addin while selecting the name of a variable

In this case, the GUI allows you to select only the desired output format ( it is assumed that the variable you select corresponds to a data frame or similar object containing the data you wish to show as table). After clicking Done the Addin will add in the Rmd document the code needed to render the selected variable as a table with the selected output format. The code will be added at the first empty line below that containing the name of the selected variable.

IMPORTANT NOTE: inserttable will make no effort to guarantee that the variable you select is a data.frame. It is up to you to select a meaningful variable!

Usage from the console

You can also use (part of) inserttable functionality from the console by calling function insert_table().

> insert_table(tbl_name = "table_1", nrows = 4, ncols = 4, tbl_format = "DT")

The function will return to the console the code needed to create a empty table of the specified dimensions and render it with the selected format:

That’s all!.

I hope someone else will find this useful!

To leave a comment for the author, please follow the link and comment on their blog: Posts on Lorenzo Busetto Website & Blog.

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)