---
title: "twbparser-intro"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{twbparser-intro}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(twbparser)
ok <- FALSE
twb_path <- system.file("extdata", "test_for_wenjie.twb", package = "twbparser")
if (nzchar(twb_path) && file.exists(twb_path)) {
parser <- TwbParser$new(twb_path)
ok <- TRUE
} else {
cat("> Demo .twb not found in installed package. Skipping executable examples.\n")
}


```

# Introduction

`twbparser` parses Tableau `.twb` and `.twbx` workbooks and exposes datasources, relationships, joins, fields, calculated fields, and TWBX assets. It also provides page-centric insights: dashboards, worksheets, stories, their composition, filter positions, chart types, and colors/palettes. This vignette demonstrates common use cases.

# Parse a Tableau Workbook

```{r parse-twb, eval=exists("parser")}
parser$summary
parser$overview
```

# Extracting Datasources and Parameters

```{r datasources, eval=exists("parser")}
datasources <- parser$get_datasources()
parameters <- parser$get_parameters()

print(head(datasources))
print(head(parameters))
```

# Fields and calculated fields

Parameters are excluded by default from calculated fields; opt-in via `include_parameters = TRUE`.

```{r calculated_fields, eval=exists("parser")}
head(parser$get_fields())
head(parser$get_calculated_fields(pretty = TRUE, wrap = 120))

```

# Page insights

List all pages and summarize each page

```{r insights_1, eval=ok}
twb_pages(parser)
twb_pages_summary(parser)


```

Inspect what a specific page contains

```{r insight_2, eval=ok}


pg <- twb_pages(parser)
nm <- if (nrow(pg)) pg$name[[1]] else NA_character_
if (!is.na(nm)) {
  parser$get_page_composition(nm)
}


```

Filters and their positions across dashboards

```{r insights_3, eval=ok}
twb_dashboard_filters(parser)

```

Chart (mark) types per worksheet and colors/palettes

```{r insights_4, eval=ok}
twb_charts(parser)
twb_colors(parser)

```

# Relationships and Joins

```{r relationships-joins, eval=exists("parser")}
relations <- parser$get_relationships()

head(relations)
```

# Working with TWBX Files (if applicable)

```{r twbx, eval=exists("parser") && !is.null(parser$twbx_path)}
parser$get_twbx_manifest()
parser$get_twbx_extracts()
parser$get_twbx_images()

```

# Validation of Relationships

```{r validate, eval=exists("parser")}
v <- parser$validate()
if (isTRUE(v$ok)) {
cat("Relationships validated successfully.\n")
} else {
print(v$issues)
}

```

# Summary

This vignette overviewed how to use the `twbparser` package for detailed inspection and extraction of Tableau workbook internals to assist in analysis, replication, or integration workflows.
