无法从特定日期的 R shiny 中的 sql 获取列值
Not being able to get column values from sql in R shiny for particular date
我是 R 的新手
我正在尝试使用我创建的 sql 数据库在 Shiny 中绘制控制图。
在下面的代码中,我能够根据日期获取 sql 数据。
但我无法访问我必须为其绘制图形的列值。
以下是数据库的前几行:
id product_name product_config detected_width created
1 Belt width 69.84 2020-04-19
2 Belt width 71.12 2020-04-19
在绘图选项卡中,
我收到以下错误:
错误:'data' 必须是向量类型,原为 'NULL'
所以,我猜列值没有被选中。谁能帮忙解决这个问题。
library(pool)
library(dplyr)
library(shiny)
library(DBI)
library(plotly)
library(qcc)
ui <- fluidPage(
fluidRow(
column(4,
h3("Selecting Data"),
dateInput("date", "Enter a date:", value = Sys.Date())
),
column(8,
h3("Plot"),
tabsetPanel(
tabPanel("Table", tableOutput("tbl")),
tabPanel("Control Chart",plotOutput("plot"))
)
)
)
)
server <- function(input, output, session){
output$tbl <- renderTable({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "testdatabase",
host = "localhost",
username = "root",
password = "root"
)
on.exit(dbDisconnect(conn), add = TRUE)
sql <- "SELECT * FROM Ceat_table WHERE created = ?date1;"
query <- sqlInterpolate(conn, sql, date1 = input$date)
dbGetQuery(conn, query)
})
output$plot <- renderPlot({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "testdatabase",
host = "localhost",
username = "root",
password = "root"
)
on.exit(dbDisconnect(conn), add = TRUE)
sql <- "SELECT * FROM Ceat_table WHERE created = ?date1;"
query <- sqlInterpolate(conn, sql, date1 = input$date)
dbGetQuery(conn, query)
ceatdb <- tbl(conn, "Ceat_table")
a<-qcc(ceatdb$detected_width,type = "xbar.one")
plot(a)
})
}
shinyApp(ui = ui, server = server)
错误出在你的 renderPlot
函数中。
请注意,renderTable
函数中的最后一个命令是 dbGetQuery(conn, query)
。这从数据库中获取数据,因为它是最后一个命令,所以该命令的结果将传递给 UI.
相比之下,对于 renderPlot
函数,您可以通过两种不同的方式访问相同的 table:
- 直接作为
dbGetQuery(conn, query)
但不在本地存储获取的结果。
- 作为带有
ceatdb <- tbl(conn, "Ceat_table")
的远程 table,但没有使用 collect()
. 将结果完全加载到 R 中
我建议您只使用其中一种方法。
选项 1:保存 dbGetQuery
的结果:
conn <- dbConnect( your_connection_details_here )
on.exit(dbDisconnect(conn), add = TRUE)
sql <- "SELECT * FROM Ceat_table WHERE created = ?date1;"
query <- sqlInterpolate(conn, sql, date1 = input$date)
ceatdb = dbGetQuery(conn, query) # key change
a<-qcc(ceatdb$detected_width,type = "xbar.one")
plot(a)
选项 2:使用 collect()
:
将远程 table 加载到内存中
conn <- dbConnect( your_connection_details_here )
on.exit(dbDisconnect(conn), add = TRUE)
ceatdb <- tbl(conn, "Ceat_table") %>% collect() # key change
a<-qcc(ceatdb$detected_width,type = "xbar.one")
plot(a)
请注意,对于远程 tables,不能使用 $
表示法以与本地 tables 相同的方式访问列。证明这一点的一种方法是比较 names(my_table)
和 colnames(my_table)
的输出。本地 tables 将对这两个命令给出相同的结果,但远程 tables 不会。
我是 R 的新手
我正在尝试使用我创建的 sql 数据库在 Shiny 中绘制控制图。
在下面的代码中,我能够根据日期获取 sql 数据。
但我无法访问我必须为其绘制图形的列值。
以下是数据库的前几行:
id product_name product_config detected_width created
1 Belt width 69.84 2020-04-19
2 Belt width 71.12 2020-04-19
在绘图选项卡中,
我收到以下错误:
错误:'data' 必须是向量类型,原为 'NULL'
所以,我猜列值没有被选中。谁能帮忙解决这个问题。
library(pool)
library(dplyr)
library(shiny)
library(DBI)
library(plotly)
library(qcc)
ui <- fluidPage(
fluidRow(
column(4,
h3("Selecting Data"),
dateInput("date", "Enter a date:", value = Sys.Date())
),
column(8,
h3("Plot"),
tabsetPanel(
tabPanel("Table", tableOutput("tbl")),
tabPanel("Control Chart",plotOutput("plot"))
)
)
)
)
server <- function(input, output, session){
output$tbl <- renderTable({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "testdatabase",
host = "localhost",
username = "root",
password = "root"
)
on.exit(dbDisconnect(conn), add = TRUE)
sql <- "SELECT * FROM Ceat_table WHERE created = ?date1;"
query <- sqlInterpolate(conn, sql, date1 = input$date)
dbGetQuery(conn, query)
})
output$plot <- renderPlot({
conn <- dbConnect(
drv = RMySQL::MySQL(),
dbname = "testdatabase",
host = "localhost",
username = "root",
password = "root"
)
on.exit(dbDisconnect(conn), add = TRUE)
sql <- "SELECT * FROM Ceat_table WHERE created = ?date1;"
query <- sqlInterpolate(conn, sql, date1 = input$date)
dbGetQuery(conn, query)
ceatdb <- tbl(conn, "Ceat_table")
a<-qcc(ceatdb$detected_width,type = "xbar.one")
plot(a)
})
}
shinyApp(ui = ui, server = server)
错误出在你的 renderPlot
函数中。
请注意,renderTable
函数中的最后一个命令是 dbGetQuery(conn, query)
。这从数据库中获取数据,因为它是最后一个命令,所以该命令的结果将传递给 UI.
相比之下,对于 renderPlot
函数,您可以通过两种不同的方式访问相同的 table:
- 直接作为
dbGetQuery(conn, query)
但不在本地存储获取的结果。 - 作为带有
ceatdb <- tbl(conn, "Ceat_table")
的远程 table,但没有使用collect()
. 将结果完全加载到 R 中
我建议您只使用其中一种方法。
选项 1:保存 dbGetQuery
的结果:
conn <- dbConnect( your_connection_details_here )
on.exit(dbDisconnect(conn), add = TRUE)
sql <- "SELECT * FROM Ceat_table WHERE created = ?date1;"
query <- sqlInterpolate(conn, sql, date1 = input$date)
ceatdb = dbGetQuery(conn, query) # key change
a<-qcc(ceatdb$detected_width,type = "xbar.one")
plot(a)
选项 2:使用 collect()
:
将远程 table 加载到内存中
conn <- dbConnect( your_connection_details_here )
on.exit(dbDisconnect(conn), add = TRUE)
ceatdb <- tbl(conn, "Ceat_table") %>% collect() # key change
a<-qcc(ceatdb$detected_width,type = "xbar.one")
plot(a)
请注意,对于远程 tables,不能使用 $
表示法以与本地 tables 相同的方式访问列。证明这一点的一种方法是比较 names(my_table)
和 colnames(my_table)
的输出。本地 tables 将对这两个命令给出相同的结果,但远程 tables 不会。