闪亮的日期输入弹出消息

时间:2015-02-25 02:07:33

标签: r shiny

我有一个应用程序的服务器文件,它从yahoo finance获取股票信息,来自用户输入的股票代码。这里相关的服务器代码是

dataInput <- reactive({
  getSymbols(input$symb, src = "yahoo", 
    from = input$dates[1],
    to = input$dates[2],
    auto.assign = FALSE)
})

,ui.r代码是

  textInput("symb", "Symbol", "^FTSE"),

  dateRangeInput("dates", 
    "Date range",
    start = "2015-01-01", 
    end = as.character(Sys.Date())),

  submitButton("Analysis"),width=6)

这给出了以下

enter image description here

但是,如果用户输入的符号不正确或不是股票,我会收到以下错误

Error in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,  : 
  cannot open URL 'http://ichart.finance.yahoo.com/table.csv?s=sdf&a=11&b=16&c=2014&d=1&e=25&f=2015&g=d&q=q&y=0&z=sdf&x=.csv'

这很好,因为它不能打开网址,因为他们输入的符号不存在股票。但是,我想要一个弹出消息,说它们已经插入了一个库存符号。我已经尝试过这样做的方法,包括bsAlert()方法,但我似乎无法做到这一点。任何帮助都会很棒

1 个答案:

答案 0 :(得分:0)

获取符号时可以使用try/catch,如果出现错误,则会显示bsAlert

<强> app.R

library(quantmod)
library(shinyBS)

server<-function(input, output,session) {
  #get the symbol data
  symbolData<-reactive({
    #try/catch in case there is an error
    data<-tryCatch({
      #if there is a bsAlert, close it
      closeAlert(session, "alert")
      #try to get the symbols
      getSymbols(input$symb, src = "yahoo", 
               from = input$dates[1],
               to = input$dates[2],
               auto.assign = FALSE)},
               #if there is an error
               error=function(cond) {
                 #create the bsAlert
                 createAlert(session, inputId = "alert_anchor",
                             alertId="alert",
                             message = "Please enter a valid symbol",
                             type = "warning",
                             append="false",
                             dismiss = FALSE
                 )
                 #return an empty string
                 return("")
               })
    data
    })

  #as an example, output the table
  output$table<-renderDataTable({symbolData()})
}

ui<-fluidPage(
  fluidRow(
    column(3, wellPanel(
      textInput("symb", "Symbol", "^FTSE"),
      bsAlert(inputId = "alert_anchor"),
      dateRangeInput("dates", 
                     "Date range",
                     start = "2015-01-01", 
                     end = as.character(Sys.Date())),
      submitButton("Submit")
    )),
  column(6, dataTableOutput("table"))

  ))
shinyApp(ui = ui, server = server)