1

I'm quite familiar with C# .NET, and I'd like to call functions from .NET's System.Text library, such as .PadLeft(), string.Format, etc.

Does anyone know is this is possible?

Contango
  • 76,540
  • 58
  • 260
  • 305

3 Answers3

4

At the moment, as others have mentioned, it isn't easy to call .NET code from R. Using the rcom package may be viable, but the intersection of R programmers and C# programmers is pretty small, so few people have tried it.

There are some options if you are willing to tweak your technology stack.

  • MATLAB has excellent .NET integration and can do most things that R can do.

  • R has pretty good Java integration via the rJava package.

If you are happy with a pure-R solution, then the stringr package makes string handling pretty painless. stringr::str_pad is the same as C#'s string.PadLeft method. sprintf, format, formatC, and prettyNum from base R provide a variety of ways of formatting numbers. The scales package also has several formatting functions.

Community
  • 1
  • 1
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
4

I know this post has been around for some time, but since searches for answers on StackOverflow are an ongoing affair I thought I'd put another option.

The package rClr is available from https://r2clr.codeplex.com/ , soon to migrate to https://rclr.codeplex.com/. It is possible to call arbitrary .NET code, including string operations:

library(rClr)
clrCallStatic('System.String', 'Format', 'Hello, the number is {0}', 42L)
## [1] "Hello, the number is 42"

However, since strings, and arrays thereof, are on purpose converted transparently to R character vectors, instance methods such as PadLeft are not easily available. A helper class with static methods would do the trick. I'll consider adding facilities for .NET string manipulations in the package if there is interest.

j-m
  • 1,473
  • 1
  • 13
  • 17
1

R and .NET are not related in any way. That means that is is not easily possible.

You should simply learn the correct methods to use in R.

Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • Of course this is possible, but given that I am very familiar with C#, and .NET has a more comprehensive library for dealing with strings, why not? – Contango Feb 04 '13 at 12:56
  • 1
    Why not write you entire program in .net then? – spender Feb 04 '13 at 12:57
  • @spender Because R has amazing functionality when it comes to dealing with stats, its amazing for interactively exploring data, and I'm doing a www.coursera.com course that teaches data analysis with R. – Contango Feb 04 '13 at 12:58
  • Downvoted for "You should simply learn the correct methods to use in R". That isn't helpful; it's patronising. – Richie Cotton Feb 04 '13 at 14:10
  • @RichieCotton: It certainly wasn't meant to be or even sound patronising. It is a common and understandable thing for new developers in a certain discipline: They want to use what they already know. But they are on a new platform and therefore should go with the native tools of that platform. Otherwise they will never truly understand that new platform and benefit from its full potential. BTW: Thanks for leaving a comment regarding your downvote, I appreciate that! – Daniel Hilgarth Feb 04 '13 at 14:13
  • @Richie Cotton I understand where you're coming from - its good to learn the features of a new platform. And I certainly will do when it comes to something as simple as strings. However, I actually want to call a lot of other custom .NET functionality from R, but I restricted the question to strings to keep everything simple. – Contango Feb 05 '13 at 09:49