Skip to contents

gpc_camelize() takes an input 'snake_case' vector and transforms it to CamelCase.

gpc_columns_clean() is used internally to ensure consistent column formatting.

gpc_colnames_standardize() transforms column names to shorter equivalents, primary useful for (horizontal) contraction of table displays.

gpc_datatable() standardizes column names for consistent display. In non-interactive sessions it returned a DT::datatable() for interactive navigation.

gpc_datatable_download() adds download buttons to tables

gpc_short_title() shortens titles, e.g., for use as labels in interactive plots.

Usage

gpc_camelize(x)

gpc_columns_clean(.data)

gpc_colnames_standardize(.data = NULL)

gpc_datatable(.data, truncate_columns = TRUE, width = 40L)

gpc_datatable_download(.data, truncate_columns = FALSE, width = 40L)

gpc_shorten(x, to = 40L)

Arguments

x

character(), e.g., project_title to be truncated.

.data

A tibble, usually from an invocation of reporter_projects() or icite(). .data can also be NULL for gpc_colnames_standardize(), in which case the return value is a tibble summarizing how column names 'from' reporter_projects() or icite() are translated 'to' display column names.

truncate_columns

logical(1) truncate long column names (default: TRUE)?

width

integer(1) number of characters to truncate at; remaining characters are replaced by '...'.

to

integer(1) maximum number of characters to retain in x.

Details

gpc_camelize() is useful in standardizing the user interface to the API, where the input 'include_fields' are CamelCase in the API, but the return values are snake_case. Using gpc_camelize() (internally) allows the user to interact with the API using a consistent snake_case style.

gpc_columns_clean() performs the following:

  • Remove leading or trailing whitespace on 'character' columns.

  • Coerce columns whose name ends with _date to as.Date().

  • Coerce 'pmid', 'year' / 'fiscal year' and 'citation_count' to integer()

  • Remove 'group' annotation from tibbles created during processing.

gpc_datatable() is a convenience function used in vignettes to (a) standardize column names for better formatting; (b) provides a consistent presentation of DT::datatable without rownames, with pageLength of 5, and horizontal scrolling, which means that long tables don't overwhelm articles or spill into margins, and (c) only transforms the table when not interactive.

Articles typically use gpc_datatable() in a code chunk with include = FALSE, so that the user sees the datatable but not code.

Examples

gpc_camelize(c("opportunity_number", "core_project_num"))
#> [1] "OpportunityNumber" "CoreProjectNum"   

projects <- tribble(
    ~contact_pi_name, ~project_start_date,    ~pmid, ~project_title,
    "Ima Pi ",        "2018-07-15T12:07:00Z", "123", "This is the title",
    "Iman Other ",    "2021-01-14T12:01:00Z", "456", "A short title"
)

clean <-
    projects |>
    gpc_columns_clean()
clean           # no trailing whitespace; dates as Date objects;
#> # A tibble: 2 × 4
#>   contact_pi_name project_start_date  pmid project_title    
#>   <chr>           <date>             <int> <chr>            
#> 1 Ima Pi          2018-07-15           123 This is the title
#> 2 Iman Other      2021-01-14           456 A short title    

gpc_colnames_standardize()      # summarize column name standardization
#> # A tibble: 13 × 2
#>    from                      to              
#>    <chr>                     <chr>           
#>  1 opportunity_number        foa             
#>  2 core_project_num          project         
#>  3 coreproject               project         
#>  4 project_start_date        start_date      
#>  5 project_end_date          end_date        
#>  6 fiscal_year               year            
#>  7 award_amount              amount          
#>  8 contact_pi_name           contact_pi      
#>  9 project_title             title           
#> 10 citation_count            citn            
#> 11 relative_citation_ratio   rcr             
#> 12 field_citation_rate       fcr             
#> 13 cited_by_core_project_num cited_by_project
gpc_colnames_standardize(clean) # shorter column names
#> # A tibble: 2 × 4
#>   contact_pi start_date  pmid title            
#>   <chr>      <date>     <int> <chr>            
#> 1 Ima Pi     2018-07-15   123 This is the title
#> 2 Iman Other 2021-01-14   456 A short title    

clean |>             # standardized colnames;
    gpc_datatable()  # datatable in non-interactive session
clean |> gpc_datatable_download()
clean |> mutate(short_title = gpc_shorten(project_title, 13)) #> # A tibble: 2 × 5 #> contact_pi_name project_start_date pmid project_title short_title #> <chr> <date> <int> <chr> <chr> #> 1 Ima Pi 2018-07-15 123 This is the title This is th... #> 2 Iman Other 2021-01-14 456 A short title A short title