Skip to content

Commit

Permalink
Merge pull request #89 from zhezhaozz/zz-glimpse
Browse files Browse the repository at this point in the history
Add `@glimpse`
  • Loading branch information
Karandeep Singh authored Apr 10, 2023
2 parents a5670e3 + f36456f commit 83e469e
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Tidier.jl updates

## v0.7.3 - 2023-04-10
- Added `@glimpse()`

## v0.7.2 - 2023-04-05
- Moved repo to TidierOrg

Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tidier"
uuid = "f0413319-3358-4bb0-8e7c-0c83523a93bd"
authors = ["Karandeep Singh"]
version = "0.7.2"
version = "0.7.3"

[deps]
Chain = "8be319e6-bccf-4806-a6f7-6fae938471bc"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ To support R-style programming, Tidier.jl is implemented using macros.

Tidier.jl currently supports the following top-level macros:

- `@glimpse()`
- `@select()`, `@rename()`, and `@distinct()`
- `@mutate()` and `@transmute()`
- `@summarize()` and `@summarise()`
Expand Down
4 changes: 2 additions & 2 deletions docs/examples/UserGuide/dataset_movies.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ movies = dataset("ggplot2", "movies");
@slice(1:5)
end

# Let's use the `describe()` function, which is re-exported from the `DataFrames.jl` package to describe the dataset.
# Let's use `@glimpse()` to preview the dataset.

describe(movies)
@glimpse(movies)
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ Tidier.jl currently supports the following top-level macros:

```@raw html
!!! example "Top-level macros:"
- `@glimpse()`
- `@select()`, `@rename()`, and `@distinct()`
- `@mutate()` and `@transmute()`
- `@summarize()` and `@summarise()`
Expand Down
26 changes: 25 additions & 1 deletion src/Tidier.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ using Reexport
export Tidier_set, across, desc, n, row_number, starts_with, ends_with, matches, if_else, case_when, ntile,
@select, @transmute, @rename, @mutate, @summarize, @summarise, @filter, @group_by, @ungroup, @slice,
@arrange, @distinct, @pull, @left_join, @right_join, @inner_join, @full_join, @pivot_wider, @pivot_longer,
@bind_rows, @bind_cols, @clean_names, @count, @tally, @drop_na
@bind_rows, @bind_cols, @clean_names, @count, @tally, @drop_na, @glimpse

# Package global variables
const code = Ref{Bool}(false) # output DataFrames.jl code?
Expand Down Expand Up @@ -654,4 +654,28 @@ macro drop_na(df, exprs...)
return df_expr
end

"""
$docstring_glimpse
"""
macro glimpse(df, width = 80)
df_expr = quote
# DataFrame() needed to handle grouped data frames
println("Rows: ", nrow(DataFrame($(esc(df)))))
println("Columns: ", ncol(DataFrame($(esc(df)))))

if $(esc(df)) isa GroupedDataFrame
println("Groups: ", join(string.(groupcols($(esc(df)))), ", "), " [", length(keys($(esc(df)))), "]")
end

for (name, col) in pairs(eachcol(DataFrame($(esc(df)))))
rpad("." * string(name), 15) *
rpad(eltype(col), 15) *
join(col, ", ") |>
x -> first(x, $width) |> # show the first $width number of characters
println
end
end
return df_expr
end

end
40 changes: 40 additions & 0 deletions src/docstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1829,4 +1829,44 @@ julia> @chain df @drop_na(starts_with("a"))
2 │ 2 missing
3 │ 4 4
```
"""

const docstring_glimpse =
"""
@glimpse(df, width = 80)
Preview a DataFrame (or GroupedDataFrame).
The `@glimpse` macro is used to preview a DataFrame or GroupedDataFrame. Each column is printed on a separate row, along with its data type and first few elements, with the output truncated based on the `width`.
# Arguments
- `df`: A DataFrame or GroupedDataFrame.
- `width`: The width of the output, measured in the number of characters. Defaults to 80.
# Examples
```jldoctest
julia> df = DataFrame(
a = 1:100,
b = 1:100,
c = repeat(["a"], 100)
);
julia> @chain df @glimpse
Rows: 100
Columns: 3
.a Int64 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
.b Int64 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
.c String a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,
julia> @chain df begin
@group_by(a)
@glimpse()
end
Rows: 100
Columns: 3
Groups: a [100]
.a Int64 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
.b Int64 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
.c String a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,
```
"""

2 comments on commit 83e469e

@kdpsingh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/81346

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.3 -m "<description of version>" 83e469ee2587ca62c08817fe1bc8ff23d347a48a
git push origin v0.7.3

Please sign in to comment.