R, Shiny, Rmarkdown Dashboard Tutorial with Cryptocurrency Data Example
This post is intended for those with some exposure to R and shiny. If you are brand new to Shiny or Rmarkdown, then you may want to review this post before proceeding onward.
I'll address the following:
This code is not in my .rmd file. I like to use .rda files so I don't have to do any extra work when I read it in. Just makes the .rmd file feel less encumbered, I guess. Not necessary to use .rda file, of course. When pulling external data into your rmarkdown document, the working directory (place where the program looks for files and exports them) will automatically be in the same location as your .rmd file.
One thing that has always annoyed me about Shiny is the document margins are automatically enforced on every document to be a certain width. On larger screens it isn't a big deal but on small screens it starts to look overly narrow and often disrupts rendered plots. The following code adjusts the default width to automatically respond to the width of the computer screen viewing it.
This goes into the main-container div of the unseen inner-workings of the shiny html code and makes that adjustment. You could also insert your own custom margin width inside the curly brackets if you wanted to. You need to use the renderUI function to make specific adjustments to your UI and could use this function to implement multicolumn setups or other UI displays. If you are used to the two-file way of doing shiny apps, then you can implement code you would have used in your ui.r file this way. For the most part, rmarkdown handles the UI aspect of things just fine unless you are looking to do something special or different.
As a side note, the shiny library has html tag objects that allow you to implement specific html builder functions. This likely would appeal to experienced web-developers although I'm not sure you would need to use R shiny if you are already a web development wizard.
Now for the actual dashboard. For the R outputs, I want to display a correlation matrix for the three currencies, and I want a trend graph to display the data over time. I'm going to use a date range widget so the user can chose the date range for the crypto data, along with a selectInput widget so the user can chose the daily coin value, daily token volume, or daily $ volume. I'm going to use another selector widget so the user can decide which currencies to include in the graph.
I'm going to use a sidebar panel for the widgets because the correlation matrix will be small and we have plenty of horizontal space. Particularly with the adjusted margins. For my widgets, the code is pretty straightforward.
I just placed my widgets within the sidebarPanel function separated by a comma and rmarkdown did the rest. In case there is any confusion about what I did in the widgets, check the documentation.
The code for the correlation matrix looks worse than it is because I had to subset each currency by the date range and the selected information. Notice how the date range input comes as a vector of two values. Consequently, I used "&" in the logic for subsetting the rows so it would select those between the two selected dates.
Lastly, I do something very similar for the data that feeds the line graph. I used an if statement so I could do a little y axis formatting for currency values.
At the end, I specified pixel width and height because the defaults often are too small for my taste. The dashboard you end up with looks like this.
Hopefully, all of that code made sense. I feel like there weren't any curve balls in this one and you end up with a simple example dashboard. If not, feel free to ask me any specific questions at this site. Be aware that there is a nominal $1.50 fee to submit questions. That is because it takes time and effort to respond to your questions.
I'll address the following:
- Loading and using data in your document
- Adjusting margins in your shiny document. Margins are by default set at a specific width for all shiny documents.
- Provide example code for R, Rshiny, Rmarkdown dashboard.
- Includes two selector inputs, one to choose which column of the daily trading data to use and the other to select which cryptocurrencies to plot.
- date range input
- render table with correlation matrix
- render line graph with options to select which cryptocurrencies to graph.
This code is not in my .rmd file. I like to use .rda files so I don't have to do any extra work when I read it in. Just makes the .rmd file feel less encumbered, I guess. Not necessary to use .rda file, of course. When pulling external data into your rmarkdown document, the working directory (place where the program looks for files and exports them) will automatically be in the same location as your .rmd file.
One thing that has always annoyed me about Shiny is the document margins are automatically enforced on every document to be a certain width. On larger screens it isn't a big deal but on small screens it starts to look overly narrow and often disrupts rendered plots. The following code adjusts the default width to automatically respond to the width of the computer screen viewing it.
This goes into the main-container div of the unseen inner-workings of the shiny html code and makes that adjustment. You could also insert your own custom margin width inside the curly brackets if you wanted to. You need to use the renderUI function to make specific adjustments to your UI and could use this function to implement multicolumn setups or other UI displays. If you are used to the two-file way of doing shiny apps, then you can implement code you would have used in your ui.r file this way. For the most part, rmarkdown handles the UI aspect of things just fine unless you are looking to do something special or different.
As a side note, the shiny library has html tag objects that allow you to implement specific html builder functions. This likely would appeal to experienced web-developers although I'm not sure you would need to use R shiny if you are already a web development wizard.
Now for the actual dashboard. For the R outputs, I want to display a correlation matrix for the three currencies, and I want a trend graph to display the data over time. I'm going to use a date range widget so the user can chose the date range for the crypto data, along with a selectInput widget so the user can chose the daily coin value, daily token volume, or daily $ volume. I'm going to use another selector widget so the user can decide which currencies to include in the graph.
I'm going to use a sidebar panel for the widgets because the correlation matrix will be small and we have plenty of horizontal space. Particularly with the adjusted margins. For my widgets, the code is pretty straightforward.
I just placed my widgets within the sidebarPanel function separated by a comma and rmarkdown did the rest. In case there is any confusion about what I did in the widgets, check the documentation.
The code for the correlation matrix looks worse than it is because I had to subset each currency by the date range and the selected information. Notice how the date range input comes as a vector of two values. Consequently, I used "&" in the logic for subsetting the rows so it would select those between the two selected dates.
Lastly, I do something very similar for the data that feeds the line graph. I used an if statement so I could do a little y axis formatting for currency values.
At the end, I specified pixel width and height because the defaults often are too small for my taste. The dashboard you end up with looks like this.
Hopefully, all of that code made sense. I feel like there weren't any curve balls in this one and you end up with a simple example dashboard. If not, feel free to ask me any specific questions at this site. Be aware that there is a nominal $1.50 fee to submit questions. That is because it takes time and effort to respond to your questions.
Comments
Post a Comment