如何使这个R脚本工作

时间:2018-04-29 20:55:22

标签: r shiny

我无法理解为什么输入不会传递到服务器端,工作并在绘图中返回。我真的希望有人能够启发UI / SERVER动态的工作原理。

    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)

    )
  )
))

1 个答案:

答案 0 :(得分:1)

第一个问题似乎是src getSymbols参数返回错误:

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

您还需要将input$stock更改为input$stocks(并在UI端引用stocks)。此外,您需要将output$plot更改为"plot"。我使用yahoo代替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)

enter image description here

总的来说,您似乎应该再次了解Shiny结构的基础知识:https://shiny.rstudio.com/tutorial/