2016-12-18 5 views
0

공간 라이브러리 sp의 클래스와 함수를 사용하는 패키지를 작성 중입니다. rbind에 대한 sp 내보내기 메서드 (나는 rbind generic을 호출하는데 정확합니까?). 예를 들어R : 제네릭 메서드가 첨부되지 않은 상태에서 패키지 내부에서 작동하지 않아야합니까?

, 다음 코드는 두 SpatialPoints 객체를 만든 다음 함께 가입 rbind.SpatialPoints을 사용

> crdsA <- matrix(c(1,2,3,4), ncol = 2) 
> crdsB <- matrix(c(7,8), ncol = 2) 
> 
> sptsA <- sp::SpatialPoints(crdsA) 
> sptsB <- sp::SpatialPoints(crdsB) 
> 
> sp::rbind.SpatialPoints(sptsA, sptsB) 
SpatialPoints: 
    coords.x1 coords.x2 
[1,]   1   3 
[2,]   2   4 
[3,]   7   8 
Coordinate Reference System (CRS) arguments: NA 

을하지만, 그때SpatialPointsDataFrame (더 높은 수준으로 SpatialPoints를 변환하는 경우 라이브러리 내에서 객체 클래스)를 선택한 다음 rbind.SpatialPointsDataFrame을 사용하면 오류가 발생합니다.

> sptsdfA <- sp::SpatialPointsDataFrame(sptsA, data.frame(IDs = c(1,2))) 
> sptsdfB <- sp::SpatialPointsDataFrame(sptsB, data.frame(IDs = 3)) 
> 
> sp::rbind.SpatialPointsDataFrame(sptsdfA, sptsdfB) 
Error in rbind2(..1, r) : 
    no method for coercing this S4 class to a vector 
rbind.SpatialPointsDataFrame <- function(...) { 
    dots = list(...) 
    names(dots) <- NULL # bugfix Clement Calenge 100417 
    sp = do.call(rbind, lapply(dots, function(x) as(x, "SpatialPoints"))) 
    df = do.call(rbind, lapply(dots, function(x) [email protected])) 
    SpatialPointsDataFrame(sp, df, coords.nrs = dots[[1]]@coords.nrs) 
} 

그래서이 문제가 될 것 같다,하지만 난 왜 이해가 안 다음 rbind.SpatialPointsDataFramesource code에서

살펴보면은 rbind에 대한 SpatialPoints 호출하는 것을 알 수있다. sp 라이브러리를 첨부하면 이러한 문제는 발생하지 않지만 rbind이 내부적으로 rbind.SpatialPointsDataFrame 내에서 호출 되었기 때문에 나머지 라이브러리는 첨부되지 않았을 것이라고 생각했습니다.

NAMESPACE에 import(sp)importFrom(sp,rbind.SpatialPoints)이 포함되어 있어도 패키지의 컨텍스트 내에서 코드가 작동하지 않습니다.

패키지로드, 첨부 및 가져 오기와 관련하여 명확하게 이해하지 못하는 부분이있을 것입니다. 아무도 왜 sp::rbind.SpatialPointsDataFrame 라이브러리의 나머지 부분이 연결되지 않고 작동하지 않는지 설명하고, 내 패키지 내에서 어떻게 작동시킬 수 있습니까?

고맙습니다.

답변

0

rbind은 일반적인 서명이 아니기 때문에 첫 번째 인수로는 서명이 ...이므로 첫 번째 인수로 전달할 수 없습니다. 모든 경우에있어 방법을 프로그램하는 것이 현명했다. maptools::spRbind을 보셨습니까?

+0

감사합니다. 저는 임시적으로 (또는 영구적 인) 해결책으로'maptools :: spRbind'를 실제로 사용했습니다. 나는 방법 디스패치 [여기] (http://adv-r.had.co.nz/S3.html)에서 읽었고, 약간 이해가된다. 비록 이것이 어떻게 관련되어 있는지 꽤 이해할 수는 없지만 연결되는 패키지에 연결 되나요? –