トップ «前の日記(2012-10-11(Thu)) 最新 次の日記(2012-10-24(Wed))» 編集

屑俺日記

僕の備忘録(PC、UN*X、ネットワーク関連が中心)なんです。
自分の書いたところは適当(な時とか)に書き換えますので御了承を。


2012-10-21(Sun) よく晴れて、モニタの字がよく見えない

Rによる FizzBuzz

if ... else のあたりは、別段難しくない。しかし入力値チェック が少々邪魔臭かった。
面倒になったので、 コマンドライン引数が "3.1" などの場合は 整数3などに変えてしまった。
コマンドライン引数が数値にならない場合は、標準エラーが出るが、無視する。 引数がなければ 1 から 20 まで Fizzbuzz。

#!/usr/bin/env Rscript
 
numcheck <-function(arg){
  if(class(arg) != "integer") FALSE
  else if (is.na(arg)) FALSE
  else TRUE
}
 
fb <- function(num) {
  if (num %% 15 == 0) "FizzBuzz"
  else if( num %% 5 == 0) "Buzz"
  else if( num %% 3 == 0) "Fizz"
  else num
}
 
check = as.integer(commandArgs()[6])
if (! numcheck(check)) {
  for (x in c(1:20))
    print (fb(x))
} else fb(check)

/bin/bash: /home/$USER/foo.txt: 許可がありません ...viでテンポラリに保存したつもりが、手が滑ったのが敗因らしい。

RのARGVみたいなもの

$ cat commandline.R 
#!/usr/bin/env Rscript
print(commandArgs())
 
$ Rscript commandline.R 
[1] "/usr/lib/R/bin/exec/R" "--slave"               "--no-restore"         
[4] "--file=commandline.R"  "--args"  
 
$ ./commandline.R 
[1] "/usr/lib/R/bin/exec/R"  "--slave"                "--no-restore"          
[4] "--file=./commandline.R" "--args"    

リンクはご自由にどうぞ。でもURLや内容が変った場合はあしからず。

index.htmlは ここから。