Skip to content

jbryer/VisualStats

Repository files navigation

VisualStats: R package for visualizing statistical tests

Authors: Jason Bryer, Ph.D. and Bruce Dudek, Ph.D.
Website: https://jbryer.github.io/VisualStats/

This package contains functions and Shiny applications designed to visualize statistical tests. The repository also contains a Quarto book. The latest version can be installed from Github using the remotes package:

remotes::install_github('jbryer/VisualStats')

Framework for creating Shiny applications

One of the primary goals of using the Quarto book website format is that Shiny applications can be embedded providing the reader the ability to interact with many of the statistical visualizations presented. The WebR project provides the framework for embedding R code within HTML pages. Ideally we would load the VisualStats package in WebR and call the functions directly. Unfortunatley, the WebR framework does not support installing R packages from source (and according to their documentation, may likely never have this feature). However, it is possible to load R scripts from source. The goal here is to maximize the ways in which Shiny applications can be deployed (using Shiny server, running locally, and embedded in Quarto documents) while minimizing the code duplication.

When developing a new Shiny application, the app should be located in the R/ directory as R package functions (i.e. include Roxygen documentation). In particular there needs to be at least three functions (note that FEATURE should be replaced with the name of the application):

#' Shiny UI
#' @return a Shiny UI object.
#' @export
FEATURE_shiny_ui <- function() {
  # Shiny UI code here. Basic siderbar panel provided
  fluidPage(
        sidebarLayout(
            sidebarPanel(
              # Shiny inputs
            ),
            mainPanel(plotOutput("plot", height = '600px'))
        )
    )
}

#' Shiny server
#' @param input Shiny input object.
#' @param output Shiny output object.
#' @param session Shiny session object.
#' @return a function with Shiny server logic.
#' @export
FEATURE_shiny_server <- function(input, output, session) {
  # Shiny server code here
  output$plot <- renderPlot({
    # Plotting code
})
}

#' Run the Shiny server
#' @param ... other parameters passed to [shiny::shinyApp]
#' @export
FEATURE_shiny <- function(...) {
    shiny::shinyApp(ui = FEATURE_shiny_ui, server = FEATURE_shiny_server, ...)
}

Note: Using shinylive to embed Shiny applications currently does not work consistently. For now Shiny applications are hosted using Shiny server and are embedded using HTML iframes.

Within the Quarto document where the Shiny application is to be embedded, add the following code in a shinylive-r code chunk. Note that we need to source the R scripts containing the relevant functions. Since we are calling these functions outside of the VisualStats R package any package dependencies need to be installed using teh webr::install command and loaded using the library command.

```{shinylive-r}
#| label: fig-shiny-FEATURE
#| viewerHeight: 600
#| standalone: true
webr::install("ggplot2")
library(ggplot2)
# Include any R scripts that have the implementation of any R functions used
source('https://raw.githubusercontent.com/jbryer/VisualStats/main/R/FEATURE_vis.R')
# Include R script defining the Shiny UI and server
source('https://raw.githubusercontent.com/jbryer/VisualStats/main/R/FEATURE_shiny.R')
shinyApp(ui = FEATURE_shiny_ui, server = FEATURE_shiny_server)
```

To deploy the application using Shiny server, create a file called app.R with the following (these are also included in the inst/shiny directory):

library(VisualStats)
shinyApp(ui = FEATURE_shiny_ui, server = FEATURE_shiny_server)

Helpful links

Code of Conduct

Please note that the VisualStats project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

About

R package for visualizing statistical tests

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published