acowley / Frames

Data frames for tabular data.
Other
297 stars 41 forks source link

Show instance / printing functions for Frame? #130

Closed chfin closed 3 years ago

chfin commented 5 years ago

It would be nice to have a Show instance for Frames, or at least a function to quickly print a frame manually.

I'm currently using something like this:

showRow :: (RecMapMethod Show ElField a, RecordToList a)
        => (Record a) -> String
showRow row = intercalate "\t" $ showFields row

showHeader :: forall a . (ColumnHeaders a) => Frame (Record a) -> String
showHeader frame = intercalate "\t" $ columnHeaders (Proxy :: Proxy (Record a))

viewFrame :: (RecMapMethod Show ElField a, RecordToList a, ColumnHeaders a)
          => Frame (Record a) -> IO ()
viewFrame frame = do
  putStrLn $ showHeader frame
  mapM_ (putStrLn . showRow) frame

This could be easily turned into a Show instance or a handful of manual showing functions. Having Show would be easier to use, but lacks a corresponding Read instance. Some kind of preview that only shows a limited number of rows might also be useful.

If you want, I can prepare a pull request.

teto commented 3 years ago

I was looking for similar functions to troubleshoot an issue so it's a yes from me but I am no maintainer.

acowley commented 3 years ago

Yeah, I don't know how this isn't there already! I guess pipePreview was serving that role, but it's not nearly obvious enough. Is the IO () type on viewFrame the best option or should we return a list of lines to make it easier to take a prefix?

acowley commented 3 years ago

I took a whack at this. Let me know if there are any suggestions!

acowley commented 3 years ago

I've pushed these in the 0.7.1 release. I hope they are helpful!