Peter Chung
Peter Chung

Reputation: 1122

R addTA function in Shiny App

I would like to create a Shiny App to show the chartseries plot. However, it did not show the addTA plot. I would like to do the addTA function out of the chartseries function because I wanted to put them into options, below its my code:

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

The addTA function did not output, any advice, thank you.

Upvotes: 0

Views: 453

Answers (1)

FXQuantTrader
FXQuantTrader

Reputation: 6891

Wrap your quantmod charting function calls chartSeries/chart_Series, addTA/add_TA, addRSI, etc, with print(.) to ensure they are drawn in Shiny apps:

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

Upvotes: 4

Related Questions