Learn R Programming

pxmake

Overview

‘pxmake’ is an R package for creating and modifying PX-files.

With pxmake you can:

  • Import a PX-file, modify it, and save it as a new PX-file.
  • Modify all metadata keywords in a PX-file.
  • Do complex modifications to a PX-file, like adding total levels to a variable.
  • Save a PX-file as an Excel workbook.

Installation

# Install the latest release from CRAN
install.packages('pxmake')

# Or the development version from GitHub
# install.packages('pak')
pak::pak("StatisticsGreenland/pxmake")

How to use

(Find complete documentation on pxmake webpage.)

Use px() to import an existing PX-file into R.

library(pxmake)

# Import PX-file
x <- px(input = "example.px")

Once imported, use one of pxmake’s modifying functions.

In general, modifying functions are named after the keyword they modify. It’s possible to chain multiple modifying functions together in tidyverse style by using the pipe operator %>%.

library(magrittr) # import pipe

# Create px object from data frame
x <- px(data.frame(year = as.character(rep(2021:2023, each = 3)), 
                   group = c('a', 'b', 'c'), 
                   value = runif(9)
                   )
        ) 

head(x$data, 4)
#> # A tibble: 4 × 3
#>   year  group value
#>   <chr> <chr> <dbl>
#> 1 2021  a     0.266
#> 2 2021  b     0.372
#> 3 2021  c     0.573
#> 4 2022  a     0.908

x %>% 
  px_timeval("year") %>%  # Set year as TIMEVAL
  px_heading("year") %>%  # Set year as HEADING
  px_stub("group") %>%    # Set group as STUB
  px_decimals("2") %>%    # Set DECIMALS to 2
  px_save("example.px") # Save as PX-file

Modifying functions

Currently the following 49 keywords have a modifying function in pxmake:

#>  Keyword            Function name        
#>  AGGREGALLOWED      px_aggregallowed     
#>  AUTOPEN            px_autopen           
#>  AXIS-VERSION       px_axis_version      
#>  BASEPERIOD         px_baseperiod        
#>  CELLNOTE           px_cellnote          
#>  CELLNOTEX          px_cellnotex         
#>  CFPRICES           px_cfprices          
#>  CHARSET            px_charset           
#>  CODEPAGE           px_codepage          
#>  CONFIDENTIAL       px_confidential      
#>  CONTACT            px_contact           
#>  CONTENTS           px_contents          
#>  CONTVARIABLE       px_contvariable      
#>  COPYRIGHT          px_copyright         
#>  CREATION-DATE      px_creation_date     
#>  DATA               px_data              
#>  DECIMALS           px_decimals          
#>  DESCRIPTION        px_description       
#>  DESCRIPTIONDEFAULT px_descriptiondefault
#>  DOMAIN             px_domain            
#>  ELIMINATION        px_elimination       
#>  HEADING            px_heading           
#>  INFOFILE           px_infofile          
#>  LANGUAGE           px_language          
#>  LANGUAGES          px_languages         
#>  LAST-UPDATED       px_last_updated      
#>  LINK               px_link              
#>  MAP                px_map               
#>  MATRIX             px_matrix            
#>  NEXT-UPDATE        px_next_update       
#>  NOTE               px_note              
#>  NOTEX              px_notex             
#>  PRECISION          px_precision         
#>  SHOWDECIMALS       px_showdecimals      
#>  SOURCE             px_source            
#>  STOCKFA            px_stockfa           
#>  STUB               px_stub              
#>  SUBJECT-AREA       px_subject_area      
#>  SUBJECT-CODE       px_subject_code      
#>  TABLEID            px_tableid           
#>  TIMEVAL            px_timeval           
#>  TITLE              px_title             
#>  UNITS              px_units             
#>  UPDATE-FREQUENCY   px_update_frequency  
#>  VALUENOTE          px_valuenote         
#>  VALUENOTEX         px_valuenotex        
#>  VALUES             px_values            
#>  VARIABLE-LABEL     px_variable_label    
#>  VARIABLE-TYPE      px_variable_type

In addition to the above, the following 3 modifying functions are available:

#>  Function name
#>  px_add_totals
#>  px_figures   
#>  px_order

See the help page for each modifying function for more information.

These 36 keywords currently doesn’t have a modifying function, but can be implemented.

