diff --git a/R/inspect_language.R b/R/inspect_language.R index b89a135..5766c9f 100644 --- a/R/inspect_language.R +++ b/R/inspect_language.R @@ -3,18 +3,20 @@ #' @param data_conv conversation dataset #' @param data_tokens tokens dataset #' @param lang language +#' @param returnplots boolean indicating whether plots should be made #' @param saveplot should the plot be saved #' @param allsources all sources #' #' @export inspect_language <- function(data_conv, - data_tokens, - lang, + data_tokens = NULL, + lang = NULL, + returnplots = FALSE, saveplot=FALSE, allsources=FALSE) { # conversation data dp <- data_conv |> - dplyr::filter(language == lang) + dplyr::filter(.data$language == lang) nturns <- sum(!is.na(dp$FTO)) # QUESTION: does this make sense? @@ -23,34 +25,60 @@ inspect_language <- function(data_conv, pC <- plot_turn_duration(data=dp) pD <- plot_top_turn_types(data=dp) - # token data - dt <- data_tokens |> - dplyr::filter(language==lang) - nwords <- dt$total[1] - pE <- plot_token_rank(data=dt, nwords) - # combine the plots - top_row <- cowplot::plot_grid(pA,pB,pC,labels=c("A","B","C"),rel_widths = c(1,1,1),nrow=1) - bottom_row <- cowplot::plot_grid(pD,pE,labels=c("D","E"),rel_widths = c(1,1),nrow=1) - panel <- cowplot::plot_grid(top_row,bottom_row,ncol=1) - print(panel) + if(!is.null(data_tokens)){ + # token data + dt <- data_tokens |> + dplyr::filter(.data$language==lang) + nwords <- dt$total[1] + pE <- plot_token_rank(data=dt, nwords) + } - if(saveplot) { - filename <- paste0('qc-panel-',lang,'.png') - ggplot2::ggsave(filename,bg="white",width=2400,height=1200,units="px") + + # generate plots + if(returnplots){ + if(!is.null(data_tokens)){ + generate_plots(saveplot, pA, pB, pC, pD, pE) + } else { + generate_plots(saveplot, pA, pB, pC, pD, pE = NULL) + } } # sample conversation data_convplot <- prepare_convplot(data_conv, lang) - pconv <- plot_conversation(data_convplot) - print(pconv) + + if(returnplots){ + pconv <- plot_conversation(data_convplot) + print(pconv) + + } # print summary stats report_summaries(data_conv, lang, allsources) } +generate_plots <- function(saveplot = FALSE, pA, pB, pC, pD, pE){ + # combine the plots + + if(is.null(pE)){ + top_row <- cowplot::plot_grid(pA,pB,labels=c("A","B"),rel_widths = c(1,1),nrow=1) + bottom_row <- cowplot::plot_grid(pC,pD,labels=c("C", "D"),rel_widths = c(1,1),nrow=1) + } else{ + top_row <- cowplot::plot_grid(pA,pB,pC,labels=c("A","B","C"),rel_widths = c(1,1,1),nrow=1) + bottom_row <- cowplot::plot_grid(pD,pE,labels=c("D","E"),rel_widths = c(1,1),nrow=1) + } + + panel <- cowplot::plot_grid(top_row,bottom_row,ncol=1) + print(panel) + + if(saveplot) { + filename <- paste0('qc-panel-',lang,'.png') + ggplot2::ggsave(filename,bg="white",width=2400,height=1200,units="px") + } +} + diff --git a/man/inspect_language.Rd b/man/inspect_language.Rd index 58aed64..921e928 100644 --- a/man/inspect_language.Rd +++ b/man/inspect_language.Rd @@ -6,8 +6,9 @@ \usage{ inspect_language( data_conv, - data_tokens, - lang, + data_tokens = NULL, + lang = NULL, + returnplots = FALSE, saveplot = FALSE, allsources = FALSE ) @@ -19,6 +20,8 @@ inspect_language( \item{lang}{language} +\item{returnplots}{boolean indicating whether plots should be made} + \item{saveplot}{should the plot be saved} \item{allsources}{all sources}