Getting started creating multilingual px files

This code turns a simple dataframe into a multilingual px-file in English, Danish and Greenlandic with just enough metadata to open in pxwin

Create a sample dataframe, df using codes in the dataframe

library(tidyverse)

df <-
  expand_grid(sex = c("F", "M"),
              age = c("0", "16", "26"),
              time = c("2021", "2022", "2023")
              ) %>%
  mutate(value = round(rnorm(nrow(.), mean = 20, sd = 5), 0)) %>% 
  as_tibble()

df %>% print(n=5)
# A tibble: 18 x 4
  sex   age   time  value
  <chr> <chr> <chr> <dbl>
1 F     0     2021     31
2 F     0     2022     17
3 F     0     2023     22
4 F     16    2021     22
5 F     16    2022     25
# i 13 more rows

use the pxmake library to convert the dataframe to a r-object .rds, .px and .xlsx, like this:

# devtools::install_github('StatisticsGreenland/pxmake')
library(pxmake)

# from dataframe to rds
rds <- metamake(df)

# MATRIX - used also for filename in this example
px_matrix <- "BEXFIRSTML"

# from dataframe to xlsx
metamake(df, paste0(px_matrix,".xlsx"))

# from rds to px
pxmake(rds,paste0(px_matrix,".px"))

# view(rds$metadata)
# view(rds$data)

Some keywords in the r-object (rds) are preset with default values and generated by code. The metadata can be seen using view(rds$metadata)

The sample dataframe can be edited in Excel or R. Metadata is still too vaguely defined for pxwin to show the file, but pxedit does (of course).

For pxwin to show the px-file, these keywords cannot be blank:
MATRIX, CONTENTS, UNITS, SUBJECT-CODE, SUBJECT-AREA

MATRIX & SUBJECT-CODE are not multilingual, but CONTENTS, UNITS & SUBJECT-AREA is.

So update the keywords and add necessary keywords:

# Update keyword(s) 

rds$metadata <-
  rds$metadata %>%
  mutate(
    value = ifelse(keyword == "MATRIX",        px_matrix,           value),
    value = ifelse(keyword == "SUBJECT-CODE", "BE",                 value),
    value = ifelse(keyword == "SUBJECT-AREA", "Population",         value),
    value = ifelse(keyword == "CONTENTS",     "Population",         value),
    value = ifelse(keyword == "UNITS",        "Persons",            value),
    keyword = ifelse(keyword == "VALUES",     "CODES",            keyword)
  ) 

# add keywords
rds$metadata <-
  rds$metadata %>%
  bind_rows(tribble(~keyword, ~language,~variable,~value,
                  "LANGUAGES","en",NA,c("en","da","kl"),
                  "SUBJECT-AREA","da",NA,list("Befolkning"),
                  "SUBJECT-AREA","kl",NA,list("Innuttaasut"),
                  "CONTENTS","da",NA,list("Befolkning"),
                  "CONTENTS","kl",NA,list("Innuttaasut"),
                  "UNITS","da",NA,list("Personer"),
                  "UNITS","kl",NA,list("Inuit"),
                  "STUB","da",NA,c("alder","tid"),
                  "STUB","kl",NA,c("ukiut","piffissaq"),
                  "HEADING","da",NA,c("køn"),
                  "HEADING","kl",NA,c("suiaassuseq"),
                  "VALUES","en","time",c("2021","2022","2023"),
                  "VALUES","da","tid",c("2021","2022","2023"),
                  "VALUES","kl","piffissaq",c("2021","2022","2023"),
                  "VALUES","en","sex",c("Females","Males"),
                  "VALUES","da","køn",c("Kvinder","Mænd"),
                  "VALUES","kl","suiaassuseq",c("Arnat","Angutit"),
                  "VALUES","en","age",c("0 - 15","16 - 25","26 -"),
                  "VALUES","da","alder",c("0 - 15","16 - 25","26 -"),
                  "VALUES","kl","ukiut",c("0 - 15","16 - 25","26 -"),
                  "CODES","da","tid",c("2021","2022","2023"),
                  "CODES","da","køn",c("F","M"),
                  "CODES","kl","suiaassuseq",c("F","M"),
                  "CODES","da","alder",c("0","16","26"),
                  "CODES","kl","ukiut",c("0","16","26")
                  ))


# save metadata changes
metamake(rds, paste0(px_matrix,".xlsx"))

# convert to px-file
pxmake(input = paste0(px_matrix,".xlsx"), 
       out_path = paste0(px_matrix,".px"))

and the multilingual px-file BEXFIRSTML.px will show in pxwin / pxedit / pxweb