Skip to content

Commit 6dced00

Browse files
committed
Fix info/condition in dfr_browser.mallet_model
These parameters if non-default were not getting passed on to export_browser_data correctly. Also added some more in documentation on using the "base" dfr-browser metadata/bib classes, with metadata_header=T. It's fiddly and badly done but I guess it works. Also tweaked the browser_export test to expect the edited informational message (no longer a warning) when export_browser_data gets a metadata(m) that doesn't look like DfR metadata.
1 parent 3b8c085 commit 6dced00

File tree

5 files changed

+87
-24
lines changed

5 files changed

+87
-24
lines changed

R/browser_export.R

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ write_dfb_file <- function (txt, f, zip=TRUE,
110110
#' @section Metadata format:
111111
#'
112112
#' If you are working with non-JSTOR documents, the one file that will reflect
113-
#' this is the exported metadata. dfr-browser expects seven metadata columns:
113+
#' this is the exported metadata. dfr-browser expects seven metadata columns by default:
114114
#' \code{id,title,author,journaltitle,volume,issue,pubdate,pagerange}. This
115115
#' function looks for these seven columns and, if it finds them, writes the
116116
#' metadata with these columns in this order. Any remaining columns are pushed
@@ -119,9 +119,14 @@ write_dfb_file <- function (txt, f, zip=TRUE,
119119
#' then \code{export_browser_data} will simply save all the metadata as is,
120120
#' adjusting only the CSV format to match the baseline expectation of
121121
#' dfr-browser (namely, a headerless CSV conforming to
122-
#' \href{http://tools.ietf.org/html/rfc4180}{RFC 4180}.). If your metadata does
123-
#' not match these expectations, you may have to write out the metadata yourself
124-
#' and/or customize dfr-browser to get satisfactory results.
122+
#' \href{http://tools.ietf.org/html/rfc4180}{RFC 4180}.).
123+
#'
124+
#' If your metadata does not match these expectations, an alternative is to set
125+
#' dfr-browser's configuration parameters \code{VIS.metadata.type} and
126+
#' \code{VIS.bib.type} to "base" (using the \code{info} parameter) and to write
127+
#' out a metadata file \emph{with} a header by passing \code{metadata_header=T}
128+
#' to this function or \code{\link{dfr_browser}}. For polished results more
129+
#' customization of dfr-browser might be necessary.
125130
#'
126131
#' Note that you can adjust the metadata held on the model object by assigning
127132
#' to \code{metadata(m)} before exporting the browser data. In particular, if
@@ -306,15 +311,21 @@ export_browser_data <- function (m, out_dir, zipped=TRUE,
306311

307312
md_frame <- metadata(m)
308313
if (!is.null(md_frame)) {
314+
# TODO clean up
315+
#
316+
# This whole business with the metadata columns is a bad idea and needs
317+
# to be simplified both here and in dfr-brwser
318+
309319
dfb_expected <- c(
310320
"id", "title", "author", "journaltitle", "volume", "issue",
311321
"pubdate", "pagerange")
312322

313323
if (any(! dfb_expected %in% colnames(md_frame))) {
314-
warning(
315-
"Not all expected metadata columns are present. All available metadata
316-
will be written, without any column rearrangement. dfr-browser document
317-
display may not work as expected. See ?export_browser_data for details."
324+
message(
325+
"Not all expected DfR metadata columns are present. All available metadata will
326+
be written, without any column rearrangement. dfr-browser document display may
327+
not work as expected without additional configuration. See ?export_browser_data
328+
for details."
318329
)
319330
} else {
320331
# reorder columns
@@ -599,11 +610,13 @@ export_browser_info <- function (file, info, overwrite, index) {
599610
#' \href{http://agoldst.github.io/dfr-browser}{dfr-browser}, then (optionally)
600611
#' open a web browser. It is also possible to browse a list of models.
601612
#'
602-
#' If \code{browse=T}, the function attempts to start a local webserver and open a web browser pointing to the appropriate URL. This functionality requires the \code{servr} package.
613+
#' If \code{browse=T}, the function attempts to start a local webserver and
614+
#' open a web browser pointing to the appropriate URL. This functionality
615+
#' requires the \code{servr} package.
603616
#'
604617
#' For more control over the export, including the option to export data files
605618
#' only, if for example you have modified the HTML/CSS/JS of an existing
606-
#' dfr-browser, use \code{\link{export_browser_data}}.
619+
#' dfr-browser, use \code{\link{export_browser_data}} (q.v.).
607620
#'
608621
#' @section Browsing multiple models at once:
609622
#'
@@ -660,6 +673,9 @@ export_browser_info <- function (file, info, overwrite, index) {
660673
#' display only). The elements of this list should have names corresponding to
661674
#' \code{ids}, and each of the elements of its elements should have names
662675
#' corresponding to \code{condition}. If NULL, default values are used.
676+
#' @param metadata_header if TRUE (FALSE is default), the exported metadata CSV
677+
#' will have a header row (not expected by dfr-browser by default, but see the
678+
#' example below)
663679
#' @param ids character vector. If multiple models are specified in \code{m},
664680
#' the corresponding element of \code{ids} is used as a model ID in
665681
#' dfr-browser.
@@ -688,7 +704,22 @@ export_browser_info <- function (file, info, overwrite, index) {
688704
#' "stoplist.txt", n_topics=40)
689705
#' cl <- model_distances(list(m, m2), n_words=40) %>% align_topics()
690706
#' dfr_browser(m2, permute=match(cl$clusters[[1]], cl$clusters[[2]])))
691-
#'
707+
#' # or simply:
708+
#' dfr_browser(cl)
709+
#'
710+
#' # create a browser that allows you to toggle among 2 metadata variables
711+
#' dfr_browser(m, condition=c("pubdate", "journaltitle"))
712+
#'
713+
#' # create a browser for metadata in a non-DfR format; note info specification
714+
#'
715+
#' dfr_browser(m, condition="something_else",
716+
#' metadata_header=T,
717+
#' info=list(VIS=list(
718+
#' metadata=list(type="base"),
719+
#' bib=list(type="base"),
720+
#' bib_view=list(major="all", minor="raw")
721+
#' )),
722+
#' )
692723
#' }
693724
#'
694725
#' @export
@@ -714,15 +745,19 @@ dfr_browser.mallet_model <- function (m, out_dir=tempfile("dfr-browser"),
714745

715746
if (is.null(info)) {
716747
info <- getOption("dfrtopics.browser_info")
748+
}
749+
750+
if (is.null(info$VIS) || is.null(info$VIS$condition)) {
717751
info$VIS <- browser_condition_config(
718752
info=info$VIS,
719753
var=condition,
720-
data=metadata(m)[condition]
754+
data=metadata(m)[[condition]]
721755
)
722756
}
723757

724758
export_browser_data(m, out_dir, supporting_files=TRUE,
725-
internalize=internalize, ...)
759+
internalize=internalize, overwrite=overwrite,
760+
info=info, ...)
726761

727762
if (browse)
728763
browse_dfb(out_dir)

man/dfr_browser.Rd

Lines changed: 24 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/entropy.Rd

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/export_browser_data.Rd

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-browser_export.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,10 @@ test_that("metadata munging acts as expected", {
142142
md <- metadata(m)
143143
metadata(m)$journaltitle <- NULL
144144

145-
expect_warning(
145+
expect_message(
146146
export_browser_data(m, out_dir=out_dir, zipped=T,
147147
n_scaled_words=100),
148-
"Not all expected metadata columns"
148+
"Not all expected DfR metadata columns"
149149
)
150150
md_out <- read.csv(unz(file.path(out_dir, "meta.csv.zip"), "meta.csv"),
151151
header=F, as.is=T)

0 commit comments

Comments
 (0)