# 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! ### Yide Pan

View all posts by Yide Pan >
{"ft_lang_label":"__label__en","ft_lang_prob":0.6600489,"math_prob":0.947357,"size":3827,"snap":"2020-45-2020-50","text_gpt3_token_len":1069,"char_repetition_ratio":0.13104892,"word_repetition_ratio":0.0075046904,"special_character_ratio":0.28220537,"punctuation_ratio":0.16715977,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9878782,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-10-28T08:13:57Z\",\"WARC-Record-ID\":\"<urn:uuid:f2d61390-0e89-4c69-bd6c-bd9834c6bfeb>\",\"Content-Length\":\"143289\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:88410f6a-6c75-4543-9dd4-0edef74a2987>\",\"WARC-Concurrent-To\":\"<urn:uuid:205131e6-5298-4b09-bd20-2378b7f9f2fa>\",\"WARC-IP-Address\":\"104.28.11.8\",\"WARC-Target-URI\":\"https://nycdatascience.com/blog/student-works/a-simple-shiny-interface-to-retrieve-stock-information/\",\"WARC-Payload-Digest\":\"sha1:H7ZBLXKFX7HTLWX5CV7DNHJZK5LYJFKA\",\"WARC-Block-Digest\":\"sha1:KNAUD3MA5JEHFTMQFRZADVP32FSK5ARA\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-45/CC-MAIN-2020-45_segments_1603107897022.61_warc_CC-MAIN-20201028073614-20201028103614-00568.warc.gz\"}"}