4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Rでfor文の進行状況を表示

4
Posted at

はじめに

計算に数分~数時間かかるfor文を実行するとき進行状況を確認したい場合がある。Rにおけるプログレスバーの表示方法について調べたので、備忘録として記事を残しておく。

tcltkパッケージ

プログレスバーの表示にはtcltkパッケージを使えばできる。以下、サンプルコード

library(tcltk)

n <- 1000
pb <- txtProgressBar(min = 1, max = n, style = 3)

a <- c()
for(i in 1:n){
  setTxtProgressBar(pb, i)
  a <- c(a, i^2)
  Sys.sleep(0.1) #あまりに表示が早すぎるためにわざと遅延させている。
}

Rのコンソールの表示は以下の様になる。
tcltk.gif

おわりに

こうした簡単な Tips でも、アウトプットする癖をつけていきたいと思う。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?