how to direct output of data(package = '[package]') to console

In my RStudio instance, executing the command data(package = 'ggplot2') renders its output to a tab in the 'Source Editor' pane -- is there a way of directing the output to the console? I ask because in require my students to post reproducible examples that include the output of their commands, and runningreprex() renders only the command itself.

Thanks,
David

gg2_data_obj <- data(package = 'ggplot2')
gg2_results_obj<- gg2_data_obj$results
gg2_tbl <- tibble::as_tibble(gg2_results_obj)

print(gg2_tbl)

The result of data(...) is of class 'packageIQR'. You can access the contents of the file that is being produced by looking at the 'results' element like so:

data(package = 'ggplot2')$results

Then, just wrap in as.data.frame to make it a nicer look, and customize from there:

as.data.frame(data(package = 'ggplot2')$results)

Thanks Michael and Nir (@nirgrahamuk) -- It seems that objects of class packageIQR return results that can't be printed directly, which I don't yet understand. I had been hoping there might be an argument I could supply that would redirect where the dataset information would appear, but maybe that's not possible.

Thanks again,
David

I figured something out.

 options(pager="console")
 data(package="ggplot2")

Thanks, Nir: What OS are you using? On mine (Mac OSX), options(pager="console") doesn't seem to change the behavior of data(package="ggplot2")

Windows. Perhaps you could research pager options for macOS ?

There is another solution I found which involved editing print.packageIQR function , but that might be considered, "a hack"

No one has asked you why you want to have your students use data(package=",,,") as part of an assignment. There are other R commands, like history()` that render output to a separate pane, but not ordinarily used in reproducible examples for assignments.

When they learn a new command, my students are required to write a post describing the behavior of the command that includes a reproducible example. I usually frame the task as an exercise in explaining something you learned in class to someone who was absent so they can both read through an example and reproduce it, and we happened to go over the data() command in class.

Thanks for the suggestions, Nir -- I'll see if I can sort out what's going on, and will look into both directions.

Here is a peek under the hood to what data() is doing when listing packages (i.e. use of the pager). I show where the content can be intercepted and printed in the 'normal' way.

https://www.diffchecker.com/banYAuUM

This link should persist for 7 days

Thanks, Nir! Where did the original function you modified come from? I've trying to locate source code that controls printing behavior, but with no success.

its in the utils namespace.
I located it by typing the following in the console:

getAnywhere(data())

I think you meant getAnywhere(print.packageIQR), which is what worked for me -- thanks, again, Nir! All very helpful.

right, haha.
Yes, I used getAnywhere on data, to find that it was print.packageIRQ that was being used, and therefore I used geyAnywhere to investigate that in turn. glad I could help you along.

cheers

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.