1

R の switch ステートメントは、たとえば C++ のように機能するように設計されていないことは知っていますが、ドキュメントを読んだところ、以下が機能しない理由がわかりません。

file.types <- c('bmp', 'jpeg', 'png', 'tiff', 'eps', 'pdf', 'ps')
  if(tolower(file.type) %in% file.types) {
    switch(file.type,
           bmp = bmp(filename=paste(file.location, file.name, '.',
                                    file.type, sep=''), 
                     width=res[2], height=res[1])
           jpeg = jpeg(filename=paste(file.location, file.name, '.',
                                      file.type, sep=''),
                     width=res[2], height=res[1])
           png = png(filename=paste(file.location, file.name, '.',
                                    file.type, sep=''),
                     width=res[2], height=res[1])
           tiff = tiff(filename=paste(file.location, file.name, '.',
                                      file.type, sep=''),
                       width=res[2], height=res[1])
           eps = postscript(filename=paste(file.location, file.name, '.',
                                           file.type, sep=''),
                            width=res[2], height=res[1])
           pdf = postscript(filename=paste(file.location, file.name, '.',
                                           file.type, sep=''),
                            width=res[2], height=res[1])
           ps = postscript(filename=paste(file.location, file.name, '.',
                                          file.type, sep=''),
                           width=res[2], height=res[1]))  
  } else {
      stop(paste(file.type,' is not supported', sep=''))
  }

file.type が「jpeg」の場合、次のエラーが発生します

Error: unexpected symbol in:
"           bmp = {bmp(filename=paste(file.location, file.name, '.', file.type, sep=''), width=res[2], height=res[1])}
       jpeg"

洞察に感謝します!

4

1 に答える 1

1

これは構文エラーです。,の各オプションの末尾に (コンマ)がありませんswitch。たとえば、

switch(file.type,
       bmp = bmp(filename=paste(file.location, file.name, '.', 
                                file.type, sep=''), 
                 width=res[2], height=res[1]),
                                             ^ here

一般的な形式は

switch(foo,
       opt1 = statement1,
       opt2 = statement2,
       opt3 = ,
       opt4 = statement3)

と の両方が の値opt3opt4返しますstatement3

于 2013-07-25T00:29:02.637 に答える