Looking at Daily Log Returns with tidyquant, TidyDensity, and Shiny

[This article was first published on Steve's Data Tips and Tricks, 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 blog post, we’ll walk through how to create a shiny application that allows users to analyze the weekly returns of FAANG stocks (AAPL, AMZN, FB, GOOGL, and NFLX) using the {tidyquant} and {TidyDensity} packages in R.

Section 1: Package and UI Setup

The first section of the code sets up the necessary R packages and creates the UI for the shiny app. The packages we’ll be using are:

  • shiny: for creating interactive web applications in R
  • tidyquant: for easily getting and analyzing financial data in R
  • TidyDensity: for computing and visualizing probability distributions in a tidy way
  • dplyr: for manipulating data in a tidy way
  • DT: for creating interactive and scrollable data tables

Analysts assemble your packages!


The UI consists of a title panel, a sidebar panel, and a main panel. The sidebar panel contains a select input that allows users to choose which FAANG stock to analyze, as well as a numeric input for the number of simulations to run. The main panel contains two sections: one for the tidy_autoplot() output (a plot of the stock returns), and one for the tidy_empirical() output (a table of the log returns).

Section 2: Server Setup

The second section of the code defines the server function for the shiny app. The server function takes the input values from the UI (i.e. the selected stock and number of simulations) and uses them to get and analyze the stock data.

To get the stock data, we use the tq_get() function from the tidyquant package to retrieve the adjusted stock prices for the selected security from January 1, 2010 to the present. We then use the tq_transmute() function to compute the weekly log returns of the stock and rename the resulting column to “log_return”.

The tidy_empirical() function from the TidyDensity package is used to compute the empirical distribution of the log returns. The resulting table is displayed using the renderDT() function from the DT package, which creates a scrollable data table that can be sorted and filtered.

The tidy_autoplot() function is used to create a plot of the log returns, which is displayed using the renderPlot() function.

Section 3: Running the App

The final section of the code runs the shiny app using the ui and server functions.

Overall, this shiny app provides a simple and interactive way for users to analyze the weekly returns of FAANG stocks using tidyquant and TidyDensity in R. By allowing users to choose which stock to analyze and how many simulations to run, the app provides a customizable way to explore the empirical distributions of the log returns.


Here is a full working example, except for FB for some reason that symbol errors out for me, go figure:


# define UI
ui <- fluidPage(
  titlePanel("FAANG Stock Analysis"),
      selectInput("ticker", "Select a security:", choices = c("AAPL", "AMZN", "FB", "GOOGL", "NFLX"), selected = "AAPL"),
      numericInput("num_sims", "Enter the number of simulations:", value = 1, min = 1, max = 25, step = 1)
      h3("Tidy Autoplot Output"),
      h3("Tidy Empirical Output"),

# define server
server <- function(input, output) {
  # get stock data using tq_get and tq_transmute
  stock_data <- reactive({
    tq_get(input$ticker, get = "stock.prices", from = "2010-01-01") %>%
      tq_transmute(select = adjusted, mutate_fun = periodReturn, period = "weekly", type = "log", col_rename = "log_return")
  # output tidy_empirical results
  output$empirical_output <- renderDT({
      tidy_empirical(stock_data()$log_return, .num_sims = input$num_sims),
      options = list(scrollY = "250px")
  # output tidy_autoplot results
  output$autoplot_output <- renderPlot({
    stock_data()$log_return %>%
      tidy_empirical(.num_sims = input$num_sims) %>%

# run the app
shinyApp(ui = ui, server = server)


To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

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)