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 50 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              
#>  OFFICIAL-STATISTICS px_official_statistics
#>  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.

Keywords without modifying functions

These 35 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                                
#>  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

526

Version

0.19.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Johan Ejstrud

Last Published

December 16th, 2025

Functions in pxmake (0.19.0)

break_long_lines

Split long strings at commas
get_timeval_type_from_values

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

Change language in table
get_pivot_variables

Get names of pivot variables
format_px_object_as_lines

Get lines for PX-file from px object
get_values_from_time_format

Get time values from TIMEVAL string
modify_languages_in_px

Change language in px object
format_data_df

Format df for px format
get_figures_variable_from_excel

Get figures variable from Excel workbook
get_metadata_df_from_px_lines

Get metadata df from px lines
format_time_values

Format time values for PX-file
get_main_language

Get the main language from metadata
download_px_or_parquet_and_return_path

Download parquet or PX-file and return path
defined_languages

Get languages used in px object
modify_or_add_row

Add or modify value
px_add_totals

Add total levels to variables
enumerate_lines

Add lines numbers
get_data_cube

Get data cube used in PX-file format
px_codepage

CODEPAGE
px_aggregallowed

AGGREGALLOWED
px

Create a px object
get_variable_label

Get variable code and label
px_classification

Create a classification object
greenlanders

Greenlanders
get_mandatory_variables

A list of which variables should be in each sheet
change_pivot_variables

Change pivot variables
px_autopen

AUTOPEN
get_default_encoding

Default encoding to read and save PX-file in
px_description

DESCRIPTION
merge_named_lists

Put two named lists together, remove duplicates and sort
population_gl

Population Greenland
error

Throw error
get_metadata_df_from_px

Get metadata df from px object
px_axis_version

AXIS-VERSION
error_if_agument_is_not_character_string

Throw error if argument is not a character string
get_excel_sheet

Get specific sheet from Excel workbook
px_classification_from_df

px classification from data frame
px_from_data_df

Create a minimal px object from a data frame
get_encoding_from_px_file

Get encoding listed in PX-file
px_decimals

DECIMALS
px_charset

CHARSET
is_path_extension

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

Guess encoding of file
px_classification_from_path

px classification from path
px_confidential

CONFIDENTIAL
mutate_all_vars_to_character

Change all variables to character
px_map

MAP
px_valuenotex

VALUENOTEX
px_values

VALUES
px_next_update

NEXT-UPDATE
px_micro

Create micro PX-files
px_baseperiod

BASEPERIOD
px_elimination

ELIMINATION
px_cellnote

CELLNOTE
px_figures

Change figures variable
px_notex

NOTEX
px_contact

CONTACT
px_contents

CONTENTS
get_px_metadata_regex

Regular expression to parse header in PX-file
px_subject_code

SUBJECT-CODE
px_languages

LANGUAGES
px_subject_area

SUBJECT-AREA
px_language

LANGUAGE
save_px_as_r_script

Save px object as an R script
px_cellnotex

CELLNOTEX
px_save_classification

Save classification as .vs and .agg files
px_cfprices

CFPRICES
validate_px_arguments

Check all arguments to px()
write_value_set

Save classification as .vs file
lst_distinct_and_arrange

Get a sorted list of distinct values in list
validate_px_classification_arguments

Check all arguments to px_classification
get_section

Return lines in section
save_px_as_xlsx

Save px object as an Excel workbook
write_aggregation

Save classification as .agg file
px_note

NOTE
px_data

DATA
px_contvariable

CONTVARIABLE
px_stub

STUB
px_source

SOURCE
smallest_larger_value

Smallest larger value
px_creation_date

CREATION-DATE
px_heading

HEADING
px_from_px_file

Create a px object form a PX-file
px_last_updated

LAST-UPDATED
px_save

Save px object to file
px_precision

PRECISION
px_matrix

MATRIX
save_px_as_px_file

Save px object to PX-file
sort_metadata_df

Sort metadata data frame
px_from_excel

Create a px object from an Excel workbook
px_showdecimals

SHOWDECIMALS
px_link

LINK
validate_px_micro_arguments

Check all arguments to px_micro()
read_px_file

Get PX-file content as lines
readLines_guess_encoding

Read lines from file with guessed encoding
px_infofile

INFOFILE
px_variable_label

Change VARIABLE-LABEL
px_descriptiondefault

DESCRIPTIONDEFAULT
px_domain

DOMAIN
px_update_frequency

UPDATE-FREQUENCY
validate_px_data_arguments

Check all arguments to px_data
px_units

UNITS
return_px

Return px object
replace_na_language_with_main_language

Impute missing language
sort_data_table_by_order

Sort data table by order
pxmake-package

pxmake: Make PX-Files in R
px_keywords

px keywords
px_validate

Check px object
px_valuenote

VALUENOTE
new_classification

Create new classification object
zip_vectors

Zips list
new_px

Create new px object
readLines_with_encoding

Read lines with specific encoding
px_variable_type

VARIABLE-TYPE
px_stockfa

STOCKFA
px_order

Change value order
px_official_statistics

OFFICIAL-STATISTICS
update_data_table

Add value as data table in x
temp_file_with_extension

Create temporary file
section_interval

Get index of first and last line of section
px_title

TITLE
px_copyright

COPYRIGHT
wrap_varaible_in_list

Convert a variable to a list
str_quote

Add quotes around string
px_timeval

TIMEVAL
px_tableid

TABLEID
temp_dir

Create and return path to temporary directory
validate_px_save_arguments

Check all arguments to px_save()
quote_unless_yes_no

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

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

Age classification
add_main_language

Add boolean main_language column
add_language_to_keyword

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

Drop rows with only NA values
add_excel_sheet

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

Add total level to variable
add_sub_key_to_keyword

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

Create a tibble with dummy values
aggregation_df

Get data set from aggregation file
align_data_frames

Align data frames
classification_file_section_heading_regexp

Regexp that matches a classification file section heading
convert_df_to_code

Create code to construct data frame
extract_section

Get section in .vs or .agg file
add_totals_to_df

Add total levels to multiple variables
convert_value_to_code

Create constructing code
fix_px

Fix some common issues in px objects
get_data_table

Return data table from px object