A simple shiny interface to retrieve stock information
Demo day: Sept 16, 2014.
Contributed by Yide Pan.
Yide took the CORP-R 001 class with Vivian Zhang (Roche R Programming
Beginner) in Aug, 2014.
This post was based on his final project submission.
Goal: Get the Chinese stock information base on input stock code.
This project is aimed to retrieve and display stock information using shiny.
Data Prep
Sina is the ideal data source for China's stock infomration , we can get data using such kind of code, however sina.com can provide more precise data to minutes level.
library(RCurl)
library(XML)
library(plyr)
raw<-getURL("http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?symbol=sh600000&end_date=20121231&begin_date=20111231")
data do.call(rbind,xmlToList(data))
But the data from sina is not well orgnized, need spent time to clean and orgnize, my main goal is to use shiny, so I directly use quantmod and go with yahoo as data source.
Server.R
Here is server end code, very simple, it's amazing R can handle the data in such a performat way.
if (!require(quantmod)) {
stop("This app requires the quantmod package. To install it, run 'install.packages("quantmod")'.n")
}
# Download data for a stock if needed, and return the data
require_symbol if (is.null(envir[[symbol]])) {
envir[[symbol]] }
envir[[symbol]]
}
shinyServer(function(input, output) {
# Create an environment for storing data
symbol_env
# Make a chart for a symbol, with the settings from the inputs
make_chart symbol_data #TA_STR chartSeries(symbol_data,
name = symbol,
type = input$chart_type,
subset = paste("last", input$time_num, input$time_unit),
#log.scale = input$log_y,
theme = "white")
}
output$plot_1 output$plot_2 output$plot_3 output$plot_4 output$plot_5 })
ui.R
The front end code, mainly invoke the build in compoent.
shinyUI(pageWithSidebar(
headerPanel("炒股神器 发财大计"),
sidebarPanel(
wellPanel(
p(strong("股票")),
textInput(inputId = "stock1", label = "股票1"),
textInput(inputId = "stock2", label = "股票2"),
textInput(inputId = "stock3", label = "股票3"),
textInput(inputId = "stock4", label = "股票4"),
textInput(inputId = "stock5", label = "股票5")
),
// input box to manipulate stock code
selectInput(inputId = "chart_type",
label = "图形",
choices = c("蜡烛图" = "candlesticks",
"火柴图" = "matchsticks",
"柱形图" = "bars",
"线型图" = "line")
),
wellPanel(
p(strong("日期范围 (从现在起倒推)")),
sliderInput(inputId = "time_num",
label = "时间个数",
min = 1, max = 24, step = 1, value = 6),
selectInput(inputId = "time_unit",
label = "时间单位",
choices = c("日" = "days",
"周" = "weeks",
"月" = "months",
"年" = "years"),
selected = "Months")
)
#checkboxInput(inputId = "log_y", label = "log y axis", value = FALSE)
),
mainPanel(
conditionalPanel(condition = "input.stock1",
br(),
div(plotOutput(outputId = "plot_1"))),//invoke the sever function
conditionalPanel(condition = "input.stock2",
br(),
div(plotOutput(outputId = "plot_2"))),//invoke the sever function
conditionalPanel(condition = "input.stock3",
br(),
div(plotOutput(outputId = "plot_3"))),//invoke the sever function
conditionalPanel(condition = "input.stock4",
br(),
div(plotOutput(outputId = "plot_4"))),//invoke the sever function
conditionalPanel(condition = "input.stock5",
br(),
plotOutput(outputId = "plot_5")) //invoke the sever function
)
))
Conclusion
Shiny is very powerful, as I am informatics associate who will use various tools to handle data, and present them to scientists. Many time I meet the problem how to create a orgnized interface on top of the data I sorted out, and let user gain the flexibility to analyze data on multiple dimensions. Shiny and R is a good solution, but I still need to figure out a quick way to deploy shiny to my user.
I'd like to thanks to Vivian and her team, they give me a very vivid lesson can utilize new technology help me speed up the data process work!