16

Wikipedia states that R is "multi-paradigm: array, object-oriented, imperative, functional, procedural, reflective and also dynamic. But what about its type safety? Please explain the different aspects/kinds of possible type safety in R with examples, e.g.

  • Do we get unchecked run-time type errors?
  • Are operations or function calls which attempt to disregard data types rejected?
  • Is there a well-defined error or exceptional behavior (as opposed to an undefined behavior) as soon as a type-matching failure happens?
  • Are data objects fixed and invariable typed?
  • Can the type system be evaded?
  • Does it have a complex, fine-grained type system with compound types and does each each object have a well-defined type that will prohibit illegal values and operations?
  • Support for implicit type conversion (this has been shown by some answers already, thank you).

These questions are derived from Wikipedia (http://en.wikipedia.org/wiki/Strong_typing) as strong/weakly typed is too fuzzy to ask for (thanks delnan for clarifications here).

Navin
  • 3,681
  • 3
  • 28
  • 52
Peter Kofler
  • 9,252
  • 8
  • 51
  • 79
  • 2
    http://en.wikipedia.org/wiki/R_(programming_language) – DuckMaestro Jan 12 '13 at 23:10
  • Thanks for the Wikipedia link. I have quoted that link in my question. Unfortunately it does not say anything about typing. – Peter Kofler Jan 12 '13 at 23:12
  • 1
    On the Wikipedia page it says in the right-hand side box: `Typing discipline: Dynamic`. Granted, it does not answer the question about strong or weak typing. – Heinzi Jan 12 '13 at 23:17
  • 2
    I've posted this on several answers too, but I believe this can't be over-stressed: ["weakly typed" is meaningless](http://stackoverflow.com/a/9929697/395760). –  Jan 12 '13 at 23:25
  • Delnan, I understand and wikipedia about weakly typing says the same, but I have to start somewhere, right? – Peter Kofler Jan 12 '13 at 23:26
  • 2
    If you down vote please comment why so I can improve the question. Thanks – Peter Kofler Jan 12 '13 at 23:29
  • 1
    You could instead ask about specific properties of the language (for example, some commonly considered indicative of "weak typing"). Someone telling you "it's weakly typed" doesn't tell you *anything*, so it's a pointless question to ask (actually worse than pointless IMHO, as it keeps this nonsense categorization - and hence confusion - alive). –  Jan 12 '13 at 23:32
  • Still asking for discussion. I vote to close. – Matthew Lundberg Jan 13 '13 at 02:11
  • 1
    Sorry, this is not a discussion. Either the R language has one of these properties or not. I agree that the topic of weak/strong is fuzzy and invites for discussion, but these concrete questions suggested by delnan are not. – Peter Kofler Jan 13 '13 at 12:47

5 Answers5

11
> a = TRUE
> a
[1] TRUE
> a[3] = 1
> a
[1]  1 NA  1
> a[5] = 3.14
> a
[1] 1.00   NA 1.00   NA 3.14
> a = "Ala"
> a
[1] "Ala"

You can see it's dynamic - I can assign anything to variable a and change it at will. It's also weakly typed performing implicit conversions liberally - see how I can add ints and then floats to what was originally boolean "vector". You can usually treat booleans as is they were numbers and numbers as if they were strings.

zch
  • 14,931
  • 2
  • 41
  • 49
  • dynamic, I agree. But the mode of the vector changes when you assign something different in the defined hierarchy of logical -> number -> character. Java is strongly typed still you can add an int and a double (where the int gets converted/widened before). – Peter Kofler Jan 12 '13 at 23:19
  • 1
    Please don't use "weakly typed", as it can mean any of [a number of unrelated things](http://stackoverflow.com/a/9929697/395760). Rather state what you actually mean (e.g. "allows implicit conversions"). –  Jan 12 '13 at 23:23
  • Delnan, agree. Changed the question accordingly. – Peter Kofler Jan 12 '13 at 23:33
9

Implicit coercion by operators and functions is widespread in the language (and it has argument recycling). Warnings are issued most of the time for the recycling but not for coercion.

 a <- c(TRUE, FALSE)
     b <- c(-4:4)
 a < b
#[1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE
#Warning message:
#In a < b : longer object length is not a multiple of shorter object length

  c(a,b)
# [1]  1  0 -4 -3 -2 -1  0  1  2  3  4

 a + b
[1] -3 -3 -1 -1  1  1  3  3  5
# Standard Warning message re: multiple of shorter object length

> data.frame( a =a, b=b)
Error in data.frame(a = a, b = b) : 
  arguments imply differing number of rows: 2, 9

But if arguments to data.frame have lengths that are an even multiple of the vector with the longest lenght, they get recycled without warning:

> data.frame( a =c(TRUE,FALSE,TRUE), b=b)
      a  b
1  TRUE -4
2 FALSE -3
3  TRUE -2
4  TRUE -1
5 FALSE  0
6  TRUE  1
7  TRUE  2
8 FALSE  3
9  TRUE  4

Functions are made polymorphic via two class systems, one of which depends on the period character signalling the target class, the other of which depends on object class signatures. (And the function naming conventions are very erratic.)

I disagree with the premise of R as an object-oriented language. People with object-oriented experience that come to R expect a different behavior than it exhibits. It is a functional language that is dispatched with class systems that examine the objects given but it's not like a real object-oriented language.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
8

There's an excellent survey that answers your question with examples: Evaluating the Design of the R Language by F.Morandat et al.

A citation from abstract:

R is a dynamic language for statistical computing that combines lazy functional features and object-oriented programming"

Each of these key words "dynamic", "lazy" etc is provided with the examples in this paper (look at the chapter "3. The Three Faces of R").

The authors went through the following points:
– Semantics of Core R
– They built a corpus of R programs and evaluated them.
– Implementation Evaluation
– Language Evaluation

Max Li
  • 5,069
  • 3
  • 23
  • 35
4

R has essentially no type safety. You can do all sorts of things which end up with meaningless objects.

x <- lm(Sepal.Width ~ Sepal.Length, data=iris)
x
## Call:
## lm(formula = Sepal.Width ~ Sepal.Length, data = iris)
##
## Coefficients:
##  (Intercept)  Sepal.Length  
##      3.41895      -0.06188  

opar <- par(mfrow=c(2,2))

plot(x)                  # Note generated plot results.
par(opar)


class(x) <- "data.frame"
plot(x)
Error in data.matrix(x) : 
  (list) object cannot be coerced to type 'double'
x
## [1] coefficients  residuals     effects       rank          fitted.values assign        qr            df.residual  
## [9] xlevels       call          terms         model        
## <0 rows> (or 0-length row.names)
Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112
1

R is dynamically typed.

Example: x <- "hi" (typeof(x) = character); x <- 1 (typeof(x) = double)

Concerning type safety: x <- 1; y <- "a"

x + y : Error in x + y : non-numeric argument to binary operator

However paste(x,y) (which is a concatenation) works : "1 a"

  • 3
    Not sure the "interpreted -> weakly typed" argument is correct. http://en.wikipedia.org/wiki/Strong_typing says Ruby is strongly typed, still it is interpreted. – Peter Kofler Jan 12 '13 at 23:16
  • -1 What OP said, plus: Weak/strong typing is unrelated to static/dynamic typing (your example shows dynamic typing) for most definitions of weak/strong typing, and it's [ill-defined to boot](http://stackoverflow.com/a/9929697/395760). –  Jan 12 '13 at 23:20
  • Yes,the both of you are correct. I change that.. –  Jan 13 '13 at 12:37