% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/audit-export.R
\name{write_trail}
\alias{write_trail}
\title{Write an Audit Trail to a File}
\usage{
write_trail(.trail, file, format = c("rds", "json"))
}
\arguments{
\item{.trail}{An \code{\link[=audit_trail]{audit_trail()}} object.}

\item{file}{Path to the output file. A \code{.rds} extension is conventional
for \code{format = "rds"}; \code{.json} for \code{format = "json"}.}

\item{format}{One of \code{"rds"} (default) or \code{"json"}. The JSON format
requires the \pkg{jsonlite} package to be installed.}
}
\value{
\code{.trail}, invisibly.
}
\description{
Saves an \code{\link[=audit_trail]{audit_trail()}} to disk as either an RDS file (default) or a
JSON file. The RDS format preserves all R types and can be restored
perfectly with \code{\link[=read_trail]{read_trail()}}. The JSON format produces a human-readable
representation suitable for archiving or interoperability with other tools.
}
\note{
Custom diagnostic results (the \code{custom} field, populated via \code{.fns}
in \code{\link[=audit_tap]{audit_tap()}}) are serialised on a best-effort basis for JSON output.
Complex R objects such as environments or functions cannot be represented
in JSON and will cause an error.
}
\examples{
trail <- audit_trail("example")
mtcars |> audit_tap(trail, "raw")
tmp <- tempfile(fileext = ".rds")
write_trail(trail, tmp)
restored <- read_trail(tmp)

}
\seealso{
\code{\link[=read_trail]{read_trail()}}, \code{\link[=trail_to_list]{trail_to_list()}}

Other audit export:
\code{\link[=audit_export]{audit_export()}},
\code{\link[=read_trail]{read_trail()}},
\code{\link[=trail_to_df]{trail_to_df()}},
\code{\link[=trail_to_list]{trail_to_list()}}
}
\concept{audit export}