#>  Keyword             Function name          Priority Complexity
#>  ATTRIBUTE-ID        px_attribute_id                           
#>  ATTRIBUTE-TEXT      px_attribute_text                         
#>  ATTRIBUTES          px_attributes                             
#>  DATABASE            px_database                               
#>  DATANOTE            px_datanote                               
#>  DATANOTECELL        px_datanotecell                           
#>  DATANOTESUM         px_datanotesum                            
#>  DATASYMBOL1         px_datasymbol1                            
#>  DATASYMBOL2         px_datasymbol2                            
#>  DATASYMBOL3         px_datasymbol3                            
#>  DATASYMBOL4         px_datasymbol4                            
#>  DATASYMBOL5         px_datasymbol5                            
#>  DATASYMBOL6         px_datasymbol6                            
#>  DATASYMBOLNIL       px_datasymbolnil                          
#>  DATASYMBOLSUM       px_datasymbolsum                          
#>  DAYADJ              px_dayadj                                 
#>  DEFAULT-GRAPH       px_default_graph                          
#>  DIRECTORY-PATH      px_directory_path                         
#>  DOUBLECOLUMN        px_doublecolumn                           
#>  FIRST-PUBLISHED     px_first_published                        
#>  HIERARCHIES         px_hierarchies                            
#>  HIERARCHYLEVELS     px_hierarchylevels                        
#>  HIERARCHYLEVELSOPEN px_hierarchylevelsopen                    
#>  HIERARCHYNAMES      px_hierarchynames                         
#>  INFO                px_info                                   
#>  KEYS                px_keys                                   
#>  META-ID             px_meta_id                                
#>  OFFICIAL-STATISTICS px_official_statistics                    
#>  PARTITIONED         px_partitioned                            
#>  PRESTEXT            px_prestext                               
#>  PX-SERVER           px_px_server                              
#>  REFPERIOD           px_refperiod                              
#>  ROUNDING            px_rounding                               
#>  SEASADJ             px_seasadj                                
#>  SURVEY              px_survey                                 
#>  SYNONYMS            px_synonyms

Finally these 2 keywords will not have a modifying function, because they are automatically determined by the data.

#>  Keyword     
#>  CODES       
#>  VARIABLECODE

Need help?

If you have any questions or need help, feel free to open an issue on GitHub, or contact Lars and Johan via email.

For developers

PX specification

pxmake is based on the PX-file format specification on Statistics Swedens homepage.

PxJob

Some tests cases uses PxJob. Install pxjob64Win to be able ro run these tests. This only works on Windows.

How to create a new release

  1. Run usethis::use_release_issue() and follow checklist.
  2. Sync fork on PxTools/pxmake.

Copy Link

Version

Install

install.packages('pxmake')

Monthly Downloads

370

Version

0.17.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Johan Ejstrud

Last Published

April 7th, 2025

Functions in pxmake (0.17.0)

format_px_object_as_lines

Get lines for PX-file from px object
get_encoding_from_px_file

Get encoding listed in PX-file
format_time_values

Format time values for PX-file
age_classification

Age classification
px_aggregallowed

AGGREGALLOWED
new_classification

Create new classification object
mutate_all_vars_to_character

Change all variables to character
defined_languages

Get languages used in px object
add_excel_sheet

Add a data frame as a sheet to an Excel workbook
get_timeval_type_from_values

Get time scale code from values (see TIMEVAL in px-specification)
format_data_df

Format df for px format
fix_px

Fix some common issues in px objects
modify_or_add_row

Add or modify value
new_px

Create new px object
get_data_cube

Get data cube used in PX-file format
get_default_encoding

Default encoding to read and save PX-file in
create_dummy_tibbles

Create a tibble with dummy values
convert_value_to_code

Create constructing code
align_data_frames

Align data frames
px_baseperiod

BASEPERIOD
get_excel_sheet

Get specific sheet from Excel workbook
px_classification_from_df

px classification from data frame
px_add_totals

Add total levels to variables
get_mandatory_variables

A list of which variables should be in each sheet
drop_blank_rows

Drop rows with only NA values
px_decimals

DECIMALS
px_classification_from_path

px classification from path
get_figures_variable_from_excel

Get figures variable from Excel workbook
get_main_language

Get the main language from metadata
px_contvariable

CONTVARIABLE
px_cfprices

CFPRICES
px_cellnote

CELLNOTE
px_cellnotex

CELLNOTEX
get_metadata_df_from_px

Get metadata df from px object
guess_file_encoding

Guess encoding of file
px_copyright

COPYRIGHT
px_from_excel

