Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type-safety of the R language [closed]

Tags:

r

typing

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).

like image 313
Peter Kofler Avatar asked Jan 12 '13 23:01

Peter Kofler


People also ask

What type of language is R?

R is an open source programming language and software environment for statistical computing and graphics. It is one of the primary languages used by data scientists and statisticians alike. It is supported by the R Foundation for Statistical Computing and a large community of open source developers.

What is type-safe code?

A type-safe language is one where the only operations that one can execute on data are the ones that are condoned by the data's type. That is, if your data is of type X and X doesn't support operation y , then the language will not allow you to to execute y(X) .

Is R weakly typed?

Since R language is weakly typed language, or dynamically typed similar to Perl, Python or Matlab/Octave, most of R users omit to place type checks in their functions if not rarely.

Is type-safe the same as strongly typed?

From wikipedia: Type safety: Type safety is synonymous with one of the many definitions of strong typing; but type safety and dynamic typing are mutually compatible.


2 Answers

> 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.

like image 130
zch Avatar answered Oct 13 '22 21:10

zch


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.

like image 37
IRTFM Avatar answered Oct 13 '22 23:10

IRTFM