Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function to specify column widths: cols_widths() #293

Merged
merged 46 commits into from
Jul 26, 2019
Merged

Conversation

rich-iannone
Copy link
Member

This PR allows for manual adjustments to column widths via the new cols_widths() function. We choose which columns get specific widths (in pixels, usually by use of the px() helper function) and all other columns are assigned a default width value though the .others argument.

There are two ways make the width assignments, both occur in the ...: (1) named arguments, where the names are those of the columns and the values are the dimensions (in px where, for instance, px(80) would helpfully generate 30px), and, (2) paired vars()---px() expressions, which:

  • are separated by commas (e.g., vars(col_b), px(120))
  • allow the use of one or more column names, column indices, or select helpers (e.g., starts_with(), contains(), everything(), etc.) within each vars() expression
  • cannot be mixed with named arguments as in (1)

Here are two examples (using the two different expression styles) that make the same table:

# Use `exibble` to create a gt table;
# with named arguments in `...`, we
# can specify exact the widths for
# those columns (the `.others` value
# will provide a default width for
# any unset columns)
exibble %>%
  dplyr::select(
    num, char, date,
    datetime, row
  ) %>%
  gt() %>%
  cols_widths(
    num = px(150),
    char = px(100),
    date = px(200),
    datetime = px(200),
    .others = px(60)
  )

# We can take a different approach
# in making the same table by using
# paired `vars()`-dimension
# expressions; these are all
# unnamed and we need to ensure
# alternation between `vars()`
# and `px()` expressions
exibble %>%
  dplyr::select(
    num, char, date,
    datetime, row
  ) %>%
  gt() %>%
  cols_widths(
    vars(num), px(150),
    vars(ends_with("r")), px(100),
    vars(starts_with("date")), px(200),
    .others = px(60)
  )

Here is the table:

cols_widths_table

Fixes #111.

* master:
  Add the `cells_stubhead_label()` location helper (#276)
  Refactoring of the `tab_options()` function (#288)
  Add the `path` argument to `gtsave()` (#290)
  Refactoring of heading component (#239)
  Use `sass` package from CRAN (not GitHub) (#313)
Merge branch 'master' into cols-widths

* master:
  Allow an empty table with zero rows to create an empty gt table (#283)
  Fix for Markdown/HTML-formatting of spanner column labels (#251)
  Escaping for row group label (#252)

# Conflicts:
#	R/gt.R
* master:
  Fix for `cell_borders()` (#323)
@rich-iannone rich-iannone changed the title [WIP] Add function to specify column widths: cols_widths() Add function to specify column widths: cols_widths() Jul 25, 2019
@rich-iannone rich-iannone requested a review from schloerke July 26, 2019 16:59
@rich-iannone rich-iannone merged commit 43ba21a into master Jul 26, 2019
@rich-iannone rich-iannone deleted the cols-widths branch July 26, 2019 17:46
rich-iannone added a commit that referenced this pull request Jul 26, 2019
* master:
  Add function to specify column widths: `cols_widths()` (#293)
rich-iannone added a commit to batpigandme/gt that referenced this pull request Nov 11, 2019
* master: (59 commits)
  add fmt_markdown to _pkgdown.yml (rstudio#371)
  Use `tidy_grepl()` instead of `expect_match()`
  Use `tidy_grepl()` & `expect_true()`
  Add `dt_*()` methods for all major, internal data structures (rstudio#355)
  Update CLA text (rstudio#373)
  remove reliance on Github version of sass, which is now updated on CRAN (rstudio#366)
  Fix for invisible top borders in rendered HTML tables (rstudio#359)
  CSS modifications (rstudio#346)
  Incorporate alpha option to `cell_fill()` (rstudio#340)
  Enable option to set row group padding in `tab_options()` (rstudio#342)
  Modify `filter()` statement (rstudio#341)
  Allow use of integers in `pct()`/`px()` (rstudio#336)
  Fix for resolving row group cells (rstudio#335)
  Easier access to footnote mark options (rstudio#333)
  Export `as.tags.gt_tbl()` (rstudio#331)
  Several `rlang`-based fixes (rstudio#334)
  Add function to specify column widths: `cols_widths()` (rstudio#293)
  Add `rstudio/sass` to Remotes list (rstudio#326)
  Fix for `cell_borders()` (rstudio#323)
  Allow an empty table with zero rows to create an empty gt table (rstudio#283)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add option to specify column widths
2 participants