下面是我试图在rstudio中运行并得到以下错误的代码,可以有人指出我的错误:
Warning: Error in .oci.GetQuery: invalid connection
Stack trace (innermost first):
84: .oci.GetQuery
83: .local
82: dbGetQuery
81: dbGetQuery
80: getqdata [P:/EI_DBA_Engineering/ShinyR/EI/App5/apptest.r#51]
79: ggplotly [P:/EI_DBA_Engineering/ShinyR/EI/App5/apptest.r#56]
78: func
77: origRenderFunc
76: output$dynamicPlot
1: runApp
下面的代码试图连接到数据库并检索数据,我在连接时得到错误。
require(ggplot2)
require(scales)
require(reshape)
require(ROracle)
require(shiny)
library(plotly)
ui <- dashboardPage(
skin="green",
dashboardHeader(title = "Real Time Dashboard for EI"),
dashboardSidebar(disable = TRUE),
dashboardBody(
fluidRow(
# A stati"c valueBox
#column(5,DT::dataTableOutput("text1"))
),
box(
title = "CPU UTILIZATION DAILY "
,status = "primary"
,solidHeader = TRUE
,collapsible = TRUE
,width = "800px"
,plotlyOutput('dynamicPlot', height = "300px")
)
))
server <- function(input, output, session) {
sqlstmt1 <- reactive({
sqlstmt1 <- paste0("select instance_name from v$instance")
print (sqlstmt1)
return(sqlstmt1)
})
con <- reactive({
drv <- dbDriver("Oracle")
on.exit(dbDisconnect(con))
con <- dbConnect(drv, dbname = "abcd", username = "aaaaa", password = "bbbbbbb")
print (con)
return(con)
})
getqdata <- function (con) {
print ("inside getn")
#print (con())
#print (sqlstmt1())
cat("Refreshing....","n")
mdat <- dbGetQuery(con,sqlstmt1())
return(mdat)
}
output$dynamicPlot <- renderPlotly({
p1 <- getqdata(con())
stout <- t(p1())
mdata <- melt(p1(), id.var = c("TTIME", "ENTITY_NAME"))
print (mdata)
invalidateLater(10000, session)
#p1 <- plot_ly(mdata, x = ~TTIME, y = ~value, type = 'scatter', mode = 'lines', color= ~ENTITY_NAME , fill = 'tozeroy') %>%
# layout(xaxis = list(title = 'Date',showticklabels = FALSE),
# yaxis = list(title = 'CPU Percentage'))
#ggplotly(p1)
#invalidateLater(10000, session)
})
}
shinyApp(ui, server)
我通过取出on.exit(dbDisconnect(con))