Earlier in the year I deployed my first “finished” Shiny app, and it went pretty well! In an attempt to keep my Shiny eye in, I wanted to continue building apps to further develop those skills. I had also been collecting an ever-growing list of links to free R material (picked up from Twitter, blog posts etc), and I wanted a way of storing them for easy reference.
In an attempt to feed 2 birds with one scone, I have built an app that provides a fully searchable table of free R materials on the web. In this post I’ll walk through the app, which is built around the DT
package - a great way of creating interactive HTML tables that works nicely with shiny
.
Should you wish to dive straight in, the app lives here: https://committedtotape.shinyapps.io/freeR/
It’s a very simple app, with just the one interactive element: the table built with the DT
package.
The DT
package provides a convenient way in R to make interactive HTML tables. It’s an interface to the JavaScript library DataTables.
For getting started with the DT
package, look no further than the documentation from RStudio.
The key thing I wanted for my list of R materials was for it to be searchable, and DT
tables provide a search box as standard.
The default look of the DT
table is great, but I decided to tinker with it, along with the overall look of the app. In the end I chose a style similar to the look of this blog. These changes were made using a CSS file (see here). I won’t go through this now, partly because I want to focus on the main app code and partly because my CSS coding technique is largely built around trial & error/stealing stuff from StackOverflow (I’ll leave the CSS tutorials until I’m an expert!). But hopefully you can see that with some minimal CSS you can start making stylish apps.
All the code required to build the app can be found on my Github (more on that later).
The app.R program that builds the UI and Server for the app is extremely brief. It’s split into 3 sections:
Reads in the csv file (r_reading_list.csv
) that I’ve created containing a record for each book collected. Each record contains the following information: title, author, description, url and tags (e.g. keywords). Some work is required to set up the hyperlink for the title of the book:
# Read in csv file of resources compiled by me
reading_list <- read_csv("r_reading_list.csv")
# Create string for hyperlinks
reading_table <- reading_list %>%
mutate(title_link = paste0("<a href='",link,"' target='_blank'>",title,"</a>")) %>%
select(title_link, everything(), -title, -link)
The UI controls the look of the app. As I’ve said, it is very simple. After including the CSS from the styles.css
file, it sandwiches the DT
table between an intro and some credits. To set up where the table will be placed you need the DTOutput
function from shiny
, passing it the ID (labelled “tbl” here, to be referenced in the Server code):
ui <- fluidPage(
# formatting with css
includeCSS("styles.css"),
# Application title
h2(toupper("Free R Reading Material")),
# Intro text
p("A collection of books about the R programming language and Data Science, that you can read for free!"),
p("If there is a book that you think I should add to the list, then please let me know",
a("@committedtotape", href = "https://twitter.com/committedtotape")),
# DT table
DTOutput("tbl"),
br(),
# credits
div(p("Compiled by", a("@committedtotape", href = "https://twitter.com/committedtotape"), "using the shiny and DT packages"),
p("Blog:", a("davidsmale.netlify.com", href = "https://davidsmale.netlify.com/portfolio/")),
p("GitHub:", a("freeR", href = "https://github.com/committedtotape/freeR")),
style="text-align: right;")
)
The DTOutput
function used in the UI is paired with the renderDT
function in the Server. Within this function you call the main DT
function, datatable
, which will create our interactive HTML table.
The first argument to pass into datatable
is the dataframe created earlier. There are then a host of arguments and options that you can use, I’ve only utilised a handful here:
server <- function(input, output) {
# render DT table
output$tbl <- renderDT({
datatable(reading_table,
colnames = c('Title', 'Author(s)', 'Description', 'Keywords/Packages'),
rownames = FALSE,
escape = FALSE,
class = 'display',
options = list(pageLength = 20,
lengthMenu = c(10, 20, 50))
)
})
}
colnames
- Provide your own column names if you want them to appear different to what’s in the dataframe.rownames
- Do you want to include the row names? I don’t think I ever have!escape
- TRUE
by default, but I’ve set to FALSE
to enable the rendering of the hyperlinks in the table. Discussed in section 2.10 of the documentation.class
- This determines the styling of the table. I played around with a few different options but ended up back at ‘display’ which is the default.options
- A range of options can be passed in a list. Check them out here. I’ve just used pageLength
to set the number of rows displayed, and lengthMenu
to determine the drop down options of entries to be shown.That’s it! Told you it was brief. There is a lot more to DT
than what I’ve just been through, but hopefully this serves as a gentle introduction.
I’m now using Git/Github, so hooray for me! I mean, I can only do the basics of creating a repo, linking it to an RStudio project and committing and pushing code. But still, I feel a little more data science-y now.
I started taking the Coursera Data Science Specialisation course in April, and the first course, The Data Scientist’s Toolbox has a section on version control with Git/Github. I had been putting off using it for sometime, but finally had to bite the bullet and the videos in the course talk you through set up and the main concepts. This, along with Jenny Bryan’s excellent book Happy Git and GitHub for the useR (naturally listed in the app!), have helped me incorporate the basics into my workflow and I feel a lot better for it!
I plan to set up a repo for all projects going forward, along with some of my previous projects.
I’ve already had a few people forking the repo for this app, and had my first pull request, from Matthias Grenié adding 2 books to the list, which I happily merged! In a very small way I feel like I’m now contributing to open-source and it feels good! (I just need to not get obsessed by the little green squares).
After posting the app on Twitter I’ve had several people provide suggestions, which is great! I’ve added the majority of them now I think. Please get in touch if I’ve missed anything.
I'm compiling a list of #rstats books freely available online. Mainly for my own reference, but may be useful to others! Here's what I have so far, in a #shiny app! Thanks, as always, to the #rstats community for the great resources! https://t.co/UAZ6JI50sD pic.twitter.com/RRlNbachkk
— David Smale 🔎 (@committedtotape) May 24, 2019
As mentioned earlier, you can make a pull request to add books I’ve missed to the csv file for inclusion in the app. Just follow the format of the existing records. Check the README.md for details. To the README I’ve also added a list of all the fine people whose suggestions have been added to the app. Thanks!
There have been several books suggested that I hadn’t come across before and I’m looking forward to getting into them.
I am constantly amazed by all the learning resources out there, created by leading members of the R community and provided free of charge. If you find a book particularly useful, why not let the author know, tweet about it, or write a blog post about it!
And if, like me, you still enjoy the feel of a physical, real-life book, why not buy a copy!