2017-12-29 4 views
-1

저는 R에 익숙하지 않아 일부 지리적 출력으로 함수를 만들려고합니다. 지도 제목과 같은 특정 매개 변수에 대한 기본값을 설정하는 if/else 문을 포함하려고하지만 동일한 문제가 반복적으로 발생했습니다. 다른 사람들도 같은 문제를 가지고 있지만 그들의 질문이 나를 도와주지 않은 응답을 가지고 있습니다.if/else 문에서 중괄호 관련 문제

다음은 현재 발생하고있는 오류는 물론 단순화 된 코드입니다.

my_function <- function(x, y, map.title, a, ...){ 
    neighbours <- spdep::poly2nb(x, queen=T, snap=T) 
    print("neighbours defined") 
    local <- spdep::localmoran(y, listw=nb2listw(neighbours, style="W")) 
    moran_map <- x 
    [email protected] <- cbind([email protected], local) 
    if(map.title = NULL) { 
    seg_map <- tmap::tm_shape(moran_map) + 
       tm_fill(col = "Ii", 
         style = "quantile", 
         title = "Local Moran's I Statistic") + 
       tm_layout(title = "Good Maps Have Titles") 
    return(map) 
} else { 
    seg_map <- tmap::tm_shape(moran_map) + 
        tm_fill(col = 'Ii', 
          style = 'quantile', 
          title = "Local Moran's I Statistic", 
          palette = a) + 
        tm_layout(title = map.title) 
    return(map) 
    } 
} 

다음 오류가 계속 발생합니다.

>Error: no function to return from, jumping to top level 

> } 
Error: unexpected '}' in " }" 

> } 
Error: unexpected '}' in " }" 

누구나 내가 뭘 잘못하고 있는지 어떻게 알 수 있습니까?

감사합니다. 당신이 그 오류를 가지고 전에

+3

R :'map.title = NULL'에서는 작동하지 않습니다. 'isnull()'을 사용하십시오. – Parfait

+3

그리고 * map *은 어디 있습니까? 'return (seg_map)'을 사용하십시오. – Parfait

답변

0

, 당신은 하나 이전했다 : 당신은 if 시험에서 단일 =를 사용할 수 없습니다

+ if(map.title = NULL) { 
Error: unexpected '=' in: 
" [email protected] <- cbind([email protected], local) 
    if(map.title =" 

. 일반적으로 ==을 사용하여 값의 동일성을 테스트하지만 NULL은 특별하므로 여기서는 is.null(map.title) (Parfait이 거의 말한대로)을 원합니다.

R이 해당 명령문에서 오류를 발견하면 함수 정의가 중지되고 다시 시작됩니다. 나중의 진술은 의미가 없으므로 그러한 오류가 발생했습니다.