2015-11-12 23 views
0

我有一個循環從每個表的每個字段獲取一些信息,從列表中的每個數據庫(因此3個嵌套for循環)獲取一些信息。因爲它是很難知道我在哪個週期和我的互聯網經常胡扯了,我決定吐出的每個循環週期的快速線......這是代碼的樣子:cat()不會每次都在for循環中輸出

for (i in 1:nrow(conns)){ 
    ## Connect to each db in the dataframe conns and get a list of the Tables 

    ## FOR EACH TABLE 
    for (j in 1:length(Tables)){ 

     ## FOR EACH COLUMN (or field) 
     for (k in 1:length(Columns)){ 
      ## do stuff, and then: 

      cat(paste(i," of ",nrow(conns),"; ",round(j/length(Tables)*100,2),"%; ",k, 
         " of ",length(Columns),"; \n",sep="")) 

     } 


    } 
    close(channel) 
} 

這是貓的輸出:

14 of 14; 43.77%; 1 of 5; 
14 of 14; 43.77%; 2 of 5; 
14 of 14; 43.77%; 3 of 5; 
14 of 14; 43.77%; 4 of 5; 
14 of 14; 43.77%; 5 of 5; 
14 of 14; 44.15%; 1 of 4; 
14 of 14; 44.15%; 2 of 4; 
14 of 14; 44.15%; 3 of 4; 
14 of 14; 44.15%; 4 of 4; 
14 of 14; 44.53%; 1 of 4; 
14 of 14; 44.53%; 2 of 4; 
14 of 14; 44.53%; 3 of 4; 
14 of 14; 44.53%; 4 of 4; 
> 

然後循環結束時沒有錯誤。但爲什麼它停在44.5%?所以我決定檢查循環是否已經手動更改,並獲得以下輸出:

> length(Tables) 
[1] 265 
> j 
[1] 265 
> cat(paste(i," of ",nrow(conns),"; ",round(j/length(Tables)*100,2),"%; ",k, 
+ " of ",length(Columns),"; \n",sep="")) 
14 of 14; 100%; 4 of 0; 

那麼,爲什麼在j%的停止輸出爲44.53%,而實際上它是正確的100%?

編輯:對於下面循環結構的完整代碼:

## FOR EACH PROJECT 
for (i in 13:nrow(conns)){ 
    d <- conns[i,1] %>% as.character 
    p <- conns[i,3] %>% as.character 
    u <- conns[i,2] %>% as.character 

    channel <- odbcConnect(d,u,p) 

    ## Find out what tables are available 
    sqlTables(channel) %>% select(TABLE_NAME) -> Tables 
    Tables <- as.vector(Tables[,1]) 

    ## Throw out long uuid ones 
    Tables <- Tables[!(substr(Tables,9,9) == "-" & nchar(Tables) == 36)] 

    ## FOR EACH TABLE 
    for (j in 1:length(Tables)){ 
    ## Check that Table name doesn't have questionmarks or starts with sys 
    if (!grepl("\\?|^sys",Tables[j])){ 
     ##GET COLUMNS 
     Columns <- as.data.frame(colnames(
     sqlFetch(channel, Tables[j], rows_at_time = 5,max=1))) 
     ## Check that there's at least 1 column 
     if (ncol(Columns)!=0){ 
     Columns <- as.vector(Columns[,1]) 

     ## FOR EACH COLUMN 
     for (k in 1:length(Columns)){ 
      if(grepl("\\?\\?\\?\\?|DoB",Columns[k])!=T){ 
      db1[l,1] <- d 
      db1[l,2] <- Tables[j] 
      db1[l,3] <- Columns[k] 

      AC <- paste('"',as.character(Columns[k]),'"',sep="") 
      Q <- paste('SELECT COUNT(',AC,') AS Count1, 
         COUNT(DISTINCT (',AC,')) AS Count2 FROM "',Tables[j],'"',sep="") 
      Result <- sqlQuery(channel, Q, rows_at_time = 5) 
      db1[l,4] <- Result[1,1] 
      db1[l,5] <- Result[1,2] 
      cat(paste(i," of ",nrow(conns),"; ",round(j/length(Tables)*100,2),"%; ",k, 
         " of ",length(Columns),"; \n",sep="")) 
      l <- l + 1 
      } 
     } 
     } 
    } 
    } 
    close(channel) 
} 
+2

沒有足夠的信息來說明,我們需要一個可重複的例子......也許你在'j'循環體內意外地創建了一個多餘的'j'變量? –

+0

添加附加信息。 –

+1

@AmitKohli這是阻止它的三個if語句之一。對於44.53%之後的情況,無論是!!grepl(「\\?|^sys」,表[j]),'ncol(Columns)!= 0'還是'grepl(「\\?\\?\\\ ?\\?| DoB「,列[k])!= T'不正確。 –

回答

3

這三個中的一個,如果是在隨後的循環到達print語句停止它的語句。

對於44.53%後的情況,無論是!grepl("\\?|^sys",Tables[j]),ncol(Columns)!=0還是grepl("\\?\\?\\?\\?|DoB",Columns[k])!=T都是不正確的。