Reputation: 1875
I want to see the contents of a dataframe that is created inside a function after the dataframe gets created.
Let's say the dataframe is called df in the function. If I just include a line of code like this:
df
I don't get any display to the console. I would get the dump of the dataframe contents outside a function.
Thanks
Upvotes: 1
Views: 2931
Reputation: 7714
df <- data.frame(x = rpois(100,80))
print_me <- function(x){
print(x)
}
print_me(df)
Upvotes: 1
Reputation: 174948
In an interactive session, when you type the name of an object, like df
, R implicitly prints()
the object using an appropriate method. It is as if you'd typed print(df)
, without you actually having to type all those extra characters.
However, there are a few places that this automatic printing is not active
for
, while
etc.,In such cases, you need to explicitly print()
an object.
This often catches people out with lattice and ggplot2 plots, which need to be print()
ed to get them drawn on the device. but in general use, the user never has to explicitly print()
these plots to get something drawn.
Upvotes: 4
Reputation: 395903
You're expecting it to print out because when you type the name of an object into R's shell, you have the representation of the object helpfully echoed back to you. Inside a function, this doesn't happen unless you explicitly tell R to print the information.
If print(df)
doesn't work, look for buffered output and be sure it's turned off.
Upvotes: 3
Reputation: 52697
When you type a variable name in the console and hit enter, R silently passes that variable to print
. In order to get the same effect from inside a function, you need to explicitly print. In your case:
print(df)
Upvotes: 1