【问题标题】:How to add a custom indicator to Shiny, R quantmod question如何向 Shiny、R quantmod 问题添加自定义指标
【发布时间】:2020-09-26 10:56:19
【问题描述】:

我是新手,试图向 Shiny/R quantmod 应用程序添加自定义指标。

R 脚本中的以下代码运行良好。

library(quantmod)
getSymbols('SBUX')
barChart(SBUX)
# create new TA function
myInd <- function(x) {
  return(WMA(Cl(x)))
}
addMyInd <- newTA(FUN = myInd)
addMyInd()

闪亮的等价物

library(shiny)
library(quantmod)

myInd <- function(x) {
  return(WMA(Cl(x)))
}
addMyInd <- newTA(FUN = myInd)

shinyServer(function(input, output,session) {
observe({
  query <- parseQueryString(session$clientData$url_search)
  dataInput <- reactive({ as.xts(getSymbols('SBUX', auto.assign = FALSE)) })
  output$chart <- renderPlot({ chartSeries(dataInput(), name = 'SBUX', TA = c(addMyInd()) ) })  
 })
})

失败并出现错误:找不到函数“myInd”。

而用任何内置函数替换“addMyInd”效果很好。

output$chart <- renderPlot({ chartSeries(dataInput(), name = 'SBUX', TA = c(addWMA()) )

知道如何让 Shiny 找到“myInd”函数吗?

【问题讨论】:

    标签: r shiny quantmod shinyapps technical-indicator


    【解决方案1】:

    我无法真正尝试它是否有效,但根据我的 Shiny 应用程序,您可以尝试 a) 使其具有响应性:

    myInd <- function(x) {
      reactive(return(WMA(Cl(x))))
    }
    

    并且 b) 将其放入服务器调用中:

    shinyServer(function(input, output,session) {
    
      myInd <- function(x) {
        reactive(return(WMA(Cl(x))))
      }
      addMyInd <- newTA(FUN = myInd)
    
      observe({
        query <- parseQueryString(session$clientData$url_search)
        dataInput <- reactive({ as.xts(getSymbols('SBUX', auto.assign = FALSE)) })
        output$chart <- renderPlot({ chartSeries(dataInput(), name = 'SBUX', TA = c(addMyInd()) ) })  
      })
    })
    

    【讨论】:

    • 谢谢,但没用。如果你可以试试。 ui.Rlibrary(shiny) shinyUI( plotOutput("chart", height = "565px") )server.Rlibrary(shiny) library(quantmod) shinyServer(function(input, output,session) { myInd &lt;- function(x) {reactive(return(WMA(Cl(x))))} addMyInd &lt;- newTA(FUN = myInd) observe({ query &lt;- parseQueryString(session$clientData$url_search) dataInput &lt;- reactive({ as.xts(getSymbols('SBUX', auto.assign = FALSE)) }) output$chart &lt;- renderPlot({ chartSeries(dataInput(), name = 'SBUX', TA = c(addMyInd()) ) }) }) })localhost:8100
    【解决方案2】:

    适用于可能面临类似问题的任何人。

    诀窍是将自定义指标的两个函数放在另一个文件中,并将其包含在 server.R 中。

    helper.R

    library(quantmod)
    
    myInd <- function(x) {
        return(WMA(Cl(x)))
    }
    
    addMyInd <- newTA(FUN = myInd)
    

    server.R 只是读取

    library(shiny)
    library(quantmod)
    
    source("helper.R");
    
    shinyServer(function(input, output,session) {
    observe({
      query <- parseQueryString(session$clientData$url_search)
      dataInput <- reactive({ as.xts(getSymbols('SBUX', auto.assign = FALSE)) })
      output$chart <- renderPlot({ 
        chartSeries(dataInput(), name = 'SBUX', TA = c(addMyInd()) ) 
        }) 
     })
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-12
      • 2020-02-20
      相关资源
      最近更新 更多