简体   繁体   中英

How to connect to SQL Server with R?

I want to create a model in R using a connection to data stored in SQL Server datawarehouse. I tried to use RevoScaleR library which returned

package RevoScaleR is not available (for R version 3.4.1)

so, I edited the connection string (given on the code below) for ODBC library:

install.packages("RevoScaleR")
#require("RevoScaleR")

if (!require("RODBC"))
    install.packages("RODBC")

conn <- odbcDriverConnect(connection="Driver={SQL Server Native Client 11.0}; Server=CZPHADDWH01/DEV; Database=DWH_Staging; trusted_connection=true")

sqlWait <- TRUE;
sqlConsoleOutput <- FALSE;
cc <- RxInSqlServer(connectionString = conn, wait = sqlWait)
rxSetComputeContext(cc)

train_query <- "SELECT TOP(10000) * FROM dim.Contract"
formula <- as.formula("Cosi ~ ContractID + ApprovedLoanAmount + ApprovedLoadDuration")
forest_model <- rxDForest(formula = formula,
                            data = train_query,
                            nTree = 20,
                            maxDepth = 32,
                            mTry = 3,
                            seed = 5,
                            verbose = 1,
                            reportProgress = 1)
rxDForest_model <- as.raw(serialize(forest_model, connection = conn))

lenght(rxDForest_model)

However:

package 'RODBC' successfully unpacked and MD5 sums checked

The downloaded binary packages are in C:\\Users\\sjirak\\AppData\\Local\\Temp\\Rtmpqa9iKN\\downloaded_packages Error in odbcDriverConnect(connection = "Driver={SQL Server Native Client 11.0}; Server=CZPHADDWH01/DEV; Database=DWH_Staging; trusted_connection=true") : could not find function "odbcDriverConnect" In library(package, lib.loc = lib.loc, character.only = TRUE, logical.return = TRUE, : there is no package called 'RODBC' Any help would be appreciated.

Looking at the documentation of the ODBC, I see the following functions

odbc-package 
dbConnect,OdbcDriver-method 
dbUnQuoteIdentifier 
odbc 
odbc-tables 
OdbcConnection 
odbcConnectionActions 
odbcConnectionIcon 
odbcDataType 
OdbcDriver 
odbcListColumns 
odbcListDataSources 
odbcListDrivers 
odbcListObjects 
odbcListObjectTypes 
odbcPreviewObject 
OdbcResult 
odbcSetTransactionIsolationLevel 
test_roundtrip

hence I dont see your function in this list. This could be the reason why...

Hence, check the documentation for the proper function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM