การอ้างอิงและการคัดลอก

การประมวลผลข้อมูลขนาดใหญ่ใน R

Simon Urbanek

Member of R-Core, Lead Inventive Scientist, AT&T Labs Research

big.matrix และ matrix — ความเหมือน

  • Subset
  • Assign
การประมวลผลข้อมูลขนาดใหญ่ใน R

big.matrix และ matrix — ความต่าง

  • big.matrix ถูกเก็บไว้บนดิสก์
  • คงอยู่ข้ามเซสชัน R
  • แชร์ข้ามเซสชัน R ได้
การประมวลผลข้อมูลขนาดใหญ่ใน R

R มักสร้างสำเนาเมื่อกำหนดค่า

โค้ดนี้สร้างสำเนาของ a แล้วกำหนดให้กับ b

a <- 42
b <- a
a
42
b
42
a <- 43
a
43
b
42
การประมวลผลข้อมูลขนาดใหญ่ใน R

R มักสร้างสำเนาเมื่อกำหนดค่า

a <- 42

foo <- function(a){a <- 43 paste("Inside the function a is", a)}
foo(a)
"Inside the function a is 43"
paste("Outside the function a is still", a)
"Outside the function a is still 42"
การประมวลผลข้อมูลขนาดใหญ่ใน R

R object บางชนิดไม่ถูกคัดลอก

ฟังก์ชันนี้เปลี่ยนค่าของ a ใน global environment จริง

foo <- function(a) {a$val <- 43 
                    paste("Inside the function a is", a$val)}
a <- environment()
a$val <- 42
foo(a)
"Inside the function a is 43"
paste("Outside the function a$val is", a$val)
"Outside the function a$val is 43"
การประมวลผลข้อมูลขนาดใหญ่ใน R

`deepcopy()`

# x is a big matrix
x <- big.matrix(...)

# x_no_copy and x refer to the same object
x_no_copy <- x

# x_copy and x refer to different objects
x_copy <- deepcopy(x)

การประมวลผลข้อมูลขนาดใหญ่ใน R

พฤติกรรมแบบ Reference

R จะไม่สร้างสำเนาโดยอัตโนมัติ

  • ลดการใช้หน่วยความจำ
  • ลดเวลาประมวลผล
การประมวลผลข้อมูลขนาดใหญ่ใน R

R object บางชนิดไม่ถูกคัดลอก

library(bigmemory)

x <- big.matrix(nrow = 1, ncol = 3, type = "double", 
                init = 0, 
                backingfile = "hello-bigmemory.bin", 
                descriptorfile = "hello-bigmemory.desc")
การประมวลผลข้อมูลขนาดใหญ่ใน R

R object บางชนิดไม่ถูกคัดลอก

x_no_copy <- x
x[,]
0 0 0
x_no_copy[,]
0 0 0
x[,] <- 1
x[,]
1 1 1
x_no_copy[,]
1 1 1
การประมวลผลข้อมูลขนาดใหญ่ใน R

R object บางชนิดไม่ถูกคัดลอก

x_copy <- deepcopy(x)
x[,]
1 1 1
x_copy[,]
1 1 1
x[,] <- 2
x[,]
2 2 2
x_copy[,]
1 1 1
การประมวลผลข้อมูลขนาดใหญ่ใน R

มาฝึกกันเถอะ!

การประมวลผลข้อมูลขนาดใหญ่ใน R

Preparing Video For Download...