---
title: "Sample datasets with the correspondenceTables package"
output: rmarkdown::html_vignette

vignette: >
  %\VignetteIndexEntry{Sample datasets with the correspondenceTables package}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
library(correspondenceTables)
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
tmp_dir<-tempdir()
csv_files<-list.files(tmp_dir, pattern = ".csv")
if (length(csv_files)>0) unlink(csv_files)
```

This vignette provides information about applying the correspondenceTables package on the sample datasets included in it.

ATTENTION: please set as working directory a folder different than the folder in which the package has been installed.

LOCAL COPIES OF THE SAMPLE DATA

If users wish so, they can make copies of the sample datasets in a local folder of their choice. There are two ways of doing this:

1) Unpack into any folder of your choice the tar.gz file in which the package has arrived. All sample datasets may be found in the "inst/extdata" subfolder of this folder.

2) Copy sample datasets from the "extdata" subfolder of the folder in which the package has been installed in your PC's R library.

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

ACCESSING SAMPLE DATASETS FROM WITHIN THE PACKAGE

Application of function updateCorrespondenceTable().

Case 1

Execute the following code in order to get the path of the required input files.

```{r}
A <- system.file("extdata", "CN2021.csv", package = "correspondenceTables")
AStar <- system.file("extdata", "CN2022.csv", package = "correspondenceTables")
B <- system.file("extdata", "CPA21.csv", package = "correspondenceTables")
AB <- system.file("extdata", "CN2021_CPA21.csv", package = "correspondenceTables")
AAStar <- system.file("extdata", "CN2021_CN2022.csv", package = "correspondenceTables")
```

Execute the following code line to apply function updateCorrespondenceTable() on these data.

```{r}
UPC <- updateCorrespondenceTable(A, B, AStar, AB, AAStar, file.path(tmp_dir,"updateCorrespondenceTableCase1.csv"),
                                 "B", 0.4, 0.4)
