This experiment will plot the Microsoft stock or the Google stock depending on user input. User can also select a date range.
The Shiny application will be called Experiment2. To set things up I will need to create 4 things
1. Create an Experiment2 folder.
2. Create a server.R script file in the Experiment2 folder.
3. Create a ui.R script file in the Experiment2 folder.
4. Create an Experiment2_App.R script file outside the Experiment1 folder.
The server.R script is
The Experiment2_app.R script is
The output is rendered here
https://andrnev.shinyapps.io/Experiment2/
The Shiny application will be called Experiment2. To set things up I will need to create 4 things
1. Create an Experiment2 folder.
2. Create a server.R script file in the Experiment2 folder.
3. Create a ui.R script file in the Experiment2 folder.
4. Create an Experiment2_App.R script file outside the Experiment1 folder.
The server.R script is
The ui.R script islibrary(shiny)
library(Quandl)
library(ggplot2)
shinyServer(function(input, output) {
# Place this variable here. It will be run only once each time a user visits the app.
# This makes the output unique to a particular user
stkList <- data.frame(name=c("Microsoft", "Google"),symbol=c("GOOG/NASDAQ_MSFT","GOOG/NASDAQ_GOOGL" ),stringsAsFactors = FALSE)
output$text1 <- renderText({
paste("You have selected ",input$var)
})
#renderPlot function is required to plot the graph
output$distPlot <- renderPlot({
# Use grep to find the Quandl data code name
# Place the Quandl function inside the render* function otherwise it does not become part of the
# reactive component
stkPrice <- Quandl(stkList[grep(input$var,stkList[,1], ignore.case = TRUE),2],
api_key="xxxxxxxxxxxxxxx",
trim_start=input$daterange[1],
trim_end=input$daterange[2],
order="asc")
#ggplot qplot function
qplot(stkPrice$Date,stkPrice$Close,
geom=c("line","point"),
xlab="Time",
ylab=paste(input$var, " stock price"))})
})
library(shiny)shinyUI(fluidPage(
titlePanel("Stock plotter with dateRangeInput reactive programming"),
sidebarLayout(
sidebarPanel(
helpText("Stock plot using Quandl and Shiny"),
selectInput("var",
label = "Choose a Microsoft or Google",
choices = c("Microsoft", "Google"),
selected = "Microsoft"),
#dateRangeInput to select the dates required for the plotting
dateRangeInput("daterange", "Date range:",
start = "01-Jan-2015",
end = "31-Jan-2016",
min = "01-Jan-2000",
max = "01-Jan-2017",
format = "dd-M-yyyy",
separator = " - ")
),
mainPanel(
textOutput("text1"),
br(),
plotOutput("distPlot")
)
)
))
The Experiment2_app.R script is
library(shiny)
runApp("Experiment2")
The output is rendered here
https://andrnev.shinyapps.io/Experiment2/
Comments
Post a Comment