@@ -25,23 +25,24 @@ function dataset(package_name::AbstractString, dataset_name::AbstractString)
2525 error (" Unable to locate dataset file $rdaname or $csvname " )
2626 end
2727 # Finally, inject metadata into the dataframe to indicate origin:
28- DataFrames . metadata! (dataset, " RDatasets.jl" , (string (package_name), string (dataset_name)))
28+ metadata! (dataset, " RDatasets.jl" , (string (package_name), string (dataset_name)))
2929 return dataset
3030end
3131
3232
3333"""
3434 RDatasets.description(package_name::AbstractString, dataset_name::AbstractString)
35- RDatasets.description(df::DataFrame) # only call this on dataframes from RDatasets!
35+ RDatasets.description(df::AbstractDataFrame; default=nothing)
3636
37- Returns an `RDatasetDescription` object containing the description of the dataset.
37+ Return an `RDatasetDescription` object containing the description of the dataset.
3838
3939Invoke this function in exactly the same way you would invoke `dataset` to get the dataset itself.
4040
41- This object prints well in the REPL, and can also be shown as markdown or HTML.
41+ This object prints well in the REPL, and can also be shown as Markdown or HTML.
4242
43- !!! note Unexported
44- This function is left deliberately unexported, since the name is pretty common.
43+ When passing a `DataFrame`, it must have been obtained from `RDatasets.dataset`. If the DataFrame
44+ does not have the required metadata, an error is thrown unless a `default` value is provided,
45+ in which case that value is returned instead.
4546"""
4647function description (package_name:: AbstractString , dataset_name:: AbstractString )
4748 doc_html_file = joinpath (@__DIR__ , " .." , " doc" , package_name, " $dataset_name .html" )
@@ -53,15 +54,15 @@ function description(package_name::AbstractString, dataset_name::AbstractString)
5354end
5455
5556# This is a convenience function to get the description of a dataset from a DataFrame.
56- # Since we set metadata on the DataFrame, we can use this to get the description,
57- # if it exists.
58- function description (df:: AbstractDataFrame )
59- if " RDatasets.jl" in DataFrames. metadatakeys (df)
60- package_name, dataset_name = DataFrames. metadata (df, " RDatasets.jl" )
57+ # Since we set metadata on the DataFrame, we can use this to get the description.
58+ function description (df:: AbstractDataFrame ; default= nothing )
59+ if " RDatasets.jl" in metadatakeys (df)
60+ package_name, dataset_name = metadata (df, " RDatasets.jl" )
6161 return description (package_name, dataset_name)
62+ elseif default != = nothing
63+ return default
6264 else
63- @warn " No metadata indicating dataset origin found. Returning default description."
64- return RDatasetDescription (" No description available." )
65+ throw (ArgumentError (" DataFrame does not have RDatasets.jl metadata. Use a DataFrame obtained from `RDatasets.dataset`, or provide a `default` value." ))
6566 end
6667end
6768
7172A type to hold the content of a dataset description.
7273
7374The main purpose of its existence is to provide a way to display the content
74- differently in HTML and markdown contexts.
75+ differently in HTML and Markdown contexts.
7576
76- Invoked through [`RDatasets.description`](@ref).
77+ Obtained through [`RDatasets.description`](@ref).
7778"""
7879struct RDatasetDescription
7980 content:: String
0 commit comments