Why does dbListTables give a warning when called through a function? (R DBI)

I wrote a function using dbListTables from a DBI package that throws a warning that I cannot understand. When I run the same code outside the function, I do not receive a warning message.

For information, the Microsoft SQL Server database is used.

Reproducible example

library(odbc)
library(DBI)

# dbListTables in a function: gives a warning message

dbListTablesTest <- function(dsn, userName, password){

  con <- dbConnect(
    odbc::odbc(),
    dsn      = dsn,
    UID      = userName,
    PWD      = password,
    Port     = 1433,
    encoding = "latin1"
  )

  availableTables <- dbListTables(con)
}

availableTables <- 
  dbListTablesTest(
    dsn = "myDsn"
    ,userName = myLogin
    ,password = myPassword
  )

# dbListTables not within a function works fine (no warnings)

con2 <- dbConnect(
  odbc::odbc(),
  dsn      = "myDsn",
  UID      = myLogin,
  PWD      = myPassword,
  Port     = 1433,
  encoding = "latin1"
)

availableTables <- dbListTables(con2)

(By the way, I understand that I have to use dbDisconnect to close the connection after working with it, but it seems to trigger similar warnings. Therefore, for simplicity, I skipped dbDisconnect.)

Warning message

When executing the above code, I get the following warning message when using the first option (via a function), but I do not get it when using the second option (without a function).

warning messages from top-level task callback '1'
Warning message:
Could not notify connection observer. trying to get slot "info" from an object of a basic class ("character") with no slots 

dbListTables, , .

  • ?
  • , dbListTables ?
  • / , ?

R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=Dutch_Belgium.1252  LC_CTYPE=Dutch_Belgium.1252    LC_MONETARY=Dutch_Belgium.1252 LC_NUMERIC=C                   LC_TIME=Dutch_Belgium.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  tools     methods   base     

other attached packages:
[1] DBI_0.7    odbc_1.1.3

loaded via a namespace (and not attached):
[1] bit_1.1-12     compiler_3.4.2 hms_0.3        tibble_1.3.4   Rcpp_0.12.13   bit64_0.9-7    blob_1.1.0     rlang_0.1.2  

!

+4
1

TL: DR odbc::dbConnect .

odbc github . dbConnect db. :

# perform the connection notification at the top level, to ensure that it had
# a chance to get its external pointer connected, and so we can capture the
# expression that created it
if (!is.null(getOption("connectionObserver"))) { # nocov start
  addTaskCallback(function(expr, ...) {
    tryCatch({
      if (is.call(expr) && identical(expr[[1]], as.symbol("<-"))) {
        # notify if this is an assignment we can replay
        on_connection_opened(eval(expr[[2]]), paste(
          c("library(odbc)", deparse(expr)), collapse = "\n"))
      }
    }, error = function(e) {
      warning("Could not notify connection observer. ", e$message, call. = FALSE)
    })

    # always return false so the task callback is run at most once
    FALSE
  })
} # nocov end

warning . , . ?

, , .

, , "TaskCallBack". , top-level task. 100%, , , , .

script. , :

library(odbc)
con <- odbc::dbConnect(odbc::odbc(), ...)

:

function(expr, ...) {
    tryCatch({
      if (is.call(expr) && identical(expr[[1]], as.symbol("<-"))) {
        # notify if this is an assignment we can replay
        on_connection_opened(eval(expr[[2]]), paste(
          c("library(odbc)", deparse(expr)), collapse = "\n"))
      }
    }, error = function(e) {
      warning("Could not notify connection observer. ", e$message, call. = FALSE)
    }
}

. odbc, on_connection_opened, . , - tryCatch.

on_connection_opened?

:

on_connection_opened <- function(connection, code)

, , :

display_name <- connection@info$dbname

, :

"" ( "" )

, on_connection_opened . ? eval(expr[[2]])

: con

, .

, :

?

, . . , , . .

, dbListTables ?

dbListTables , dbConnect is. , , .

/ , ?

. , , .

, TaskCallback:

  before <- getTaskCallbackNames()
  con <- odbc::dbConnect(odbc::odbc(), ...)
  after <- getTaskCallbackNames()
  removeTaskCallback(which(!after %in% before))

on_connection_opened ? ?

Github, RStudio. , . , .

+4

Source: https://habr.com/ru/post/1687436/


All Articles