with convenient and improves readability in an interactive context, but it can damage your brain in a programming context where you pass things back and forth through functions and do things in different environments. In the general case, inside R, the use of symbols, rather than names, is a kind of “semantic sugar” that is convenient and readable in interactive use, but mildly outdated for programming [for example, $ , subset ]). If you want to compromise using the name ( "a" ) rather than the character ( a ), I would suggest returning to the simpler obj[[col]] rather than using with here ...
So, as a standalone answer:
foo <- function(object,col) { print(names(object)) print(object[[col]]) }
If you want to allow multiple columns (i.e. character vector)
foo <- function(object,col) { print(names(object)) print(object[col]) }
edit : refrain from using subset with function suggested by @hadley
(this will print the response as a data frame, even if a single column is selected, which may not be what you want).
source share