cat() 不会每次都在 for 循环中输出

cat() doesn't output everytime in a for loop

我有一个循环,它从列表中每个数据库的每个 table 的每个字段中获取一些信息(因此 3 个嵌套 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循环结构的完整代码如下:

## 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)
}

它是阻止它在后续循环中到达打印语句的三个 if 语句之一。

对于 44.53% 之后的情况,!grepl("\?|^sys",Tables[j])ncol(Columns)!=0grepl("\?\?\?\?|DoB",Columns[k])!=T 都不成立。