我想查询一个SQL Server数据库。用户必须选择一些带有一些小部件的项来构建SQL查询,然后,通过操作按钮触发查询结果,将查询结果存储为数据框架,并由renderTable函数用作输入。不管我做了什么来修复它,我总能得到这样的信息:
不能强迫类型‘闭包’到类型‘字符’的向量。
你能给我一些建议吗?
这是我的代码:
library(shiny)
library(RODBC)
# Builds conection chain ----
conection <- paste0('driver={', DriverDB, '}; ',
'server=', myServerDB, '; ',
'database = ', myDataBase, '; ',
'uid = ', myUser, '; ',
'pwd = ', myPassword, '; ',
'trusted_connection = true')
# Define UI ----
ui <- fluidPage(
titlePanel()),
sidebarLayout(
sidebarPanel(
radioButtons(...),
selectInput(...),
dateRangeInput(...),
actionButton('execute_query', 'Execute query'),
),
mainPanel(
tableOutput('result')
)
)
)
# Define server logic ----
server <- function(input, output) {
myQuery <- reactive({'builds query expression from widgets inputs'})
myData <- reactive({
req(input$execute_query)
result <- NULL
channel_db <- odbcDriverConnect(conection)
result <- sqlQuery(channel_db, myQuery)
odbcClose(channel_db)
result
})
output$result <- renderTable({myData()})
}
# Run the app ----
shinyApp(ui = ui, server = server)我在R控制台中检查了SQL查询和对齐的有效性,它们运行良好。
发布于 2020-10-04 23:50:08
因为myQuery是反应性数据,所以您需要像以后对myData那样,把它当作一个函数来处理。
使用:
myData <- reactive({
req(input$execute_query)
result <- NULL
channel_db <- odbcDriverConnect(conection)
result <- sqlQuery(channel_db, myQuery()) # <-- the only change, add ()
odbcClose(channel_db)
result
}) 也许应该知道,“闭包”类似于“函数”。而且,对于所有意图和目的,反应数据和反应组件的出现和行为类似于函数。
https://stackoverflow.com/questions/64200756
复制相似问题