我是 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' must be of a vector type, was '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)