Create a px object from an Excel workbook
px_from_data_df

Create a minimal px object from a data frame
px_description

DESCRIPTION
px_keywords

px keywords
extract_section

Get section in .vs or .agg file
px_contact

CONTACT
error_if_agument_is_not_character_string

Throw error if argument is not a character string
px_contents

CONTENTS
px_data

DATA
px_creation_date

CREATION-DATE
is_path_extension

Check if a path has a specific extension (function factory)
px_confidential

CONFIDENTIAL
get_section

Return lines in section
get_px_metadata_regex

Regular expression to parse header in PX-file
px_infofile

INFOFILE
px_heading

HEADING
get_values_from_time_format

Get time values from formatted string
get_metadata_df_from_px_lines

Get metadata df from px lines
modify_languages_in_px

Change language in px object
px_note

NOTE
px_notex

NOTEX
px_title

TITLE
get_pivot_variables

Get names of pivot variables
merge_named_lists

Put two named lists together, remove duplicates and sort
px_domain

DOMAIN
px_stockfa

STOCKFA
px_descriptiondefault

DESCRIPTIONDEFAULT
modify_languages_in_table

Change language in table
px_link

LINK
px_units

UNITS
lst_distinct_and_arrange

Get a sorted list of distinct values in list
px_last_updated

LAST-UPDATED
px_map

MAP
smallest_larger_value

Smallest larger value
sort_metadata_df

Sort metadata data frame
px_axis_version

AXIS-VERSION
px_tableid

TABLEID
px_autopen

AUTOPEN
validate_px_classification_arguments

Check all arguments to px_classification
px_language

LANGUAGE
px_matrix

MATRIX
px_order

Change value order
px_valuenotex

VALUENOTEX
validate_px_micro_arguments

Check all arguments to px_micro()
get_variable_label

Get variable code and label
greenlanders

Greenlanders
px_codepage

CODEPAGE
px

Create a px object
px_stub

STUB
px_variable_type

VARIABLE-TYPE
pxmake-package

pxmake: Make PX-Files in R
population_gl

Population Greenland
px_precision

PRECISION
readLines_with_encoding

Read lines with specific encoding
px_micro

Create micro PX-files
px_next_update

NEXT-UPDATE
write_aggregation

Save classification as .agg file
read_px_file

Get PX-file content as lines
write_value_set

Save classification as .vs file
quote_unless_yes_no

Add quotes around unless in some very specific cases required by the px format
px_elimination

ELIMINATION
readLines_guess_encoding

Read lines from file with guessed encoding
str_quote

Add quotes around string
temp_dir

Create and return path to temporary directory
px_classification

Create a classification object
px_charset

CHARSET
px_save

Save px object to file
px_save_classification

Save classification as .vs and .agg files
px_source

SOURCE
px_from_px_file

Create a px object form a PX-file
px_valuenote

VALUENOTE
px_showdecimals

SHOWDECIMALS
save_px_as_xlsx

Save px object as an Excel workbook
px_figures

Change figures variable
px_timeval

TIMEVAL
zip_vectors

Zips list
px_subject_area

SUBJECT-AREA
wrap_varaible_in_list

Convert a variable to a list
section_interval

Get index of first and last line of section
validate_px_save_arguments

Check all arguments to px_save()
px_subject_code

SUBJECT-CODE
px_variable_label

Change VARIABLE-LABEL
return_px

Return px object
px_values

VALUES
replace_na_language_with_main_language

Impute missing language
px_update_frequency

UPDATE-FREQUENCY
px_languages

LANGUAGES
px_validate

Check px object
validate_px_arguments

Check all arguments to px()
temp_file_with_extension

Create temporary file
save_px_as_px_file

Save px object to PX-file
save_px_as_r_script

Save px object as an R script
add_sub_key_to_keyword

Add a sub key word (see px-specification for details on sub keys)
aggregation_df

Get data set from aggregation file
add_cell_to_keyword

Add cell name to keywords that support it (see px-specification)
add_total_level_to_var

Add total level to variable
add_language_to_keyword

Add language, other than the main langue, to keyword
add_totals_to_df

Add total levels to multiple variables
break_long_lines

Split long strings at commas
classification_file_section_heading_regexp

Regexp that matches a classification file section heading
add_main_language

Add boolean main_language column
change_pivot_variables

Change pivot variables
enumerate_lines

Add lines numbers
error

Throw error
convert_df_to_code

Create code to construct data frame