2015-12-16 44 views
1

我們在RedHat 6.7上運行Shiny Server(和Shiny Dashboard),並在嘗試使用RODBC查詢數據庫時運行到段錯誤。當我們將文件輸入到app.R或向server.R文件中的數據庫發出查詢請求時,會發生這種情況。當我們在R控制檯中運行相同的代碼時,它工作得很好。RODBC查詢降低Shiny服務器

看起來像Shiny和RODBC之間必定存在某種類型的交互,導致了這個問題。作爲一種解決方法,我們正在使用RJDBC,它運行良好,但我們擔心它不會很好地適合我們。而且速度很慢。

任何人都有一個想法,我們可以做些什麼來使RODBC正常工作?

這是我們正在運行產生錯誤的代碼:

library(RODBC) 
myConn <- odbcConnect("DSN_Name") 
segment <- sqlQuery(myConn, "SELECT * FROM foo") 

如果您使用的是基本閃亮的應用程序,並從上面你就可以重新創建錯誤添加代碼。這裏是RStudio的代碼。

ui.R

library(shiny) 
# Define UI for application that draws a histogram 
shinyUI(fluidPage(
# Application title 
titlePanel("Hello Shiny!"), 
# Sidebar with a slider input for the number of bins 
sidebarLayout(
    sidebarPanel(
    sliderInput("bins", 
       "Number of bins:", 
       min = 1, 
       max = 50, 
       value = 30) 
), 

# Show a plot of the generated distribution 
mainPanel(
    plotOutput("distPlot") 
) 
) 
    )) 

server.R

library(shiny) 
# Define server logic required to draw a histogram 
shinyServer(function(input, output) { 
# Expression that generates a histogram. The expression is 
# wrapped in a call to renderPlot to indicate that: 
# 
# 1) It is "reactive" and therefore should re-execute automatically 
#  when inputs change 
# 2) Its output type is a plot 
output$distPlot <- renderPlot({ 
x <- faithful[, 2] # Old Faithful Geyser data 
bins <- seq(min(x), max(x), length.out = input$bins + 1) 

# draw the histogram with the specified number of bins 
hist(x, breaks = bins, col = 'darkgray', border = 'white') 
}) 
    }) 

這裏是來自服務器的錯誤消息:

An error has occurred 

The application exited unexpectedly. 

socket hang up 

Listening on http://127.0.0.1:37079 Attaching package: ‘shinydashboard’ 

The following object is masked from ‘package:graphics’: box 

Loading required package: ggplot2 

Attaching package: ‘plotly’ The following object is masked from ‘package:ggplot2’: last_plot 

The following object is masked from ‘package:graphics’: layout Attaching package: ‘DT’ 

The following objects are masked from ‘package:shiny’: dataTableOutput, renderDataTable 

Loading required package: DBI Loading required package: rJava Loading required package: 

NLP Attaching package: ‘NLP’ The following object is masked from ‘package:ggplot2’: annotate 

Loading required package: RColorBrewer 

*** caught segfault *** 
address (nil), cause 'unknown' 

