原文: http://supstat.com.cn/blog/2014/12/03/a-simple-shiny-interface-to-retrieve-stock-information/
本文的作者是某国际知名制药公司在华研究中心的工程师,今年8月他们部门接受了我们的R语言培训,这篇文章就是培训后他做的presentation.
这个项目以利用shiny获取和展示股票信息为目标。
新浪是获取中国股票信息源数据的理想场所,我们可以利用下面的代码来得到数据,然而,sina.com还能够提供准确到分钟的精确信息。
library(RCurl)
2 library(XML)
3 library(plyr)
4 raw <- getURL("http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?symbol=sh600000&end_date=20121231&begin_date=20111231")
5 raw
6 do.call(rbind, xmlToList(raw))
但是来自新浪的数据格式并不规范,我们需要花大量的时间去清洗和整理。我的主要目标是利用shiny来展示股市数据,因此我使用雅虎为数据源然后直接使用quantmod程序包来提取数据。
下面服务终端代码。代码非常简单。让人吃惊的是R居然能如此完美的处理这一大堆股票数据。
1 if (!require(quantmod)) {
2 stop("This app requires the quantmod package. To install it, run 'install.packages(\"quantmod\")'.\n")
3 }
1 # Download data for a stock if needed, and return the data
2 require_symbol <- function(symbol, envir = parent.frame()) {
3 if (is.null(envir[[symbol]])) {
4 envir[[symbol]] <- getSymbols(symbol, auto.assign = FALSE)
5 }
6
7 envir[[symbol]]
8 }
9
10
11 shinyServer(function(input, output) {
12 # Create an environment for storing data
13 symbol_env <- new.env()
14
15 # Make a chart for a symbol, with the settings from the inputs
16 make_chart <- function(symbol) {
17 symbol_data <- require_symbol(symbol, symbol_env)
18 #TA_STR <- paste0()
19 chartSeries(symbol_data,
20 name = symbol,
21 type = input$chart_type,
22 subset = paste("last", input$time_num, input$time_unit),
23 #log.scale = input$log_y,
24 theme = "white")
25 }
26
27 output$plot_1 <- renderPlot({ make_chart(input$stock1) })
28 output$plot_2 <- renderPlot({ make_chart(input$stock2) })
29 output$plot_3 <- renderPlot({ make_chart(input$stock3) })
30 output$plot_4 <- renderPlot({ make_chart(input$stock4) })
31 output$plot_5 <- renderPlot({ make_chart(input$stock5) })
32 })
接着是前端代码,它主要包括前端组件的构建。
1 shinyUI(pageWithSidebar(
2 headerPanel("炒股神器 发财大计"),
3 sidebarPanel(
4 wellPanel(
5 p(strong("股票")),
6 textInput(inputId = "stock1", label = "股票1"),
7 textInput(inputId = "stock2", label = "股票2"),
8 textInput(inputId = "stock3", label = "股票3"),
9 textInput(inputId = "stock4", label = "股票4"),
10 textInput(inputId = "stock5", label = "股票5")),
11 selectInput(inputId = "chart_type",
12 label = "图形",
13 choices = c("蜡烛图" = "candlesticks",
14 "火柴图" = "matchsticks",
15 "柱形图" = "bars",
16 "线型图" = "line")),
17 wellPanel(
18 p(strong("日期范围 (从现在起倒推)")),
19 sliderInput(inputId = "time_num",
20 label = "时间个数",
21 min = 1, max = 24, step = 1, value = 6),
22
23 selectInput(inputId = "time_unit",
24 label = "时间单位",
25 choices = c("日" = "days",
26 "周" = "weeks",
27 "月" = "months",
28 "年" = "years"),
29 selected = "Months"))
30 #checkboxInput(inputId = "log_y", label = "log y axis", value = FALSE)
31 ),
32 mainPanel(
33 conditionalPanel(condition = "input.stock1",
34 br(),
35 div(plotOutput(outputId = "plot_1"))),
36 conditionalPanel(condition = "input.stock2",
37 br(),
38 div(plotOutput(outputId = "plot_2"))),
39 conditionalPanel(condition = "input.stock3",
40 br(),
41 div(plotOutput(outputId = "plot_3"))),
42 conditionalPanel(condition = "input.stock4",
43 br(),
44 div(plotOutput(outputId = "plot_4"))),
45 conditionalPanel(condition = "input.stock5",
46 br(),
47 plotOutput(outputId = "plot_5"))
48 )
49 )
50 )
最终生成的shiny app的效果是这样的:
Shiny十分强大,它就像是我信息部的同事一样能应用各种各样的工具来处理数据,然后展现给科学家们查看。以前我常常提出这样的问题,怎么样才能为我整理好的数据建立一个规范的端口,然后让用户在各个方向上灵活地分析。Shiny和R恰好是一个好的解决方法,但是我依然需要找到一个将shiny应用于用户的便捷方法。