Search code examples
rsql-serverdbi

R table from SQL weird behavior


I connected R to SQL using the following:

library(dplyr)
library(dbplyr)
library(odbc)
library(RODBC)
library(DBI)

con <- dbConnect(odbc(), 
                 Driver = "SQL Server", 
                 Server = "srv name", 
                 Database = "Warehouse")

I pull in the table I want using

data <- tbl(con, in_schema("prc", "PricingLawOfUniv")))

The following things show me what I expect to see (a 38 X 1000 table of data):

  1. head(data)
  2. colnames(data)

The following things behave as I expect:

  1. In the Environment data is a "list of 2"
  2. View(data) shows a list with "src" and "ops" - each of those is also a list of 2.

Ultimately I want to work with the 38 X 1000 table as a dataframe using dplyr. How can I do this? I tried data[1] and data[2] but neither worked. Where is the actual table I want hiding?


Solution

  • You could use DBI::Id to specify the table/schema, and then dbReadTable:

    tbl <- DBI::Id(
      schema  = "prc",
      table   = "PricingLawOfUniv"
    )
    data <- DBI::dbReadTable(con, tbl)