반응형
/*******************************************************************************************************************
-- Title : [MSR] Microsoft R 설정 및 SQL Server 연결하기
-- Reference : microsoft.com
-- Key word : SQL Server R Services revoscaler rxinsqlserver rxsetcomputecontext rxsqlserverdata rxsummary
rxgetvarinfo r studio microsoft r
*******************************************************************************************************************/
-- R Version 변경
-- SQL R
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | # *********************************************** # -- Connect SQL Server R Service on CLIENT # *********************************************** # R Studio > Tools > Global Options > R Version 변경 # "[64-bit] C:\Program Files\Microsoft SQL Server\130\R_SERVER" 선택 # --------------------------- # -- Install Package # --------------------------- library(RevoScaleR) help("RevoScaleR") help("RxSqlServerData") # --------------------------- # -- Define the connection # --------------------------- # -- This walkthrough requires SQL authentication connStr = "Driver=SQL Server;Server=localhost;Database=TaxiNYC_Sample;Uid=usr_id;Pwd=******" # -- Set ComputeContext. # Needs a temp directory path to serialize R objects back and forth sqlShareDir = paste("C:\\tempR\\",Sys.getenv("USERNAME"),sep="") sqlWait = TRUE sqlConsoleOutput = FALSE cc = RxInSqlServer(connectionString = connStr, shareDir = sqlShareDir, wait = sqlWait, consoleOutput = sqlConsoleOutput) rxSetComputeContext(cc) # 지난/현재 활성 컨텍스트 반환? # --------------------------- # -- Define a DataSource (from a select query) # --------------------------- # -- Keep in mind that inDataSource is just a reference to the result dataset from the SQL query. sampleDataQuery = "select top 200000 tipped, fare_amount, passenger_count,trip_time_in_secs,trip_distance , pickup_datetime, dropoff_datetime, cast(pickup_longitude as float) as pickup_longitude , cast(pickup_latitude as float) as pickup_latitude , cast(dropoff_longitude as float) as dropoff_longitude , cast(dropoff_latitude as float) as dropoff_latitude from nyctaxi_sample" inDataSource = RxSqlServerData( sqlQuery = sampleDataQuery, connectionString = connStr , colClasses = c(pickup_longitude = "numeric", pickup_latitude = "numeric" , dropoff_longitude = "numeric", dropoff_latitude = "numeric") , rowsPerRead=500) # Chunk like python head(inDataSource) class(inDataSource) str(inDataSource) mode(inDataSource) # *********************************************** # -- Data exploration # *********************************************** # -- Get Variables Info rxGetVarInfo(data = inDataSource) # rxGetVarInfo : 필드명, 데이터 타입 출력 # -- Summarize Statistics start.time = proc.time() rxSummary(~fare_amount:F(passenger_count,1,6), data = inDataSource) # 승객 수에 따른 요금을 요약 used.time = proc.time() - start.time print(paste("It takes CPU Time=", round(used.time[1]+used.time[2],2)," seconds, Elapsed Time=", round(used.time[3],2), " seconds to summarize the inDataSource.", sep="")) | cs |
반응형