Traceback: 
1: .Call(C_RODBCDriverConnect, as.character(connection), id, as.integer(believeNRows), as.logical(readOnlyOptimize)) 
2: odbcDriverConnect("DSN=DB_Name") 
3: eval(expr, envir, enclos) 
4: eval(expr, p) 
5: eval.parent(Call) 
6: odbcConnect("DB_Name") 
7: eval(expr, envir, enclos) 
8: eval(ei, envir) 
9: withVisible(eval(ei, envir)) 
10: source("RODBC.R") 
11: eval(expr, envir, enclos) 
12: eval(ei, envir) 
13: withVisible(eval(ei, envir)) 
14: source(file, ..., keep.source = TRUE, encoding = checkEncoding(file)) 
15: sourceUTF8(fullpath, local = new.env(parent = globalenv())) 
16: func(fname, ...) 
17: appObj() 
18: handler(req) 
19: handler(req) 
20: handler(...) 
21: handlers$invoke(req) 
22: handler(req) 
23: func(req) 
24: doTryCatch(return(expr), name, parentenv, handler) 
25: tryCatchOne(expr, names, parentenv, handlers[[1L]]) 
26: tryCatchList(expr, classes, parentenv, handlers) 
27: tryCatch(expr, error = function(e) { call <- conditionCall(e) if (!is.null(call)) { if (identical(call[[1L]], quote(doTryCatch))) call <- sys.call(-4L) dcall <- deparse(call)[1L] prefix <- paste("Error in", dcall, ": ") LONG <- 75L msg <- conditionMessage(e) sm <- strsplit(msg, "\n")[[1L]] w <- 14L + nchar(dcall, type = "w") + nchar(sm[1L], type = "w") if (is.na(w)) w <- 14L + nchar(dcall, type = "b") + nchar(sm[1L], type = "b") if (w > LONG) prefix <- paste0(prefix, "\n ") } else prefix <- "Error : " msg <- paste0(prefix, conditionMessage(e), "\n") .Internal(seterrmessage(msg[1L])) if (!silent && identical(getOption("show.error.messages"), TRUE)) { cat(msg, file = stderr()) .Internal(printDeferredWarnings()) } invisible(structure(msg, class = "try-error", condition = e))}) 
28: try({ inputStream <- if (is.null(data)) nullInputStream else InputStream$new(data, dataLength) req$rook.input <- inputStream req$rook.errors <- ErrorStream$new() req$httpuv.version <- packageVersion("httpuv") if (!is.null(req$HTTP_CONTENT_TYPE)) req$CONTENT_TYPE <- req$HTTP_CONTENT_TYPE if (!is.null(req$HTTP_CONTENT_LENGTH)) req$CONTENT_LENGTH <- req$HTTP_CONTENT_LENGTH resp <- func(req) if (is.null(resp) || length(resp) == 0) return(NULL) resp$headers <- lapply(resp$headers, paste) if ("file" %in% names(resp$body)) { filename <- resp$body[["file"]] owned <- FALSE if ("owned" %in% names(resp$body)) owned <- as.logical(resp$body$owned) resp$body <- NULL resp$bodyFile <- filename resp$bodyFileOwned <- owned } resp}) 
29: rookCall(.app$call, req, req$.bodyData, seek(req$.bodyData)) 
30: (function (req) { on.exit({ if (!is.null(req$.bodyData)) { close(req$.bodyData) } req$.bodyData <- NULL }) rookCall(.app$call, req, req$.bodyData, seek(req$.bodyData))})(<environment>) 
31: eval(substitute(expr), envir, enclos) 
32: evalq((function (req) { on.exit({ if (!is.null(req$.bodyData)) { close(req$.bodyData) } req$.bodyData <- NULL }) rookCall(.app$call, req, req$.bodyData, seek(req$.bodyData))})(<environment>), <environment>) 
33: doTryCatch(return(expr), name, parentenv, handler) 
34: tryCatchOne(expr, names, parentenv, handlers[[1L]]) 
35: tryCatchList(expr, names[-nh], parentenv, handlers[-nh]) 
36: doTryCatch(return(expr), name, parentenv, handler) 
37: tryCatchOne(tryCatchList(expr, names[-nh], parentenv, handlers[-nh]), names[nh], parentenv, handlers[[nh]]) 
38: tryCatchList(expr, classes, parentenv, handlers) 
39: tryCatch(evalq((function (req) { on.exit({ if (!is.null(req$.bodyData)) { close(req$.bodyData) } req$.bodyData <- NULL }) rookCall(.app$call, req, req$.bodyData, seek(req$.bodyData))})(<environment>), <environment>), error = function (x) x, interrupt = function (x) x) 
40: .Call("httpuv_run", PACKAGE = "httpuv", timeoutMillis) 
41: run(timeoutMs) 
42: service(timeout) 
43: serviceApp() 
44: withCallingHandlers(expr, error = function(e) { handle <- getOption("shiny.error") if (is.function(handle)) handle()}) 
45: shinyCallingHandlers(while (!.globals$stopped) { serviceApp() Sys.sleep(0.001)}) 
46: runApp(Sys.getenv("SHINY_APP"), port = port, launch.browser = FALSE) aborting ... 
+0

可以格式化的錯誤以純文本格式,因此是可讀的? – kippandrew

+0

我重新格式化了錯誤,使其更具可讀性 – wordscommentscode

回答

0

首先,我要感謝RStudio的Joe Cheng。他是一個巨大的幫助,真的竭盡全力幫助我解決這個問題。萬分感謝。

所以我遇到的問題是(恕我直言)糟糕的代碼結構的結果。這是我犯的錯誤,一旦他們確定事情按預期工作。

  • 錯誤1 - 我打電話給server.R文件app.R.這是一個 過時的命名約定,不應使用。
  • 錯誤2 - 我的server.R文件的最後一行叫做函數shinyApp(ui, server)。這是我的odbc驅動程序導致我的 數據庫的主要問題。這是創造一個非常大的堆棧,我的客戶將 推得太低,導致seg故障。道德故事, 結束您的server.R文件與服務器功能。

我的閃亮的服務器現在快樂地運行! :)

感謝喬的所有幫助。

下面是完整的話題 - https://groups.google.com/forum/#!topic/shiny-discuss/66Ve60ugmHs