基本上,我正在尝试通过向MySQL查询来在R中显示数据框。
我有两个过滤器,根据这些过滤器,数据帧/表的值将有所不同。该表是反应式的,基于用户选择的过滤器。
UI
ui <- fluidPage(fluidRow(
column(4,radioButtons("Stocks", "Stock Number",
choices = c(1: 2),selected='1')),
column(4,radioButtons("Funds", "Fund Name",
choices = list("W" = 1, "L" = 2),selected='1')),
column(4,checkboxGroupInput("Position", "Market Position",
choices = c(1:5))),
tableOutput("values")
)
服务器
server <- function(input, output)
{
tableValues<-reactive({
df<-dbSendQuery(mydb,paste0("SELECT STOCKS,FUNDS,POSITION,INVESTMENTS FROM
SUMMARY WHERE USERNAME='1223' and STOCKS=",input$Stocks," AND
FUNDS='",input$Funds,"'
AND POSITION=",input$position,";"))
return(df)
})
output$values <- renderTable({
tableValues()})
}
这是我现在所拥有的,但这似乎不起作用。关于如何显示数据框/表并根据所选过滤器使其具有反应性的任何建议?
谢谢!
答案 0 :(得分:1)
错误:error- "cannot coerce class 'structure("MySQLResult", package = "RMySQL")' to a data.frame"
。这是因为您尚未fetch
删除数据。
server <- function(input, output)
{
tableValues<-reactive({
query<-dbSendQuery(mydb,paste0("SELECT STOCKS,FUNDS,POSITION,INVESTMENTS FROM
SUMMARY WHERE USERNAME='1223' and STOCKS=",input$Stocks," AND
FUNDS='",input$Funds,"'
AND POSITION=",input$position,";"))
df = fetch(query, n = -1)
return(df)
})
output$values <- renderTable({
tableValues()})
}
https://www.rdocumentation.org/packages/DBI/versions/0.2-1/topics/dbSendQuery