Skip to main content

Interactive charts with R shiny app

Let's start with so motivational material. I recommend you to visit website: https://shiny.rstudio.com/gallery/  

Impressive, isn't ? I was watching with open mouth all this visualizations, changing parameters and observing how it would change. Creating such reports are possible with R shiny app. Shiny is an open source R package for building web application. First install it on your computer.

install.packages("shiny")

R Shiny Framework

Choose in your open RStudio: File-> New Project->New Directory->Shiny web Application. RStudio will create script app.R

Delate the content and write shinyapp and press Shift+Tab, you should see the following:

library(shiny)
ui <- fluidPage(
)
server <- function(input, output, session) { 
}
shinyApp(ui, server)

This is the main structure when you are building your shiny app. You need user interface (ui), server and shinyApp() function.

There are three pieces of an interactive component:

1. User interface will collect user input

2. Server function gives recipe for output

3. User interface displays output

To give the particular look to your app you can use some already existing layout. For example the most popular sidebar layout or grid layout.

In the next post I will describe step by step how I build my first R shiny app.


Comments

Popular posts from this blog

Model Residuals in Time Series Data

Residuals are the indicator of the model quality. Based on Rob J Hyndman's book "Forecasting: Principles & Practice", residuals in forecasting is difference between observed value and its forecast based on all previous observations. Residuals are useful in checking whether a model has adequately captured the information in the data. All the patterns should be in the model, only randomness remains in the residuals. Therefore the ideal model has to be: uncorrelated has zero mean and useful properties are: constant variance  be normally distributed First I will activate some useful libraries we will be using. library(fpp) library(forecast) For our example I will use dowjones index as a data set. The idea will be to set up already well know simple models like: Mean Model, Naive model and Drift Model. In previous post I described  it more detailed. Next, knowing what attributes  the ideal model should  have we can check which one of those 3 are quite good or  def...

Random number generators, reproducibility and sampling with dplyr

Let's assume that you want to take some random observations from your data set. Dplyr helps you with the function sample_n(). To make your code reproducible you seed the ID of a “random” set of values. You need to indicate number of rows you want to extract and specify if the rows should be replaced or not. To show you how it works I will use again mtcars dataset which is included in your base R program. Let's see first six rows of this data frame.  library(dplyr) data("mtcars") head(mtcars)                    mpg cyl disp  hp drat    wt  qsec vs am gear carb Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4 Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4 Datsun 710        22.8   4  108  93 3.85 2.320 18.61...

The Power of dplyr in R - part 3

Today I would like to present pipe operator which simplify our code and makes it more readable. As we can see all of the dplyr functions take a data frame (or tibble) as the first argument. Dplyr provides the %>% operator from magrittr that chains the functions so x %>% f(y) turns into f(x, y). Therefore  the result from one step is then “piped” into the next step. We will use pipe operator in further examples.  Additionally we will focus on grouping, ordering and summarising functions. As previously I will continue using mtcars dataset which is included in your R base program. count() #count the unique values of one or more variables   n()  n_distinct() #number of unique observation found in a category  group_by() # group by a column, allows to group operation in the “split-apply-combine" concept   library(dplyr) data("mtcars") head(mtcars)                    mpg cyl disp  hp drat...