【问题标题】:Shiny R Date Input Pop Up messageShiny R 日期输入弹出消息
【发布时间】:2015-02-25 18:25:03
【问题描述】:

我有一个应用程序的服务器文件,该文件从用户输入的股票代码中获取来自雅虎财经的股票信息。这里相关的服务器代码是

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)

这给出了以下内容

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

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'

这很好,因为它无法打开 URL,因为他们输入的符号不存在股票。但是,我希望出现一条弹出消息,说明他们插入的股票不存在股票代码。我已经尝试过执行此操作的方法,包括 bsAlert() 方法,但我似乎无法做到。任何帮助都会很棒

【问题讨论】:

标签: r shiny


【解决方案1】:

您可以在获取符号时使用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)

【讨论】:

  • 谢谢,它运行良好,但是当我这样做时,我遇到了以下问题i.stack.imgur.com/RyeoR.png。除非在控制台中按停止,否则用户无法继续或使用该应用程序,一旦他们这样做,就会出现以下错误 i.stack.imgur.com/pYh7t.png 。我该如何阻止这种情况发生?
  • 您是否介意发布整个代码,这可能与您之后对数据所做的计算有关,如果发现错误,则发布一个空字符串可能不适用于您的应用程序的其余部分跨度>
  • pastebin.com/w3zbLU1w 里面有ui和server,我相信是因为它正在从网站收集数据,所以统计测试无法正常运行。感谢您的帮助
  • 尝试在你的lg.retblock, lg.ret.vec`的反应部分添加validate(need(dataInput()!="","")),在output$plot &lt;- renderPlot({之后,它会检查你是否有dataInput并且没有运行如果没有,则代码。
  • 我想知道是否会在日期范围输入中将日期格式更改为其他格式,例如“mm/dd/yyy”?因为我有一个包含此类日期的数据集。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
  • 2017-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多