Your SlideShare is downloading. ×
Rデバッグあれこれ
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Rデバッグあれこれ

6,612
views

Published on

Tokyo.R #16 で発表したRのデバッグまとめです

Tokyo.R #16 で発表したRのデバッグまとめです

Published in: Technology

0 Comments
15 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,612
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
89
Comments
0
Likes
15
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. R Tokyo.R #16 (2011/08/27) @a_bicky
  • 2. • Takeshi Arabiki 1 ‣ Twitter: @a_bicky ‣ : id:a_bicky• R• http://d.hatena.ne.jp/a_bicky/
  • 3. Osaka.R #4http://www.slideshare.net/abicky/twitterr http://www.slideshare.net/abicky/introduction-to-hadoop-mapreduce
  • 4. • R debugger•••• ksvm••
  • 5. debug : http://ja.wikipedia.org/wiki/http://getfirebug.com/whatisfirebug R
  • 6. R debugger
  • 7. R debugger‣ R debugger‣‣‣‣‣‣
  • 8. R debugger Perl R debugger gdb debugger where bt T n n n debugonce(func※); c s s browserSetDebug(); c fin r c c c Q q q ※func<RET> debug n browser c n noptions(browserNLdisabled = TRUE) <RET>
  • 9. browserf <- function(x) { y <- x * 2 browser() # z <- y^2 return(z)}
  • 10. browserf <- function(x) { > f(10) y <- x * 2 Called from: f(10) Browse[1]> z <- y^2 return(z)}
  • 11. browserf <- function(x) { > f(10) y <- x * 2 Called from: f(10) Browse[1]> ls() z <- y^2 [1] "x" "y" return(z) Browse[1]>}
  • 12. browserf <- function(x) { > f(10) y <- x * 2 Called from: f(10) Browse[1]> ls() z <- y^2 [1] "x" "y" return(z) Browse[1]> n #} debug: z <- y^2 Browse[2]>
  • 13. browserf <- function(x) { > f(10) y <- x * 2 Called from: f(10) Browse[1]> ls() z <- y^2 [1] "x" "y" return(z) Browse[1]> n #} debug: z <- y^2 Browse[2]> z Error: object z not found Browse[2]> z
  • 14. browserf <- function(x) { > f(10) y <- x * 2 Called from: f(10) Browse[1]> ls() z <- y^2 [1] "x" "y" return(z) Browse[1]> n #} debug: z <- y^2 Browse[2]> z Error: object z not found Browse[2]> n debug: return(z) Browse[2]> z [1] 400 Browse[2]> z y^2
  • 15. browserf <- function(x) { > f(10) y <- x * 2 Called from: f(10) Browse[1]> ls() z <- y^2 [1] "x" "y" return(z) Browse[1]> n #} debug: z <- y^2 Browse[2]> z Error: object z not found Browse[2]> n debug: return(z) Browse[2]> z [1] 400 Browse[2]> z <- 1 # z Browse[2]> n [1] 1 > f(10) z
  • 16. debugf <- function(x) { > debug(f) y <- x * 2 > f(10) z <- y^2 debugging in: f(10) return(z) debug: {} y <- x * 2 z <- y^2 return(z) } Browse[2]> n debug: y <- x * 2 browser Browse[2]> n debug: z <- y^2 Browse[2]> n debug: return(z) Browse[2]> n exiting from: f(10) [1] 400 >
  • 17. debugonce> debug(f) > debugonce(f)> f(10) > f(10)debugging in: f(10) debugging in: f(10)debug: { debug: { y <- x * 2 y <- x * 2 z <- y^2 z <- y^2 return(z) return(z)} }Browse[2]> Q # Browse[2]> Q #> isdebugged(f) > isdebugged(f)[1] TRUE [1] FALSE> f(10) > f(10)debugging in: f(10) [1] 400... >※ undebug(f)
  • 18. tracebackf <- function(x) { > f(1:5) x <- as.character(x) Error in sum(x) : invalid return(g(x)) type (character) of argument} > traceback() 3: h(x) hg <- function(x) { 2: g(x) if (length(x) > 1) { 1: f(1:10) return(h(x)) > debug(h) } else { > f(1:10) return(x) debugging in: h(x) } debug: {} return(sum(x)) }h <- function(x) { Browse[2]> x return(sum(x)) [1] "1" "2" "3" "4" "5"} Browse[2]> sum(x) Error in sum(x) : invalid type (character) of argument
  • 19. browserf <- function(x) { > f(5) # y = 10 y <- x * 2 [1] 100 browser(expr = y > 10) > f(10) # y = 20 z <- y^2 Called from: f(10) return(z) Browse[1]>}f <- function(x) { y <- x * 2 if (y > 10) { browser() } z <- y^2 return(z)}
  • 20. browserTextf <- function(x) { > f(10) browser("1st bt") Called from: f(10) Browse[1]> where # # < > where 1: f(10) browser("2nd bt")} Browse[1]> browserText() [1] "1st bt" Browse[1]> c Called from: f(10) Browse[1]> where # where 1: f(10) Browse[1]> browserText() [1] "2nd bt"
  • 21. ‣‣‣
  • 22. head> head(iris) # 6 Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa4 4.6 3.1 1.5 0.2 setosa5 5.0 3.6 1.4 0.2 setosa6 5.4 3.9 1.7 0.4 setosa> head(iris, 3) # 3 Sepal.Length Sepal.Width Petal.Length Petal.Width Species1 5.1 3.5 1.4 0.2 setosa2 4.9 3.0 1.4 0.2 setosa3 4.7 3.2 1.3 0.2 setosa>※ tail
  • 23. summary> summary(m <- lm(weight ~ group, PlantGrowth))Call:lm(formula = weight ~ group, data = PlantGrowth)Residuals: Min 1Q Median 3Q Max-1.0710 -0.4180 -0.0060 0.2627 1.3690Coefficients: Estimate Std. Error t value Pr(>|t|)(Intercept) 5.0320 0.1971 25.527 <2e-16 ***grouptrt1 -0.3710 0.2788 -1.331 0.1944grouptrt2 0.4940 0.2788 1.772 0.0877 .---Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1Residual standard error: 0.6234 on 27 degrees of freedomMultiple R-squared: 0.2641, Adjusted R-squared: 0.2096F-statistic: 4.846 on 2 and 27 DF, p-value: 0.01591
  • 24. str> m # print.lm(m)Call:lm(formula = weight ~ group, data = PlantGrowth)Coefficients:(Intercept) grouptrt1 grouptrt2 5.032 -0.371 0.494> str(m)List of 13 $ coefficients : Named num [1:3] 5.032 -0.371 0.494 ..- attr(*, "names")= chr [1:3] "(Intercept)" "grouptrt1""grouptrt2"> m$coefficients #(Intercept) grouptrt1 grouptrt2 5.032 -0.371 0.494
  • 25. class> class(m)[1] "lm"data.frame matrixcharacter factor> (x <- c(b = "this is b", a = "this is a")) b a"this is b" "this is a"> (index <- factor(c("a", "b")))[1] a bLevels: a b> x[index] # b a"this is b" "this is a"> x[as.character(index)] # a b"this is a" "this is b"
  • 26. coef> m$coefficients(Intercept) grouptrt1 grouptrt2 5.032 -0.371 0.494> coef(m)(Intercept) grouptrt1 grouptrt2 5.032 -0.371 0.494
  • 27. showMethods> coefstandardGeneric for "coef" defined from package "stats"function (object, ...)standardGeneric("coef")<environment: 0x100f8d4a0>Methods may be defined for arguments: objectUse showMethods("coef") for currently available ones.> showMethods("coef") # coefFunction: coef (package stats)object="ANY"object="gausspr"object="kfa"object="kqr" lmobject="ksvm"object="lm" selectMethod (inherited from: object="ANY")object="lssvm"object="rvm"
  • 28. selectMethod, methods> selectMethod("coef", signature = "lm") # signatureMethod Definition (Class "derivedDefaultMethod"):function (object, ...)UseMethod("coef")<environment: namespace:stats>Signatures: objecttarget "ANY"defined "ANY"> methods(coef) # UseMethod methods[1] coef.aov* coef.Arima* coef.default* coef.listof*coef.nls* Non-visible functions are asterisked> coef.default # coef.lm coef.defaultError: object coef.default not found
  • 29. selectMethod, methods> selectMethod("coef", signature = "lm") # signatureMethod Definition (Class "derivedDefaultMethod"):function (object, ...)UseMethod("coef")<environment: namespace:stats>Signatures: objecttarget "ANY"defined "ANY"> methods(coef) # UseMethod methods[1] coef.aov* coef.Arima* coef.default* coef.listof*coef.nls* * Non-visible functions are asterisked> coef.default # coef.lm coef.defaultError: object coef.default not found
  • 30. package:::func> stats:::coef.default # Non-visible pkg:::funcfunction (object, ...)object$coefficients<environment: namespace:stats>> evalq(coef.default, environment(lm)) #function (object, ...)object$coefficients<environment: namespace:stats>> debug(stats:::coef.default)> coef(m)debugging in: coef.default(m)debug: object$coefficientsBrowse[2]>
  • 31. ‣ trace‣ recover‣ options(error = recover)‣ options(error = dump.frames)
  • 32. trace tracetrace(what, tracer, exit, at, print, signature, where = topenv(parent.frame()), edit = FALSE)what:tracer:exit:at:print:signature:where:edit:
  • 33. trace trace 3 5f <- function(x) { > trace(f, browser, at = c(3, 5)) cat("1st linen") > f() if (TRUE) { 1st line cat("1st 2nd linen") Tracing f() step 3 cat("2nd 2nd linen") Called from: eval(expr, envir, } enclos) Browse[1]> c cat("3rd line 1st 2nd line") 2nd 2nd line cat("4th linen") 3rd line} Tracing f() step 5 Called from: eval(expr, envir, enclos) Browse[1]> c 4th line
  • 34. trace trace 3 5f <- function(x) { > trace(f, browser, at = c(3, 5)) cat("1st linen") > f() if (TRUE) { 1st line cat("1st 2nd linen") Tracing f() step 3 cat("2nd 2nd linen") Called from: eval(expr, envir, } enclos) Browse[1]> c cat("3rd line 1st 2nd line") 2nd 2nd line cat("4th linen") 3rd line} Tracing f() step 5 Called from: eval(expr, envir, enclos) Browse[1]> c 4th line
  • 35. trace trace ff <- function(x) { # 1 > untrace(f) # trace cat("1st linen") # 2 > as.list(body(f)) if (TRUE) { # 3 [[1]] cat("1st 2nd linen") `{` cat("2nd 2nd linen") } # 1 1 [[2]]# cat("1st linen") cat("3rd line [[3]]") # 1 # 4 if (TRUE) { cat("4th linen") # 5 cat("1st 2nd linen")} cat("2nd 2nd linen") } [[4]] cat("3rd linen") [[5]] cat("4th linen")
  • 36. trace trace tracef <- function(x) { > trace(f, browser, at = c(3, 5)) cat("1st linen") [1] "f" if (TRUE) { > body(f) cat("1st 2nd linen") { cat("2nd 2nd linen") cat("1st linen") } { .doTrace(browser(), "step 3") cat("3rd line if (TRUE) {") cat("1st 2nd linen") cat("4th linen") cat("2nd 2nd linen")} } } cat("3rd linen") { .doTrace(browser(), "step 5") cat("4th linen") } }
  • 37. trace trace tracef <- function(x) { > trace(f, browser, at = c(3, 5)) cat("1st linen") [1] "f" if (TRUE) { > body(f) cat("1st 2nd linen") { cat("2nd 2nd linen") cat("1st linen") } { .doTrace .doTrace(browser(), "step 3") cat("3rd line if (TRUE) {") cat("1st 2nd linen") cat("4th linen") cat("2nd 2nd linen")} } } cat("3rd linen") { .doTrace(browser(), "step 5") cat("4th linen") } }
  • 38. trace trace tracef <- function(x) { > f() cat("1st linen") 1st line if (TRUE) { Tracing f() step 3 cat("1st 2nd linen") Called from: eval(expr, envir, cat("2nd 2nd linen") enclos) } Browse[1]> Q > # .doTrace cat("3rd line > tracingState(FALSE)") [1] TRUE cat("4th linen") > f()} 1st line 1st 2nd line 2nd 2nd line 3rd line 4th line > # .doTrace > tracingState(TRUE) [1] FALSE
  • 39. trace tracef <- function(x) { > trace(f, edit = TRUE) # f cat("1st linen") Waiting for Emacs... if (TRUE) { [1] "f" cat("1st 2nd linen") > body(f) cat("2nd 2nd linen") { } cat("1st linen") cat("3rd linen") cat("3rd line browser()") cat("4th linen") cat("4th linen") }} > f() 1st line 3rd line Called from: f()f <- function(x) { Browse[1]> cat("1st linen") cat("3rd linen") ※ .doTrace browser() cat("4th linen") tracingState}
  • 40. recover recoverf <- function(x) { > f(1) x <- g(x) return(x) Enter a frame number, or 0 to exit} 1: f(1)g <- function(x) { 2: g(x) y <- x + 1 3: h(y) y <- h(y) return(y) Selection: 1 # f(1)} Called from: top level Browse[1]> xh <- function(y) { [1] 1 z <- y + 1 recover() return(z)}
  • 41. recover recoverf <- function(x) { Browse[1]> c x <- g(x) return(x) Enter a frame number, or 0 to exit} 1: f(1)g <- function(x) { 2: g(x) y <- x + 1 3: h(y) y <- h(y) return(y) Selection: 2 # g(x)} Called from: f(1) Browse[1]> yh <- function(y) { [1] 2 z <- y + 1 recover() return(z)}
  • 42. recover recoverf <- function(x) { Browse[1]> c x <- g(x) return(x) Enter a frame number, or 0 to exit} 1: f(1)g <- function(x) { 2: g(x) y <- x + 1 3: h(y) y <- h(y) return(y) Selection: 3 # h(y)} Called from: g(x) Browse[1]> zh <- function(y) { [1] 3 z <- y + 1 recover() return(z)}
  • 43. options(error = recover) recover recoverf <- function(x) { > options(error = recover) x <- g(x) > f(1) return(x) Error in print(x) : object x not} foundg <- function(x) { Enter a frame number, or 0 to exit y <- x + 1 y <- h(y) 1: f(1) return(y) 2: g(x)} 3: h(y) 4: print(x)h <- function(y) { z <- y + 1 Selection: 3 # h(y) print(x) # Error! Called from: h(y) return(z) Browse[1]> x} Error during wrapup: object x not found Browse[1]> ls() [1] "y" "z"
  • 44. options(error = dump.frames) dump.framesdump.frames recoverf <- function(x) { > options(error = dump.frames) x <- g(x) > f(1) return(x) Error in print(x) : object x not} found > ls()g <- function(x) { [1] "f" "g" "h" "last.dump" y <- x + 1 > debugger(last.dump) y <- h(y) Message: Error in print(x) : return(y) object x not found} Available environments had calls: 1: f(1)h <- function(y) { 2: g(x) z <- y + 1 3: h(y) print(x) # Error! 4: print(x) return(z)} Enter an environment number, or 0 to exit Selection:
  • 45. ksvm
  • 46. ksvm> library(kernlab)> ksvm(Species ~ .,+ iris,+ class.weights = c(versicolor = 1, setosa = 10),+ prob.model = TRUE)Using automatic sigma estimation (sigest) for RBF or laplacekernelError in lev(ret)[yd[cind]] : only 0s may be mixed with negative subscripts
  • 47. ksvm> options(error = recover) # recover> ksvm(Species ~ .,+ iris,+ class.weights = c(versicolor = 1, setosa = 10),+ prob.model = TRUE)Using automatic sigma estimation (sigest) for RBF or laplace kernelError in lev(ret)[yd[cind]] : only 0s may be mixed with negative subscriptsEnter a frame number, or 0 to exit 1: ksvm(Species ~ ., iris, class.weights = c(versicolor = 1, setosa= 10), pro < > 9: .local(x, ...)10: as.factor(lev(ret)[yd[cind]])11: is.factor(x)12: inherits(x, "factor")Selection: 10
  • 48. ksvmCalled from: top levelBrowse[1]> ls()[1] "x"Browse[1]> xError during wrapup: promise already under evaluation: recursivedefault argument reference or earlier problems?
  • 49. ksvmCalled from: top levelBrowse[1]> ls()[1] "x"Browse[1]> xError during wrapup: promise already under evaluation: recursivedefault argument reference or earlier problems?
  • 50. ksvm> options(error = NULL)> ksvm(Species ~ .,+ iris,+ class.weights = c(versicolor = 1, setosa = 10),+ prob.model = TRUE)Using automatic sigma estimation (sigest) for RBF or laplace kernelError in lev(ret)[yd[cind]] : only 0s may be mixed with negative subscripts> ksvm #standardGeneric for "ksvm" defined from package "kernlab"function (x, ...)standardGeneric("ksvm")<environment: 0x103623e08>Methods may be defined for arguments: xUse showMethods("ksvm") for currently available ones. S4
  • 51. ksvm> showMethods("ksvm")Function: ksvm (package kernlab)x="formula"x="kernelMatrix"x="list"x="matrix"x="vector"> selectMethod(ksvm, "formula")Method Definition:function (x, ...){ .local <- function (x, data = NULL, ..., subset, na.action =na.omit, scaled = TRUE) < > ret <- ksvm(x, y, scaled = scaled, ...) < > signature matrix ksvm
  • 52. ksvm> selectMethod(ksvm, "matrix")Method Definition:function (x, ...){ .local <- function (x, y = NULL, scaled = TRUE, type = NULL, kernel = "rbfdot", kpar = "automatic", C = 1, nu = 0.2, < > , drop = FALSE][cind, ], as.factor(lev(ret)[yd[cind]]), < >
  • 53. ksvm> trace(ksvm, signature = “matrix”, edit = TRUE) browser browser() if (is.null(class.weights)) cret <- ksvm(x[c(indexes[[i]], indexes[[j]]), , drop = FALSE][cind, ], yd[cind], type = type(ret), kernel = kernel, kpar = NULL, C = C, nu = nu, tol = tol, scaled = FALSE, cross = 0, fit = FALSE, cache = cache, prob.model = FALSE) else cret <- ksvm(x[c(indexes[[i]], indexes[[j]]), , drop = FALSE][cind, ], as.factor(lev(ret)[yd[cind]]),
  • 54. ksvm> ksvm(Species ~ .,+ iris,+ class.weights = c(versicolor = 1, setosa = 10),+ prob.model = TRUE)Using automatic sigma estimation (sigest) for RBF or laplace kernelCalled from: .local(x, ...)Browse[1]> yd [1] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 [22] -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 [43] -1 -1 -1 -1 -1 -1 -1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 [64] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [85] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 lev(ret)[yd[cind]] yd (-1, 1) yd lev(ret)
  • 55. > 1:5[1] 1 2 3 4 5> .Last.value[1] 1 2 3 4 5 ESS TAB .Last.value
  • 56. f <- function(x) { y <- x g(y)}g <- function(x) { x <- x^2 browser()}> f(10)Called from: g(y) # x yBrowse[1]> x # x 2[1] 100Browse[1]> y # yError: object y not found
  • 57. Browse[1]> evalq(y, parent.frame()) # y[1] 10Browse[1]> evalq(ls(), parent.frame()) #[1] "x" "y"Browse[1]> eval.parent(quote(y)) # part1[1] 10Browse[1]> get("y", parent.frame()) # part2[1] 10Browse[1]> ls(envir = parent.frame()) #[1] "x" "y" evalq(expr, parent.frame())
  • 58. apply browserf <- function() { a <- lapply(1:10000, function(x) { browser() # 1 browser() print(x) })}
  • 59. apply browser> debug(f)> f()debugging in: f()debug: { a <- lapply(1:10000, function(x) { browser() print(x) })}Browse[2]> ndebug: a <- lapply(1:10000, function(x) { browser() print(x)})Browse[2]>Called from: FUN(1:10000[[1L]], ...)Browse[2]> c[1] 1Called from: FUN(1:10000[[2L]], ...) # browser()Browse[2]>
  • 60. apply browserBrowse[2]> evalq(FUN <- edit(FUN), parent.frame())function(x) { browser() # print(x) }Browse[2]> evalq(fix(FUN), parent.frame())Browse[2]> FUN <<- evalq(edit(FUN), parent.frame())
  • 61. R
  • 62. • HI!! http://kohske.wordpress.com/?s=r+debug• An Introduction to the Interactive Debugging Tools in R http:// www.biostat.jhsph.edu/~rpeng/docs/R-debug-tools.pdf• Debugging in R http://www.stats.uwo.ca/faculty/murdoch/ software/debuggingR/• Summer Computing 2011 Week 3:R debugging + misc http:// www.stat.cmu.edu/~hseltman/711/Week3.pdf• Rdebug http://www.math.ncu.edu.tw/~chenwc/R_note/ reference/debug/Rdebug.pdf