I'm not pretty familiar with R, but anyhow I'm writing a R wrapper for a c library. I come across this problem. How do I decide if the input argument is a string? In details,should I write like this:
dyn.load("hello.so")
do_process <- function(str) {
if(!is.character(str))
stop("not a character or string");
result <- .Call("hello", as.character(str))
return result
}
or this:
dyn.load("hello.so")
do_process <- function(str) {
if(!is.string(str))
stop("not a character or string");
result <- .Call("hello", as.string(str))
return result
}
Thanks.
String refers to a sequence of characters represented as a single data type. Character Array is a sequential collection of data type char. Strings are immutable. Character Arrays are mutable.
character() function in R converts a numeric object to a string data type or a character object. If the collection is passed to it as an object, it converts all the elements of the collection to a character or string type.
Strings are a bunch of character variables. It is a one-dimensional array of characters. One or more characters enclosed in a pair of matching single or double quotes can be considered a string in R. Strings represent textual content and can contain numbers, spaces, and special characters.
The maximum length of a character constant can be one character. A string constant can be any length. A single character string constant has an equivalent integer value.
is.string
is a function from the xtable
package. In the details section of the help page it explicitly says "These functions are private functions used by print.xtable. They are not intended to be used elsewhere."
As such, I would avoid using those functions.
In R
there is no string
data type. Instead it is called character
and you can use is.character
to do the check you're describing.
Also, as I mentioned in my comment, avoid using important base functions as variable names. Specifically str
is used to view the structure of an object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With