nutterb / pixiedust

Tables So Beautifully Fine-Tuned You Will Believe It's Magic.
179 stars 18 forks source link

get a data.frame (with all characters) from dust-object #33

Closed ghost closed 8 years ago

ghost commented 8 years ago

I wondered if it is possible to get a data.frame from a dustified/sprinkled dust-object. Sure, the orignial classes of the data.frame are lost and all characters then. A feature to get the data.frame with all characters and nice rounding etc. by pixiedust would come in handy when one wants to print a data.frame (without any further calculations done to it, esp. no summary calculations) with a package like stargazer or sjPlot (transposing the data.frame could be another feature?).

nutterb commented 8 years ago

I'm afraid I'm not really understanding what you want. Are you thinking of something like as.data.frame.dust method? Or a default scheme of rounding and formatting( like a ggplot theme)? Or something completely different?

ghost commented 8 years ago

Yes, an as.data.frame.dust method. As far as I understood, columns in a dust object are no more of their original class but character() instead. Thus, the result of as.data.frame.dust would be a data.frame with all columns as character() (and thus the nice sprinkling going on there). Such a method could be used to pre-process the layout a bit. Does that make sense?

nutterb commented 8 years ago

By a happy accident, the dust object keeps a column that provides the original class of the value. Since I've converted everything to character, I have to keep the class in order to know if rounding is appropriate. Using that, I'm able to pretty well reverse the process and re-create the original data frame.

Caveats: I've not tested this with factors and don't really know what would happen if a factor were in the data frame (for that matter, I'm not entirely sure how pixiedust would react to factors in general...but we'll ignore that glaring omission for now). I'll play around with this more if this is approaching what you are wanting.

The code for my first attempt at this is in a gist (https://gist.github.com/nutterb/73ba9ccd01b964f2e6fd) Please look it over and try it. If it's what you're expecting, I can roll the primitive version into the next release and work on beefing it up to manage other data types.

nutterb commented 8 years ago

It just occurred to me that you probably intend to as.data.frame the dust object after all of the sprinkling (hence, "and thus the nice sprinkling going on there"). That will change the work I've done so far.

Sorry to be so thick. I'll work this out this week.

ghost commented 8 years ago

Would be ideal to get both, i.e. the original data.frame and a "sprinklyfied" version (the latter being all characters). Will check the gist in a day or two.

nutterb commented 8 years ago

This is completed in latex-tables now, too. Hopefully I'll get this pushed to CRAN this weekend.

The syntax is as.data.frame(dust_object, ..., sprinkled = TRUE) which gives you the option of the sprinklified data frame or the reconstruction.

ghost commented 8 years ago

Thought dfs_dmtcars (as.data.frame(..., sprinkled=T) would have the same rounding as dmtcars (compare columns drat, wt, qsec)? [pixiedust v0.4.2 from branch latex-tables]

data(mtcars)
dmtcars <- sprinkle_table(dust(mtcars), round = 1)
dmtcars
#>     mpg cyl  disp  hp drat  wt qsec vs am gear carb
#> 1    21   6   160 110  3.9 2.6 16.5  0  1    4    4
#> 2    21   6   160 110  3.9 2.9   17  0  1    4    4
#> 3  22.8   4   108  93  3.8 2.3 18.6  1  1    4    1
#> 4  21.4   6   258 110  3.1 3.2 19.4  1  0    3    1
#> 5  18.7   8   360 175  3.1 3.4   17  0  0    3    2
[...snipp...]
df_dmtcars <- as.data.frame(dmtcars)
dfs_dmtcars <- as.data.frame(dmtcars, sprinkled = T)
dfs_dmtcars
#>     mpg cyl  disp  hp drat    wt  qsec vs am gear carb
#> 1  21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4
#> 2  21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4
#> 3  22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1
#> 4  21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1
#> 5  18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2
[...snipp...]
nutterb commented 8 years ago

I would have thought so too. I seem to have reversed the documented logic (ie, sprinkled = FALSE would give you the sprinkled version). I just fixed that in the code, but it turns out that when sprinkled, you get a tbl_df instead of a data frame. I'll work on that later tonight.