简体   繁体   中英

How to get this R script working

I have trouble understanding why the input wont get passed to the server side, worked on and returned in the plot. I really hope someone can enlighten me on the workings of the UI/SERVER dynamic.

    library(shiny)
    library(quantmod)
    library(BatchGetSymbols)


    # Define server logic required to draw a histogram
    shinyServer(function(input, output) {

      dataInput <- reactive({
        getSymbols(input$stock, src = "google",
                   auto.assign = FALSE)
      })

    output$plot <- renderPlot({    
      chartSeries(dataInput(), theme = chartTheme("white"))
      addBBands()
      addMACD()
      addRSI()
    })



      })

library(shiny)
library(quantmod)
library(BatchGetSymbols)

first.date <- Sys.Date()-365
last.date <- Sys.Date()

df.SP500 <- GetSP500Stocks()
tickers <- df.SP500$tickers

l.out <- getSymbols(tickers = tickers,
                    first.date = first.date,
                    last.date = last.date)

stocks <- df.SP500[,1]

shinyUI(fluidPage(


  titlePanel("Tim Fruitema Technical Stocks"),


  sidebarLayout(
    sidebarPanel(
      selectInput(stocks, NULL, df.SP500[,1], selected = 1, multiple = FALSE,
                  selectize = FALSE, width = NULL, size = 30),
      actionButton("update", "Submit")

    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput(output$plot)

    )
  )
))

The first problem seems to be that your src argument to getSymbols returns an error:

Error: ‘getSymbols.google’ is defunct.
Google Finance stopped providing data in March, 2018.
You could try setting src = "yahoo" instead.
See help("Defunct") and help("quantmod-defunct")

You also needed to change input$stock to input$stocks (and quote stocks on the UI side). In addition you needed to change output$plot to just "plot" . I used yahoo instead of google :

library(shiny)
library(quantmod)
library(BatchGetSymbols)

first.date <- Sys.Date() - 365
last.date  <- Sys.Date()

df.SP500   <- GetSP500Stocks()
tickers    <- df.SP500$tickers

l.out      <- getSymbols(
  tickers    = tickers,
  first.date = first.date,
  last.date  = last.date
)

stocks <- df.SP500[,1]

ui <-fluidPage(

  titlePanel("Tim Fruitema Technical Stocks"),

  sidebarLayout(
    sidebarPanel(
      selectInput("stocks", NULL, df.SP500[,1], selected = 1, multiple = FALSE,
                  selectize = FALSE, width = NULL, size = 30),
      actionButton("update", "Submit")

    ),

    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("plot")

    )
  )
)


server <- function(input, output) {

  dataInput <- eventReactive(input$update, {
    getSymbols(input$stocks, src = "yahoo",
               auto.assign = FALSE)
  })

  output$plot <- renderPlot({    
    chartSeries(dataInput(), theme = chartTheme("white"))
    addBBands()
    addMACD()
    addRSI()
  })

}

shinyApp(ui, server)

在此处输入图片说明

Overall it looks like you should go through the basics of the Shiny structure again: https://shiny.rstudio.com/tutorial/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM