Dear R users I have started to use gdb for debugging dynloaded C code as described in `Writing R Extensions'. Unfortunately I have run into several problems that I haven't been able to solve: - using `n': how comes that the same line of code appears up to three times (and inbetween the next line and maybe even the second next one?) - using `print' resp. `display': How comes the variable values are different from those shown by fprintf? How comes the variable values change when actually no value is assigned to them (and I use `n', not `s')? Let me give you a short piece of my code (the line number in brackets) (216) dyPixel = loc_mat[k][l] / 16; (217) dxPixel = loc_mat[k][l] - 16 * dyPixel; (218) ynb = k + (dyPixel - 8) / 8.; (219) xnb = l + (dxPixel - 8) / 8.; (220) fprintf(stderr, "%i %i %f %f\n", dyPixel, dxPixel, ynb, xnb); (221) linkLength = hypot(xnb-x, ynb-x); and the corresponding output of gdb, using `n' - note that a) the linenumbers show up in the sequence 216, 217, 218, 216, 217, 218, 219, 218, 219, 220, 221 and b) that the values of `xnb' and `ynb' in the end do not correspond with those shown by fprintf in the second last statement: Breakpoint 7, edgelGrouping ( curv_vec=0x44262987 "D", 'ΓΏ' <repeats 199 times>..., curv_mat=0x442620cc, or_mat=0x442c90a4, loc_mat=0x4433007c, xdim=752, ydim=558, hashTable=0xbff7f044, hashInd=0xbfffdf84) at glazer.c:216 216 dyPixel = loc_mat[k][l] / 16; 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 1143349452 3: dyPixel = 478 (gdb) n 217 dxPixel = loc_mat[k][l] - 16 * dyPixel; 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 1144553335 3: dyPixel = 1144193148 (gdb) n 218 ynb = k + (dyPixel - 8) / 8.; 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 136 3: dyPixel = 1144193148 (gdb) n 216 dyPixel = loc_mat[k][l] / 16; 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 136 3: dyPixel = 1144193148 (gdb) n 217 dxPixel = loc_mat[k][l] - 16 * dyPixel; 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 136 3: dyPixel = 8 (gdb) n 218 ynb = k + (dyPixel - 8) / 8.; 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 8 3: dyPixel = 8 (gdb) n 219 xnb = l + (dxPixel - 8) / 8.; 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 8 3: dyPixel = 8 (gdb) n 218 ynb = k + (dyPixel - 8) / 8.; 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 8 3: dyPixel = 8 (gdb) n 219 xnb = l + (dxPixel - 8) / 8.; 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 8 3: dyPixel = 8 (gdb) n 220 fprintf(stderr, "%i %i %f %f\n", dyPixel, dxPixel, ynb, xnb); 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 8 3: dyPixel = 8 (gdb) n 8 8 477.000000 583.000000 221 linkLength = hypot(xnb-x, ynb-x); 6: xnb = 9.1245819032751532e-313 5: ynb = 0 4: dxPixel = 0 3: dyPixel = 1075452551 (gdb) I would be grateful for any hints. Peter ____________________________________________________________ Peter Holzer phone: + 41 1 632 46 34 Seminar fuer Statistik, LEO C14 fax: + 41 1 632 12 28 (Leonhardstr. 27) <holzer at stat.math.ethz.ch> ETH (Federal Inst. Technology) 8092 Zurich http://www.stat.math.ethz.ch/~holzer/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 2 Oct 2000, Peter Holzer wrote:> Dear R users > > I have started to use gdb for debugging dynloaded C code as described in > `Writing R Extensions'. Unfortunately I have run into several problems that > I haven't been able to solve: > > - using `n': how comes that the same line of code appears up to three times > (and inbetween the next line and maybe even the second next one?) > > - using `print' resp. `display': How comes the variable values are > different from those shown by fprintf? How comes the variable values > change when actually no value is assigned to them (and I use `n', not > `s')?I think both problems are likely due to compiler optimisation. The GNU tools are unusual in letting you debug optimised code: the Sun compilers, for example, won't generate debugging information if you optimise, for precisely this reason. Optimisation typically results in the machine instructions for different lines of C code being interleaved. Even worse, loops may be unrolled, statements moved in or out of loops, and other complicated things done to your code. While debugging optimised code can still be useful, it is often easier to recompile the code without optimisation (as long as the bug isn't caused by optimisation) and debug that way. I'm not sure of the best way to recompile without optimisation. You could edit R_HOME/etc/Makeconf, or you could cut-and-paste the compiler invocations that R COMPILE produces and then edit them by hand. -thomas Thomas Lumley Assistant Professor, Biostatistics University of Washington, Seattle -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Thanks to Robert Gentleman, Thomas Lumley and Kurt Hornik for their replies. They all pointed out that the problem comes from using optimisation during compiling, which is usually the default. The problem can easily be solved by adding the following code to the file Makevars: %.o: %.c $(CC) -g -Wall $(PKG_CFLAGS) $(ALL_CPPFLAGS) -o $@ -c $< A little bit more fancy is: ifneq (,$(findstring o, $(opt))) USE_CFLAGS = -g -Wall $(PKG_CFLAGS) else USE_CFLAGS = $(ALL_CFLAGS) endif %.o: %.c $(CC) $(USE_CFLAGS) $(ALL_CPPFLAGS) -o $@ -c $< and then using make opt=o if I don't want any optimisation and otherwise just normal make (so I don't need to remember when I have finished debugging that I should reset the flags.) Peter On Mon, 2 Oct 2000, Peter Holzer wrote:> Dear R users > > I have started to use gdb for debugging dynloaded C code as described in > `Writing R Extensions'. Unfortunately I have run into several problems that > I haven't been able to solve: > > - using `n': how comes that the same line of code appears up to three times > (and inbetween the next line and maybe even the second next one?) > > - using `print' resp. `display': How comes the variable values are > different from those shown by fprintf? How comes the variable values > change when actually no value is assigned to them (and I use `n', not > `s')?-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi! I have a variable with 5 categories and I want to create dummy variables for each category. Is there any quick way to do it in R? Thanks, Agus ------------------------ Agus Salim PhD student Dept. of Statistics University College Cork, Ireland Tel: 353-21-4902197 ------------------------ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Tue, 3 Oct 2000, Agus Salim wrote:> I have a variable with 5 categories and I want > to create dummy variables for each category. > > Is there any quick way to do it in R?Use row indexing or model.matrix. ff <- factor(sample(letters[1:5], 25, replace=TRUE)) diag(nlevels(ff))[ff,] or model.matrix(~ ff - 1) -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._