2017-08-31 77 views
2

我想创建一个Shiny App来显示图表系列图。但是,它没有显示addTA图。我想这样做的addTA功能出ChartSeries中的功能,因为我想将它们放到选项,低于我的代码:Shiny App中的R 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函数没有输出,有什么建议,谢谢。

回答

3

包装你quantmod图表函数调用chartSeries/chart_Series, addTA/add_TA, addRSI等,用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)) 
    }) 
}) 
+0

我可以问一个问题吗?如果我在checkboxGroupInput中设置了这些指标,当我点击时如何显示指标?我无法弄清楚如何将输入$指标放在addTA函数中。 –

+0

@Peter Chung当然,给一些示例代码,我会看看(或别人可能)。但应该可能是另一个问题 – FXQuantTrader