How to "print" a dataframe representation in a string?

How do I “print” a representation of a dataframe in within a String, e.g. this would not work:

df = DataFrame(c1=["a","b"],c2=[1,2])
myStr = """
My dataframe:

$(show(df,allrows=true, allcols=true))

bla,bla,bla...
"""

You’re looking for sprint.

uhhh… can’t get it working (MethodError):

myStr = """
My dataframe:
$(sprint(show,df,allrows=true,allcols=true;context= :compact => true) )
bla,bla,bla...
"""

However, just string(df) seems to work:

myStr = """
My dataframe:
$(string(df))
bla,bla,bla...
"""
julia> myStr = """
       My dataframe:
       $(sprint(io -> show(IOContext(io, :compact => true), df, allrows=true, allcols=true)))
       bla,bla,bla...
       """

works fine. string should be unnessecary when interpolating, since IIRC it will be called automatically.

2 Likes