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