print(UPC[[1]][1:10, 1:7])
print(UPC[[2]])
```

Case 2

Execute the following code in order to get the path of the required input files.

```{r}
A <- system.file("extdata", "CN2021.csv", package = "correspondenceTables")
AStar <- system.file("extdata", "CN2022.csv", package = "correspondenceTables")
B <- system.file("extdata", "PRODCOM2021.csv", package = "correspondenceTables")
AB <- system.file("extdata", "CN2021_PRODCOM2021.csv", package = "correspondenceTables")
AAStar <- system.file("extdata", "CN2021_CN2022.csv", package = "correspondenceTables")
```

Execute the following code line to apply function updateCorrespondenceTable() on these data.

```{r, results = "hide"}
UPC <- updateCorrespondenceTable(A, B, AStar, AB, AAStar, file.path(tmp_dir,"updateCorrespondenceTableCase2.csv"), "A", 0.4, 0.3)
```

Case 3

Execute the following code in order to get the path of the required input files.

```{r}
A <- system.file("extdata", "NAICS2017.csv", package = "correspondenceTables")
AStar <- system.file("extdata", "NAICS2022.csv", package = "correspondenceTables")
B <- system.file("extdata", "NACE.csv", package = "correspondenceTables")
AB <- system.file("extdata", "NAICS2017_NACE.csv", package = "correspondenceTables")
AAStar <- system.file("extdata", "NAICS2017_NAICS2022.csv", package = "correspondenceTables")
```

Execute the following code line to apply function updateCorrespondenceTable() on these data.

```{r, results = "hide"}
UPC <- updateCorrespondenceTable(A, B, AStar, AB, AAStar, file.path(tmp_dir,"updateCorrespondenceTableCase3.csv"), "none", 0.5, 0.3)
```

Case 4

Execute the following code in order to get the path of the required input files.

```{r}
A <- system.file("extdata", "CN2021.csv", package = "correspondenceTables")
AStar <- system.file("extdata", "CN2022.csv", package = "correspondenceTables")
B <- system.file("extdata", "NST2007.csv", package = "correspondenceTables")
AB <- system.file("extdata", "CN2021_NST2007.csv", package = "correspondenceTables")
AAStar <- system.file("extdata", "CN2021_CN2022.csv", package = "correspondenceTables")
```

Execute the following code line to apply function updateCorrespondenceTable() on these data.

```{r, results = "hide"}
UPC <- updateCorrespondenceTable(A, B, AStar, AB, AAStar, file.path(tmp_dir,"updateCorrespondenceTableCase4.csv"), "B", 0.4, 0.3)
```

Case 5

Execute the following code in order to get the path of the required input files.

```{r}
A <- system.file("extdata", "CN2021.csv", package = "correspondenceTables")
AStar <- system.file("extdata", "CN2022.csv", package = "correspondenceTables")
B <- system.file("extdata", "SITC4.csv", package = "correspondenceTables")
AB <- system.file("extdata", "CN2021_SITC4.csv", package = "correspondenceTables")
AAStar <- system.file("extdata", "CN2021_CN2022.csv", package = "correspondenceTables")
```

Execute the following code line to apply function updateCorrespondenceTable() on these data.

```{r, results = "hide"}
UPC <- updateCorrespondenceTable(A, B, AStar, AB, AAStar, file.path(tmp_dir,"updateCorrespondenceTableCase5.csv"), "B", 0.3, 0.7)
```

Case 6

Execute the following code in order to get the path of the required input files.

```{r}
A <- system.file("extdata", "CN2021.csv", package = "correspondenceTables")
AStar <- system.file("extdata", "CN2022.csv", package = "correspondenceTables")
B <- system.file("extdata", "BEC4.csv", package = "correspondenceTables")
AB <- system.file("extdata", "CN2021_BEC4.csv", package = "correspondenceTables")
AAStar <- system.file("extdata", "CN2021_CN2022.csv", package = "correspondenceTables")
```

Execute the following code line to apply function updateCorrespondenceTable() on these data.

```{r, results = "hide"}
UPC <- updateCorrespondenceTable(A, B, AStar, AB, AAStar, file.path(tmp_dir,"updateCorrespondenceTableCase6.csv"), "B", 0.3, 0.6)
```


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Application of function newCorrespondenceTable().

The function fullPath is used in all cases in order to get the path of the required input files.

```{r}
fullPath <- function(CSVraw, CSVappended){
  NamesCsv <- system.file("extdata", CSVraw, package = "correspondenceTables")
  A <- read.csv(NamesCsv, header = FALSE, sep = ",")
   for (i in 1:nrow(A)) {
    for (j in 1:ncol(A)) {
      if (A[i,j]!="") {
        A[i, j] <- system.file("extdata", A[i, j], package = "correspondenceTables")
      }}}
  write.table(x = A, file = file.path(tmp_dir,CSVappended), row.names = FALSE, col.names = FALSE, sep = ",")
  return(A)
}
```

Case 1

```{r, results = "hide"}
fullPath("names1.csv", "names.csv")
```

Execute the following code to apply function newCorrespondenceTable() on these data.

```{r, results = "hide"}
system.time(NCT <- newCorrespondenceTable(file.path(tmp_dir,"names.csv"), file.path(tmp_dir,"newCorrespondenceTableCase1.csv"), "A", 0.5))
```

```{r}
print(NCT[[1]][1:10, 1:6])
print(NCT[[2]])
```

Case 2

```{r, results = "hide"}
fullPath("names2.csv", "names.csv")
```

Execute the following code to apply function newCorrespondenceTable() on these data.

```{r, results = "hide"}
system.time(NCT <- newCorrespondenceTable(file.path(tmp_dir,"names.csv"), file.path(tmp_dir,"newCorrespondenceTableCase2.csv"), "B", 0.5))
```

Case 3

```{r, results = "hide"}
fullPath("names3.csv", "names.csv")
```

Execute the following code to apply function newCorrespondenceTable() on these data.

```{r, results = "hide"}
system.time(NCT <- newCorrespondenceTable(file.path(tmp_dir,"names.csv"), file.path(tmp_dir,"newCorrespondenceTableCase3.csv"), "B", 0.5))
```

Case 4

```{r, results = "hide"}
fullPath("names4.csv", "names.csv")
```

Execute the following code to apply function newCorrespondenceTable() on these data.

```{r, results = "hide"}
system.time(NCT <- newCorrespondenceTable(file.path(tmp_dir,"names.csv"), file.path(tmp_dir,"newCorrespondenceTableCase4.csv"), "none", 0.96))
```

```{r message=TRUE, warning=TRUE, include=FALSE}
csv_files<-list.files(tmp_dir, pattern = ".csv")
if (length(csv_files)>0) unlink(csv_files)
```



































