Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
versions of R prior to 2.7.0, or without support for cairo. From R
2.7.0 'png()' by default uses the Quartz device on macOS, and that too
works in batch mode.
Earlier versions of the 'png()' device used the X11 driver, which is
a problem in batch mode or for remote operation. If you have
Ghostscript you can use 'bitmap()', which produces a PostScript or PDF
file then converts it to any bitmap format supported by Ghostscript. On
some installations this produces ugly output, on others it is perfectly
satisfactory. Many systems now come with Xvfb from X.Org
(https://www.x.org/) (possibly as an optional install), which is an X11
server that does not require a screen; and there is the *GDD*
(https://CRAN.R-project.org/package=GDD) package from CRAN, which
produces PNG, JPEG and GIF bitmaps without X11.
7.20 How can I get command line editing to work?
================================================
The Unix-like command-line interface to R can only provide the inbuilt
command line editor which allows recall, editing and re-submission of
prior commands provided that the GNU readline library is available at
the time R is configured for compilation. Note that the 'development'
version of readline including the appropriate headers is needed: users
of Linux binary distributions will need to install packages such as
'libreadline-dev' (Debian) or 'readline-devel' (Red Hat).
7.21 How can I turn a string into a variable?
=============================================
If you have
varname <- c("a", "b", "d")
you can do
get(varname[1]) + 2
for
a + 2
or
assign(varname[1], 2 + 2)
for
a <- 2 + 2
or
eval(substitute(lm(y ~ x + variable),
list(variable = as.name(varname[1]))))
for
lm(y ~ x + a)
At least in the first two cases it is often easier to just use a
list, and then you can easily index it by name
vars <- list(a = 1:10, b = rnorm(100), d = LETTERS)
vars[["a"]]
without any of this messing about.
7.22 Why do lattice/trellis graphics not work?
==============================================
The most likely reason is that you forgot to tell R to display the
graph. Lattice functions such as 'xyplot()' create a graph object, but
do not display it (the same is true of *ggplot2*
(https://CRAN.R-project.org/package=ggplot2) graphics, and Trellis
graphics in S-PLUS). The 'print()' method for the graph object produces
the actual display. When you use these functions interactively at the
command line, the result is automatically printed, but in 'source()' or
inside your own functions you will need an explicit 'print()' statement.
7.23 How can I sort the rows of a data frame?
=============================================
To sort the rows within a data frame, with respect to the values in one
or more of the columns, simply use 'order()' (e.g., 'DF[order(DF$a,
DF[["b"]]), ]' to sort the data frame 'DF' on columns named 'a' and
'b').
7.24 Why does the help.start() search engine not work?
======================================================
The browser-based search engine in 'help.start()' utilizes a Java
applet. In order for this to function properly, a compatible version of
Java must installed on your system and linked to your browser, and both
Java _and_ JavaScript need to be enabled in your browser.
There have been a number of compatibility issues with versions of
Java and of browsers. For further details please consult section
"Enabling search in HTML help" in 'R Installation and Administration'.
This manual is included in the R distribution, *note What documentation
exists for R?::, and its HTML version is linked from the HTML search
page.
7.25 Why did my .Rprofile stop working when I updated R?
========================================================
Did you read the 'NEWS' file? For functions that are not in the *base*
package you need to specify the correct package namespace, since the
code will be run _before_ the packages are loaded. E.g.,
ps.options(horizontal = FALSE)
help.start()
needs to be
grDevices::ps.options(horizontal = FALSE)
utils::help.start()
('graphics::ps.options(horizontal = FALSE)' in R 1.9.x).
7.26 Where have all the methods gone?
=====================================
Many functions, particularly S3 methods, are now hidden in namespaces.
This has the advantage that they cannot be called inadvertently with
arguments of the wrong class, but it makes them harder to view.
To see the code for an S3 method (e.g., '[.terms') use
getS3method("[", "terms")
To see the code for an unexported function 'foo()' in the namespace of
package '"bar"' use 'bar:::foo'. Don't use these constructions to call
unexported functions in your own code--they are probably unexported for
a reason and may change without warning.
7.27 How can I create rotated axis labels?
==========================================
To rotate axis labels (using base graphics), you need to use 'text()',
rather than 'mtext()', as the latter does not support 'par("srt")'.
## Increase bottom margin to make room for rotated labels
par(mar = c(7, 4, 4, 2) + 0.1)
## Create plot with no x axis and no x axis label
plot(1 : 8, xaxt = "n", xlab = "")
## Set up x axis with tick marks alone
axis(1, labels = FALSE)
## Create some text labels
labels <- paste("Label", 1:8, sep = " ")
## Plot x axis labels at default tick marks
text(1:8, par("usr")[3] - 0.25, srt = 45, adj = 1,
labels = labels, xpd = TRUE)
## Plot x axis label at line 6 (of 7)
mtext(1, text = "X Axis Label", line = 6)
When plotting the x axis labels, we use 'srt = 45' for text rotation
angle, 'adj = 1' to place the right end of text at the tick marks, and
'xpd = TRUE' to allow for text outside the plot region. You can adjust
the value of the '0.25' offset as required to move the axis labels up or
down relative to the x axis. See '?par' for more information.
Also see Figure 1 and associated code in Paul Murrell (2003),
"Integrating grid Graphics Output with Base Graphics Output", _R News_,
*3/2*, 7-12.
7.28 Why is read.table() so inefficient?
========================================
By default, 'read.table()' needs to read in everything as character
data, and then try to figure out which variables to convert to numerics
or factors. For a large data set, this takes considerable amounts of
time and memory. Performance can substantially be improved by using the
'colClasses' argument to specify the classes to be assumed for the
columns of the table.
7.29 What is the difference between package and library?
========================================================
A "package" is a standardized collection of material extending R, e.g.
providing code, data, or documentation. A "library" is a place
(directory) where R knows to find packages it can use (i.e., which were
"installed"). R is told to use a package (to "load" it and add it to
the search path) via calls to the function 'library'. I.e., 'library()'
is employed to load a package from libraries containing packages.
*Note R Add-On Packages::, for more details. See also Uwe Ligges
(2003), "R Help Desk: Package Management", _R News_, *3/3*, 37-39.
7.30 I installed a package but the functions are not there
==========================================================
To actually _use_ the package, it needs to be _loaded_ using
'library()'.
See *note R Add-On Packages:: and *note What is the difference
between package and library?:: for more information.
7.31 Why doesn't R think these numbers are equal?
=================================================
The only numbers that can be represented exactly in R's numeric type are
integers and fractions whose denominator is a power of 2. All other
numbers are internally rounded to (typically) 53 binary digits accuracy.
As a result, two floating point numbers will not reliably be equal
unless they have been computed by the same algorithm, and not always
even then. For example
R> a <- sqrt(2)
R> a * a == 2
[1] FALSE
R> a * a - 2
[1] 4.440892e-16
R> print(a * a, digits = 18)
[1] 2.00000000000000044
The function 'all.equal()' compares two objects using a numeric
tolerance of '.Machine$double.eps ^ 0.5'. If you want much greater
accuracy than this you will need to consider error propagation
carefully.
A discussion with many easily followed examples is in Appendix G
"Computational Precision and Floating Point Arithmetic", pages 753-771
of _Statistical Analysis and Data Display: An Intermediate Course with
Examples in R_, Richard M. Heiberger and Burt Holland (Springer 2015,
second edition). This appendix is a free download from
<https://link.springer.com/content/pdf/bbm%3A978-1-4939-2122-5%2F1.pdf>.
For more information, see e.g. David Goldberg (1991), "What Every
Computer Scientist Should Know About Floating-Point Arithmetic", _ACM
Computing Surveys_, *23/1*, 5-48, also available via
<https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html>.
Here is another example, this time using addition:
R> .3 + .6 == .9
[1] FALSE
R> .3 + .6 - .9
[1] -1.110223e-16
R> print(matrix(c(.3, .6, .9, .3 + .6)), digits = 18)
[,1]
[1,] 0.299999999999999989
[2,] 0.599999999999999978
[3,] 0.900000000000000022
[4,] 0.899999999999999911
7.32 How can I capture or ignore errors in a long simulation?
=============================================================
Use 'try()', which returns an object of class '"try-error"' instead of
an error, or preferably 'tryCatch()', where the return value can be
configured more flexibly. For example
beta[i,] <- tryCatch(coef(lm(formula, data)),
error = function(e) rep(NaN, 4))
would return the coefficients if the 'lm()' call succeeded and would
return 'c(NaN, NaN, NaN, NaN)' if it failed (presumably there are
supposed to be 4 coefficients in this example).
7.33 Why are powers of negative numbers wrong?
==============================================
You are probably seeing something like
R> -2^2
[1] -4
and misunderstanding the precedence rules for expressions in R. Write
R> (-2)^2
[1] 4
to get the square of -2.
The precedence rules are documented in '?Syntax', and to see how R
interprets an expression you can look at the parse tree
R> as.list(quote(-2^2))
[[1]]
`-`
[[2]]
2^2
7.34 How can I save the result of each iteration in a loop into a separate file?
================================================================================
One way is to use 'paste()' (or 'sprintf()') to concatenate a stem
filename and the iteration number while 'file.path()' constructs the
path. For example, to save results into files 'result1.rda', ...,
'result100.rda' in the subdirectory 'Results' of the current working
directory, one can use
for(i in 1:100) {
## Calculations constructing "some_object" ...
fp <- file.path("Results", paste("result", i, ".rda", sep = ""))
save(list = "some_object", file = fp)
}
7.35 Why are p-values not displayed when using lmer()?
======================================================
Doug Bates has kindly provided an extensive response in a post to the
r-help list, which can be reviewed at
<https://stat.ethz.ch/pipermail/r-help/2006-May/094765.html>.
7.36 Why are there unwanted borders, lines or grid-like artifacts when viewing a plot saved to a PS or PDF file?
================================================================================================================
This can occur when using functions such as 'polygon()',
'filled.contour()', 'image()' or other functions which may call these
internally. In the case of 'polygon()', you may observe unwanted
borders between the polygons even when setting the 'border' argument to
'NA' or '"transparent"'.
The source of the problem is the PS/PDF viewer when the plot is
anti-aliased. The details for the solution will be different depending
upon the viewer used, the operating system and may change over time.
For some common viewers, consider the following:
Acrobat Reader (cross platform)
There are options in Preferences to enable/disable text smoothing,
image smoothing and line art smoothing. Disable line art
smoothing.
Preview (macOS)
There is an option in Preferences to enable/disable anti-aliasing
of text and line art. Disable this option.
GSview (cross platform)
There are settings for Text Alpha and Graphics Alpha. Change
Graphics Alpha from 4 bits to 1 bit to disable graphic
anti-aliasing.
gv (Unix-like X)
There is an option to enable/disable anti-aliasing. Disable this
option.
Evince (Linux/GNOME)
There is not an option to disable anti-aliasing in this viewer.
Okular (Linux/KDE)
There is not an option in the GUI to enable/disable anti-aliasing.
From a console command line, use:
$ kwriteconfig --file okularpartrc --group 'Dlg Performance' \
--key GraphicsAntialias Disabled
Then restart Okular. Change the final word to 'Enabled' to restore
the original setting.
7.37 Why does backslash behave strangely inside strings?
========================================================
This question most often comes up in relation to file names (see *note
How do file names work in Windows?::) but it also happens that people
complain that they cannot seem to put a single '\' character into a text
string unless it happens to be followed by certain other characters.
To understand this, you have to distinguish between character strings
and _representations_ of character strings. Mostly, the representation
in R is just the string with a single or double quote at either end, but
there are strings that cannot be represented that way, e.g., strings
that themselves contain the quote character. So
> str <- "This \"text\" is quoted"
> str
[1] "This \"text\" is quoted"
> cat(str, "\n")
This "text" is quoted
The _escape sequences_ '\"' and '\n' represent a double quote and the
newline character respectively. Printing text strings, using 'print()'
or by typing the name at the prompt will use the escape sequences too,
but the 'cat()' function will display the string as-is. Notice that
'"\n"' is a one-character string, not two; the backslash is not actually
in the string, it is just generated in the printed representation.
> nchar("\n")
[1] 1
> substring("\n", 1, 1)
[1] "\n"
So how do you put a backslash in a string? For this, you have to
escape the escape character. I.e., you have to double the backslash.
as in
> cat("\\n", "\n")
\n
Some functions, particularly those involving regular expression
matching, themselves use metacharacters, which may need to be escaped by
the backslash mechanism. In those cases you may need a _quadruple_
backslash to represent a single literal one.
In versions of R up to 2.4.1 an unknown escape sequence like '\p' was
quietly interpreted as just 'p'. Current versions of R emit a warning.
7.38 How can I put error bars or confidence bands on my plot?
=============================================================
Some functions will display a particular kind of plot with error bars,
such as the 'bar.err()' function in the *agricolae*
(https://CRAN.R-project.org/package=agricolae) package, the 'plotCI()'
function in the *gplots* (https://CRAN.R-project.org/package=gplots)
package, the 'plotCI()' and 'brkdn.plot()' functions in the *plotrix*
(https://CRAN.R-project.org/package=plotrix) package and the
'error.bars()', 'error.crosses()' and 'error.bars.by()' functions in the
*psych* (https://CRAN.R-project.org/package=psych) package. Within
these types of functions, some will accept the measures of dispersion
(e.g., 'plotCI'), some will calculate the dispersion measures from the
raw values ('bar.err', 'brkdn.plot'), and some will do both
('error.bars'). Still other functions will just display error bars,
like the dispersion function in the *plotrix*
(https://CRAN.R-project.org/package=plotrix) package. Most of the above
functions use the 'arrows()' function in the base *graphics* package to
draw the error bars.
The above functions all use the base graphics system. The grid and
lattice graphics systems also have specific functions for displaying
error bars, e.g., the 'grid.arrow()' function in the *grid* package, and
the 'geom_errorbar()', 'geom_errorbarh()', 'geom_pointrange()',
'geom_linerange()', 'geom_crossbar()' and 'geom_ribbon()' functions in
the *ggplot2* (https://CRAN.R-project.org/package=ggplot2) package. In
the lattice system, error bars can be displayed with 'Dotplot()' or
'xYplot()' in the *Hmisc* (https://CRAN.R-project.org/package=Hmisc)
package and 'segplot()' in the *latticeExtra*
(https://CRAN.R-project.org/package=latticeExtra) package.
7.39 How do I create a plot with two y-axes?
============================================
Creating a graph with two y-axes, i.e., with two sorts of data that are
scaled to the same vertical size and showing separate vertical axes on
the left and right sides of the plot that reflect the original scales of
the data, is possible in R but is not recommended. The basic approach
for constructing such graphs is to use 'par(new=TRUE)' (see '?par');
functions 'twoord.plot()' (in the *plotrix*
(https://CRAN.R-project.org/package=plotrix) package) and
'doubleYScale()' (in the *latticeExtra*
(https://CRAN.R-project.org/package=latticeExtra) package) automate the
process somewhat.
7.40 How do I access the source code for a function?
====================================================
In most cases, typing the name of the function will print its source
code. However, code is sometimes hidden in a namespace, or compiled.
For a complete overview on how to access source code, see Uwe Ligges
(2006), "Help Desk: Accessing the sources", _R News_, *6/4*, 43-45
(<https://CRAN.R-project.org/doc/Rnews/Rnews_2006-4.pdf>).
7.41 Why does summary() report strange results for the R^2 estimate when I fit a linear model with no intercept?
================================================================================================================
As described in '?summary.lm', when the intercept is zero (e.g., from 'y
~ x - 1' or 'y ~ x + 0'), 'summary.lm()' uses the formula R^2 = 1 -
Sum(R[i]^2) / Sum((y[i])^2) which is different from the usual R^2 = 1 -
Sum(R[i]^2) / Sum((y[i] - mean(y))^2). There are several reasons for
this:
* Otherwise the R^2 could be negative (because the model with zero
intercept can fit _worse_ than the constant-mean model it is
implicitly compared to).
* If you set the slope to zero in the model with a line through the
origin you get fitted values y*=0
* The model with constant, non-zero mean is not nested in the model
with a line through the origin.
All these come down to saying that if you know _a priori_ that E[Y]=0
when x=0 then the 'null' model that you should compare to the fitted
line, the model where x doesn't explain any of the variance, is the
model where E[Y]=0 everywhere. (If you don't know a priori that E[Y]=0
when x=0, then you probably shouldn't be fitting a line through the
origin.)
7.42 Why is R apparently not releasing memory?
==============================================
This question is often asked in different flavors along the lines of "I
have removed objects in R and run 'gc()' and yet 'ps'/'top' still shows
the R process using a lot of memory", often on Linux machines.
This is an artifact of the way the operating system (OS) allocates
memory. In general it is common that the OS is not capable of releasing
all unused memory. In extreme cases it is possible that even if R frees
almost all its memory, the OS can not release any of it due to its
design and thus tools such as 'ps' or 'top' will report substantial
amount of resident RAM used by the R process even though R has released
all that memory. In general such tools do _not_ report the actual
memory usage of the process but rather what the OS is reserving for that
process.
The short answer is that this is a limitation of the memory allocator
in the operating system and there is nothing R can do about it. That
space is simply kept by the OS in the hope that R will ask for it later.
The following paragraph gives more in-depth answer with technical
details on how this happens.
Most systems use two separate ways to allocate memory. For
allocation of large chunks they will use 'mmap' to map memory into the
process address space. Such chunks can be released immediately when
they are completely free, because they can reside anywhere in the
virtual memory. However, this is a relatively expensive operation and
many OSes have a limit on the number of such allocated chunks, so this
is only used for allocating large memory regions. For smaller
allocations the system can expand the data segment of the process
(historically using the 'brk' system call), but this whole area is
always contiguous. The OS can only move the end of this space, it
cannot create any "holes". Since this operation is fairly cheap, it is
used for allocations of small pieces of memory. However, the
side-effect is that even if there is just one byte that is in use at the
end of the data segment, the OS cannot release any memory at all,
because it cannot change the address of that byte. This is actually
more common than it may seem, because allocating a lot of intermediate
objects, then allocating a result object and removing all intermediate
objects is a very common practice. Since the result is allocated at the
end it will prevent the OS from releasing any memory used by the
intermediate objects. In practice, this is not necessarily a problem,
because modern operating systems can page out unused portions of the
virtual memory so it does not necessarily reduce the amount of real
memory available for other applications. Typically, small objects such
as strings or pairlists will be affected by this behavior, whereas large
objects such as long vectors will be allocated using 'mmap' and thus not
affected. On Linux (and possibly other Unix-like systems) it is
possible to use the 'mallinfo' system call (also see the mallinfo
(https://rforge.net/mallinfo) package) to query the allocator about the
layout of the allocations, including the actually used memory as well as
unused memory that cannot be released.
7.43 How can I enable secure https downloads in R?
==================================================
When R transfers files over HTTP (e.g., using the 'install.packages()'
or 'download.file()' function), a download method is chosen based on the
'download.file.method' option. There are several methods available and
the default behavior if no option is explicitly specified is to use R's
internal HTTP implementation. In most circumstances this internal
method will not support HTTPS URLs so you will need to override the
default: this is done automatically for such URLs as from R 3.2.2.
R versions 3.2.0 and greater include two download methods
('"libcurl"' and '"wininet"') that both support HTTPS connections: we
recommend that you use these methods. The requisite code to add to
'.Rprofile' or 'Rprofile.site' is:
options(download.file.method = "wininet", url.method = "wininet")
(Windows)
options(download.file.method = "libcurl", url.method = "libcurl")
(Linux and macOS)
(Method '"wininet"' is the default on Windows as from R 3.2.2.)
Note that the '"libcurl"' method may or may not have been compiled
in. In the case that it was not, i.e. 'capabilities("libcurl") ==
FALSE', we recommend method '"wget"' on Linux and '"curl"' on macOS. It
is possible that system versions of '"libcurl"', 'wget' or 'curl' may
have been compiled without HTTPS support, but this is unlikely. As from
R 3.3.0 '"libcurl"' with HTTPS support is required except on Windows.
7.44 How can I get CRAN package binaries for outdated versions of R?
====================================================================
Since March 2016, Windows and macOS binaries of CRAN packages for old
versions of R (released more than 5 years ago) are made available from a
central CRAN archive server instead of the CRAN mirrors. To get these,
one should set the CRAN "mirror" element of the 'repos' option
accordingly, by something like
local({r <- getOption("repos")
r["CRAN"] <- "http://CRAN-archive.R-project.org"
options(repos = r)
})
(see '?options' for more information).
8 R Programming
***************
8.1 How should I write summary methods?
=======================================
Suppose you want to provide a summary method for class '"foo"'. Then
'summary.foo()' should not print anything, but return an object of class
'"summary.foo"', _and_ you should write a method 'print.summary.foo()'
which nicely prints the summary information and invisibly returns its
object. This approach is preferred over having 'summary.foo()' print
summary information and return something useful, as sometimes you need
to grab something computed by 'summary()' inside a function or similar.
In such cases you don't want anything printed.
8.2 How can I debug dynamically loaded code?
============================================
Roughly speaking, you need to start R inside the debugger, load the
code, send an interrupt, and then set the required breakpoints.
See section "Finding entry points in dynamically loaded code" in
'Writing R Extensions'. This manual is included in the R distribution,
*note What documentation exists for R?::.
8.3 How can I inspect R objects when debugging?
===============================================
The most convenient way is to call 'R_PV' from the symbolic debugger.
See section "Inspecting R objects when debugging" in 'Writing R
Extensions'.
8.4 How can I change compilation flags?
=======================================
Suppose you have C code file for dynloading into R, but you want to use
'R CMD SHLIB' with compilation flags other than the default ones (which
were determined when R was built).
Starting with R 2.1.0, users can provide personal Makevars
configuration files in '$HOME/.R' to override the default flags. See
section "Add-on packages" in 'R Installation and Administration'.
For earlier versions of R, you could change the file
'R_HOME/etc/Makeconf' to reflect your preferences, or (at least for
systems using GNU Make) override them by the environment variable
'MAKEFLAGS'. See section "Creating shared objects" in 'Writing R
Extensions'.
8.5 How can I debug S4 methods?
===============================
Use the 'trace()' function with argument 'signature=' to add calls to
the browser or any other code to the method that will be dispatched for
the corresponding signature. See '?trace' for details.
9 R Bugs
********
9.1 What is a bug?
==================
If R executes an illegal instruction, or dies with an operating system
error message that indicates a problem in the program (as opposed to
something like "disk full"), then it is certainly a bug. If you call
'.C()', '.Fortran()', '.External()' or '.Call()' (or '.Internal()')
yourself (or in a function you wrote), you can always crash R by using
wrong argument types (modes). This is not a bug.
Taking forever to complete a command can be a bug, but you must make
certain that it was really R's fault. Some commands simply take a long
time. If the input was such that you _know_ it should have been
processed quickly, report a bug. If you don't know whether the command
should take a long time, find out by looking in the manual or by asking
for assistance.
If a command you are familiar with causes an R error message in a
case where its usual definition ought to be reasonable, it is probably a
bug. If a command does the wrong thing, that is a bug. But be sure you
know for certain what it ought to have done. If you aren't familiar
with the command, or don't know for certain how the command is supposed
to work, then it might actually be working right. For example, people
sometimes think there is a bug in R's mathematics because they don't
understand how finite-precision arithmetic works. Rather than jumping
to conclusions, show the problem to someone who knows for certain.
Unexpected results of comparison of decimal numbers, for example '0.28 *
100 != 28' or '0.1 + 0.2 != 0.3', are not a bug. *Note Why doesn't R
think these numbers are equal?::, for more details.
Finally, a command's intended definition may not be best for
statistical analysis. This is a very important sort of problem, but it
is also a matter of judgment. Also, it is easy to come to such a
conclusion out of ignorance of some of the existing features. It is
probably best not to complain about such a problem until you have
checked the documentation in the usual ways, feel confident that you
understand it, and know for certain that what you want is not available.
If you are not sure what the command is supposed to do after a careful
reading of the manual this indicates a bug in the manual. The manual's
job is to make everything clear. It is just as important to report
documentation bugs as program bugs. However, we know that the
introductory documentation is seriously inadequate, so you don't need to
report this.
If the online argument list of a function disagrees with the manual,
one of them must be wrong, so report the bug.
9.2 How to report a bug
=======================
When you decide that there is a bug, it is important to report it and to
report it in a way which is useful. What is most useful is an exact
description of what commands you type, starting with the shell command
to run R, until the problem happens. Always include the version of R,
machine, and operating system that you are using; type 'version' in R to
print this.
The most important principle in reporting a bug is to report _facts_,
not hypotheses or categorizations. It is always easier to report the
facts, but people seem to prefer to strain to posit explanations and
report them instead. If the explanations are based on guesses about how
R is implemented, they will be useless; others will have to try to
figure out what the facts must have been to lead to such speculations.
Sometimes this is impossible. But in any case, it is unnecessary work
for the ones trying to fix the problem.
For example, suppose that on a data set which you know to be quite
large the command
R> data.frame(x, y, z, monday, tuesday)
never returns. Do not report that 'data.frame()' fails for large data
sets. Perhaps it fails when a variable name is a day of the week. If
this is so then when others got your report they would try out the
'data.frame()' command on a large data set, probably with no day of the
week variable name, and not see any problem. There is no way in the
world that others could guess that they should try a day of the week
variable name.
Or perhaps the command fails because the last command you used was a
method for '"["()' that had a bug causing R's internal data structures
to be corrupted and making the 'data.frame()' command fail from then on.
This is why others need to know what other commands you have typed (or
read from your startup file).
It is very useful to try and find simple examples that produce
apparently the same bug, and somewhat useful to find simple examples
that might be expected to produce the bug but actually do not. If you
want to debug the problem and find exactly what caused it, that is
wonderful. You should still report the facts as well as any
explanations or solutions. Please include an example that reproduces
(e.g., <https://en.wikipedia.org/wiki/Reproducibility>) the problem,
preferably the simplest one you have found.
Invoking R with the '--vanilla' option may help in isolating a bug.
This ensures that the site profile and saved data files are not read.
Before you actually submit a bug report, you should check whether the
bug has already been reported and/or fixed. First, try the "Show open
bugs new-to-old" or the search facility on
<https://bugs.R-project.org/>. Second, consult
<https://svn.R-project.org/R/trunk/doc/NEWS.Rd>, which records changes
that will appear in the _next_ release of R, including bug fixes that do
not appear on the Bug Tracker. Third, if possible try the current
r-patched or r-devel version of R. If a bug has already been reported or
fixed, please do not submit further bug reports on it. Finally, check
carefully whether the bug is with R, or a contributed package. Bug
reports on contributed packages should be sent first to the package
maintainer, and only submitted to the R-bugs repository by package
maintainers, mentioning the package in the subject line.
A bug report can be generated using the function 'bug.report()'. For
reports on R this will open the Web page at
<https://bugs.R-project.org/>: for a contributed package it will open
the package's bug tracker Web page or help you compose an email to the
maintainer.
There is a section of the bug repository for suggestions for
enhancements for R labelled 'wishlist'. Suggestions can be submitted in
the same ways as bugs, but please ensure that the subject line makes
clear that this is for the wishlist and not a bug report, for example by
starting with 'Wishlist:'.
Comments on and suggestions for the Windows port of R should be sent
to <R-windows@R-project.org>.
Corrections to and comments on message translations should be sent to
the last translator (listed at the top of the appropriate '.po' file) or
to the translation team as listed at
<https://developer.R-project.org/TranslationTeams.html>.
10 Acknowledgments
******************
Of course, many many thanks to Robert and Ross for the R system, and to
the package writers and porters for adding to it.
Special thanks go to Doug Bates, Peter Dalgaard, Paul Gilbert,
Stefano Iacus, Fritz Leisch, Jim Lindsey, Thomas Lumley, Martin
Maechler, Brian D. Ripley, Anthony Rossini, and Andreas Weingessel for
their comments which helped me improve this FAQ.
More to come soon ...