Riny app中的R addTA功能

时间:2017-08-31 14:36:46

标签: r function plot shiny quantmod

我想创建一个Shiny App来显示chartseries图。但是,它没有显示addTA图。我想在chartseries函数中使用addTA函数,因为我想把它们放在我的代码下面的选项中:

ui.R:

library(shiny)

shinyUI(fluidPage(
  titlePanel("Stock App"),

 sidebarLayout(
   sidebarPanel(

 helpText("HK stock market."),

 textInput("symb", h5("Symbol"), "0005.HK"),

 radioButtons(inputId="period", label=h5("Periodicity"), 
              choices=c("daily","weekly","monthly")),

 radioButtons(inputId="subset", label=h5("Time"), 
              choices=c("last 1 year","last 3 years","last 5 years")),

 checkboxGroupInput("indicator", 
                    label = h5("Indicators"), 
                    choices = list("addBBands", 
                                   "Intraday Intensity", 
                                   "MFI", "Parabolic SAR"),
                    selected = "addBBands")
   ),

   mainPanel(
     textOutput("text3"),
     br(),
     plotOutput("plot")
   ))))

server.R

library(shiny)
library(quantmod)


shinyServer(function(input, output) {

 output$text3 <- renderText({
  paste("you have chosen a stock ", input$symb)
})

 dataInput <- reactive({

   getSymbols(input$symb, src = "yahoo",
           auto.assign = FALSE, periodicity = input$period)
 })

  output$plot <- renderPlot({
  chartSeries(dataInput(), theme = chartTheme("white"), 
              up.col = "green", dn.col = "red", 
              TA = NULL, name = input$symb, subset = input$subset)
  addTA(SMA(Cl(na.omit(dataInput())),n=2), col="red", on = 1)
  addTA(SMA(Cl(na.omit(dataInput())),n=19), col="blue", on = 1)
})
})

addTA函数没有输出,有任何建议,谢谢。

1 个答案:

答案 0 :(得分:5)

使用chartSeries/chart_Series, addTA/add_TA, addRSI包裹您的quantmod图表函数调用print(.)等,以确保它们是在闪亮的应用程序中绘制的:

shinyServer(function(input, output) {

  output$text3 <- renderText({
    paste("you have chosen a stock ", input$symb)
  })

  dataInput <- reactive({

    getSymbols(input$symb, src = "yahoo",
               auto.assign = FALSE, periodicity = input$period)
  })

  output$plot <- renderPlot({
    print(chartSeries(dataInput(), theme = chartTheme("white"), 
                up.col = "green", dn.col = "red", 
                TA = NULL, name = input$symb, subset = input$subset))
    print(addTA(SMA(Cl(na.omit(dataInput())),n=2), col="red", on = 1))
    print(addTA(SMA(Cl(na.omit(dataInput())),n=19), col="blue", on = 1))
  })
})