---
title: "Workflow"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Workflow}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
## Install R Package
```{r install_r_packages, eval = FALSE}
# Enable the KWB-R universe repository
options(repos = c(
kwbr = "https://kwb-r.r-universe.dev",
CRAN = "https://cloud.r-project.org"
))
# Install R package
install.packages("geosalz.phreeqc")
```
## Tidy Lab Data
```{r tidy_lab_data}
library(geosalz.phreeqc)
file_samples <- geosalz.phreeqc::package_file("phreeqc-input.csv")
samples <- read.csv2(file_samples, check.names = FALSE)
knitr::kable(samples)
samples_tidy <- geosalz.phreeqc::tidy_samples(samples) %>%
geosalz.phreeqc::convert_to_numeric_lab_values()
samples_tidy
```
## Prepare PHREEQC Input File
```{r prepare_phreeqc_input}
solutions_input <- geosalz.phreeqc::prepare_solutions_input(
samples_tidy,
title = "Test Dataset"
)
```
Content of `solutions_input` is shown below:
```{r prepare_phreeqc_input_show, echo = FALSE, comment=NA}
cat(solutions_input)
```
## Run PHREEQC
```{r run_phreeqc_run, echo = TRUE}
### Load PHREEQC database "phreeqc.dat"
phreeqc::phrLoadDatabaseString(phreeqc::phreeqc.dat)
### Capture PHREEQC output normally sent to output file into a buffer
phreeqc::phrSetOutputStringsOn(TRUE)
## Run PHREEQC with "solutions_input"
phreeqc::phrRunString(input = solutions_input)
### Retrieves the phreeqc output as a character vector
phreeqc_output <- phreeqc::phrGetOutputStrings()
writeLines(phreeqc_output, "phreeqc_output.txt")
```
Here is the content of `phreeqc_output`
```{r prepare_phreeqc_output, echo = FALSE, comment=NA}
cat(paste0(phreeqc_output, collapse = "\n"))
```
## Get Results
```{r run_phreeqc_reusults, echo = TRUE}
sims <- geosalz.phreeqc::read_simulations(phreeqc_output)
str(sims)
sims_list <- geosalz.phreeqc::convert_simulations_to_list(sims)
str(sims_list)
```
## Export to Excel
```{r run_phreeqc_export, echo = TRUE}
openxlsx::write.xlsx(
x = sims_list,
file = "phreeqc_output.xlsx",
overwrite = TRUE
)
```