在R Shiny中工作,尝试使用下拉菜单中的选定值/字符串作为sql查询中的输入。问题是我收到下面的错误消息。我怎么能绕过这个?我们的想法是,通过从下拉菜单中选择输入(本例中的资产类),您将能够在折线图中绘制相应的返回数据。我从SO中获取了粘贴函数,但是没有使用输入$ showDrop。当整个查询被硬编码时,图表工作正常。
错误:$运算符对原子矢量无效
见下面的代码:
library(shiny)
library(RODBC)
ui <- shinyUI(
fluidPage(
titlePanel("Application Title"),
ch <- odbcDriverConnect('driver={SQL Server};server=SERVER;database=DATABASE;trusted_connection=true')
showList <- sqlQuery(ch, "SELECT DISTINCT [Asset] FROM [TABLE] Where [Market] = 'US' ORDER BY [Asset]", stringsAsFactors = FALSE)
selectInput("showDrop", "Select Asset", showList),
plotOutput("cumReturn")
))
server <- shinyServer(function(input, output) {
output$cumReturn <- renderPlot({
ch <- odbcDriverConnect('driver={SQL Server};server=SERVER;database=DATABASE;trusted_connection=true')
returns <- sqlQuery(ch, paste("SELECT [Asof], [Data] FROM [TABLE] where [Market] = 'US' AND [Asset] = ", input$showDrop))
dfx = xts(returns$Data-1, order.by=as.Date(returns$Asof))
chart.CumReturns(dfx, main = "Accumulated return", wealth.index = TRUE, geometric = TRUE, legend.loc = NULL, ylog = TRUE,
colorset = (1:12), begin = c("first", "axis"))
})
})
shinyApp(ui = ui, server = server)