【问题标题】:R addTA function in Shiny AppShiny App中的R addTA函数
【发布时间】:2018-02-09 13:37:10
【问题描述】:

我想创建一个闪亮的应用程序来显示图表图。但是,它没有显示 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")
   ))))

服务器.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函数没有输出,请指教,谢谢。

【问题讨论】:

    标签: r function plot shiny quantmod


    【解决方案1】:

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

    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))
      })
    })
    

    【讨论】:

    • 我可以再问一个问题吗?如果我在 checkboxGroupInput 中设置这些指标,我如何在单击时显示指标?我不知道如何将 input$indicator 放入 addTA 函数中。
    • @Peter Chung 当然,提供一些示例代码,我会看看(或者其他人可能会)。不过可能应该是另一个问题
    猜你喜欢
    • 2017-11-27
    • 1970-01-01
    • 2018-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 2021-02-05
    相关资源
    最近更新 更多