How to create an interactive booklist with automatic Amazon affiliate links in R?

Antoine Soetewey 2019-12-26 8 minute read


Booklists are a useful way to share the books you have read and which you recommend to other readers and/or to promote the books you have written. It can be as simple as a list of book titles displayed on your personal website or blog. You may however wish to present this list of books in a more sophisticated way, such as in a table with titles, authors and links to buy it for instance.

In this blog post I show you how to create an interactive booklist with automatic Amazon affiliate links in R. By interactive, I mean a booklist which allows users to search for books by title or author (like this booklist for instance). Moreover, by automatic Amazon affiliate links, I mean URLs (with your affiliate link of course) that redirect directly to the book in question on the Amazon webstore, without manually creating a link for each book.

This technique is especially helpful for those of you who have hundreds of books in their list as you will need to create the URL only once and it will adapt automatically to all books.


In order to build this augmented booklist with your affiliate link, you need:

  • an Amazon associates account. Register here if you do not have an account yet
  • a list of books (which you recommend and/or written by you)

Create a booklist

You have two options:

  1. create it in Excel then import it into R
  2. create it directly in R

Create it in Excel then import it

The easiest way is to follow these steps:

  1. open an Excel file and fill it in with two columns: (i) one with the titles and (ii) a second with the authors (see figure below)
  2. save it in a .csv format (in Excel, File > Save As… > choose the CSV file format and save it)
  3. import it into R (see how to import a .csv file if you struggle with the importation)
  4. (if you need to edit the list in the future, edit it directly in the .csv file and not in the Excel file)

Here is how you booklist created in Excel should look like (with I suppose more books in yours):

Step 1: Booklist created in Excel

We now import it into RStudio and rename the dataset as dat (see here why I always use a generic name instead of more specific names):

dat <- read.csv("booklist.csv", # name of your file with .csv extension
  header = TRUE, # names of variables are present
  sep = "," # values are separated by a comma

You can always check that your booklist is correctly imported by running head(name_of_dataset) or View(name_of_dataset).

Create it directly in R

You can create your booklist directly in R with the command data.frame():

# Create the data frame named dat
dat <- data.frame(
  "Title" = c(
    "A Random Walk Down Wall Street",
    "Naked Statistics",
  "Author" = c(
    "Burton G. Malkiel",
    "Charles Wheelan",
    "Steven D. Levitt and Stephen J. Dubner"
  stringsAsFactors = FALSE

# Print the data frame
##                            Title                                 Author
## 1 A Random Walk Down Wall Street                      Burton G. Malkiel
## 2               Naked Statistics                        Charles Wheelan
## 3                   Freakonomics Steven D. Levitt and Stephen J. Dubner

Make it interactive

In order to be able to search for books by author or title, we use the datatable() command from the DT package. Below the table with the default options:


Let’s improve this table by:

  • removing row numbers
  • adding a filter on top of “Title” and “Author” columns
  • adding the possibility to copy or download the table
  • show only first 5 entries instead of 10
  • order books by title in ascending order
  rownames = FALSE, # remove row numbers
  filter = "top", # add filter on top of columns
  extensions = "Buttons", # add download buttons
  options = list(
    autoWidth = TRUE,
    dom = "Blfrtip", # location of the download buttons
    buttons = c("copy", "csv", "excel", "pdf", "print"), # download buttons
    pageLength = 5, # show first 5 entries, default is 10
    order = list(0, "asc") # order the title column by ascending order

Final result

Check that everything works properly by clicking on different links. If you did not miss any steps, it should redirect you to the Amazon store with the title and author of the book in the search bar and thus the book in question appearing in the search results.


Thanks for reading.

I hope this article helped you to build an interactive booklist with an automated affiliate link to each of the book in your list.

As always, if you have a question or a suggestion related to the topic covered in this article, please add it as a comment so other readers can benefit from the discussion.

Liked this post?

  • Get updates every time a new article is published (no spam and unsubscribe anytime):

  • Support the blog
  • Share on: