knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Install R Package

# 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

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

solutions_input <- geosalz.phreeqc::prepare_solutions_input(
  samples_tidy,
  title = "Test Dataset"
)

Content of solutions_input is shown below:

cat(solutions_input)

Run PHREEQC

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

cat(paste0(phreeqc_output, collapse = "\n"))

Get Results

sims <- geosalz.phreeqc::read_simulations(phreeqc_output)
str(sims)

sims_list <- geosalz.phreeqc::convert_simulations_to_list(sims)
str(sims_list)

Export to Excel

openxlsx::write.xlsx(
  x = sims_list,
  file = "phreeqc_output.xlsx",
  overwrite = TRUE
)


KWB-R/geosalz.phreeqc documentation built on Aug. 30, 2022, 10:03 a.m.