I’m attempting to make a function for a package that allows the user to read in data without quotation marks. It works almost as expected except the function turns 3.30 into 3.3 even though the final output is character. How can I get my function to read in elements that are numbers but not to drop trailing zeros. The big picture idea is:
- function that reads elements without using quotation marks
- function will not discard what R considers non significant zeros
- though the elements may look numeric I want them to be outputted as character elements
Here are two attempts and what they out put (one attempt with substitute(...()) and the other with match.call(expand.dots = FALSE):
#attempt #1
fun <-
function(...){
substitute(...())
}
fun(3.30, 5.05)
#dropped zero on 3:30 (not what I want)
> fun(3.30, 5.05)
[1] "3.3" "5.05"
#attempt #2
fun <-
function(...){
x <- match.call(expand.dots = FALSE)
as.character(x[[2]])
}
fun(3.30, 5.05)
#again dropped zero on 3:30 (not what I want)
> fun(3.30, 5.05)
[1] "3.3" "5.05"
With quotation marks it outputs what I want but this defeats the purpose of not having to provide quotation marks:
fun('3.30', '5.05')
> fun('3.30', '5.05')
[1] "3.30" "5.05"
PS if someone has a better title for this thread feel free to edit
Well there is one way I could think of but it isn’t quite pretty and might not be stable. R also saves history of the entered calls. That could be used to obtain the exact input:
Example:
I am not sure how stable this would be, there will probably be a dozen cases it will break. Also, this assumes the function is named
fun.