4. Creating an Interactive Line Chart with shiny.gosling

In this tutorial, we’ll explore how to create an interactive line chart visualization of genomic data using the shiny and shiny.gosling packages in R. This visualization will enable users to explore trends and patterns within genomic datasets.

Introduction

The shiny and shiny.gosling packages offer powerful tools for building interactive data visualizations within Shiny apps. In this tutorial, we’ll demonstrate the process of constructing an interactive line chart visualization using genomic data. This example leverages the shiny package for user interface design and the shiny.gosling package for generating the visualizations.

Understanding Multivec Data

Multivec is a file format introduced by Higlass suitable for visualizing multi-dimensional numerical data across genomic coordinates. It’s widely utilized for representing various genomic experiments such as ChIP-seq, ATAC-seq, and Hi-C, where measurements are taken at different genomic positions.

Multivec data is essentially a matrix where rows correspond to genomic positions or regions, and columns correspond to different samples or experiments. Each matrix entry represents a value associated with a specific genomic position and sample. The genomic positions are typically represented as chromosomal coordinates (chromosome name and base pair position), allowing accurate alignment with the genome. Tools like the UCSC Genome Browser, IGV, and libraries like shiny.gosling can visualize multivec data effectively.

To delve deeper into multivec data and its application in genomics, explore these resources:

UCSC Genome Browser: A versatile tool for visualizing genomic data, including multivec data. Learn how to visualize multivec data in the UCSC Genome Browser.

Integrative Genomics Viewer (IGV): Another popular genome visualization tool supporting multivec data. Discover how to load and visualize multivec data in IGV.

BedGraph and BigWig Formats: These common formats are used to represent multivec data. Explore explanations of the BedGraph and BigWig formats.

Call library libraries.

library(shiny)
library(shiny.gosling)

Creating the Data Object

We’ll begin by fetching the multivec data using the track_data function. This function retrieves data from a specified URL, where the data is organized as a matrix with rows representing samples and columns representing positions. We’ll define the row, column, and value attributes along with the sample categories. The categories parameter should contain a list of category names (e.g., “sample 1”).


# Create data object ----
view1_data <- track_data(
  url = "https://resgen.io/api/v1/tileset_info/?d=UvVPeLHuRDiYA3qwFlm7xQ",
  type = "multivec",
  row = "sample",
  column = "position",
  value = "peak",
  categories = list("sample 1")
)

Constructing the Line Chart Track

To visualize the data, we’ll define the visual channels for the line chart. We’ll specify that the x-axis should represent genomic positions and the y-axis should represent peak values. The add_single_track function creates a line chart visualization using these visual channels. Additionally, we’ll set the width and height of the plot and specify the data source.


# Create visual channels ----
view1_x <- visual_channel_x(
  field = "position", type = "genomic", axis = "bottom"
)

view1_y <- visual_channel_y(
  field = "peak", type = "quantitative", axis = "right"
)

# Create single track ----
single_track <- add_single_track(
  width = 800,
  height = 180,
  data = view1_data,
  mark = "line",
  x = view1_x,
  y = view1_y,
  size = visual_channel_size(
    value = 2
  )
)

Composing and Arranging the View

The compose_view function is used to create a composed view that contains the single line chart track. The layout of the view is specified as “linear”.

The arrange_views function arranges the composed view created earlier. The title and subtitle for the arrangement are specified. The arranged view is named single_composed_views.


# Compose the track ----
single_composed_view <- compose_view(
  tracks = single_track,
  layout = "linear"
)

# Arrange the view above ----
single_composed_views <- arrange_views(
  title = "Basic Marks: line",
  subtitle = "This is a simple line chart.",
  views = single_composed_view
)

Shiny App ui

The navbarPage function creates a navigation bar at the top of the user interface. Within the navigation bar, there is a single tab panel named “Line Chart.” Inside the tab panel, the use_gosling function is used to incorporate the shiny.gosling package. The fluidPage function is used to create a fluid layout within the tab panel. Two columns are defined: one for the goslingOutput and another for action buttons.


ui <- navbarPage(
  title = "shiny.gosling",
  tabPanel(
    "Line Chart",
    use_gosling(),
    fluidPage(
      width = 12,
      fluidRow(
        column(
          width = 8,
          goslingOutput("gosling_plot_test")
        ),
        column(
          width = 4,
          fluidRow(
            column(
              2,
              actionButton(
                "download_png",
                "PNG",
                icon = icon("cloud-arrow-down")
              )
            ),
            column(
              2,
              actionButton(
                "download_pdf",
                "PDF",
                icon = icon("cloud-arrow-down")
              )
            )
          )
        )
      )
    )
  )
)

Shiny App server

The server logic is defined using the server function. The observeEvent functions are used to respond to button clicks and user interactions. Clicking the “PNG” or “PDF” buttons triggers the respective download action for the visualization. The output$gosling_plot_test function uses renderGosling to render the composed view using the gosling function from the shiny.gosling package.


server <- function(input, output, session) {

  observeEvent(input$download_png, {
    export_png(component_id = "sars_cov2")
  })

  observeEvent(input$download_pdf, {
    export_pdf(component_id = "sars_cov2")
  })

  observeEvent(input$zoom_out, {
    zoom_to_extent(
      component_id = "sars_cov2",
      view_id = "view2_track1"
    )
  })

  output$gosling_plot_test <- renderGosling({
    gosling(
      component_id = "sars_cov2",
      single_composed_views,
      clean_braces = TRUE
    )
  })
}

shinyApp(ui, server)

Session Info


sessionInfo()
#> R version 4.4.2 (2024-10-31)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Ubuntu 24.04.1 LTS
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3 
#> LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/libopenblasp-r0.3.26.so;  LAPACK version 3.12.0
#> 
#> locale:
#>  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=C              
#>  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
#>  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Etc/UTC
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats4    stats     graphics  grDevices utils     datasets  methods  
#> [8] base     
#> 
#> other attached packages:
#>  [1] sessioninfo_1.2.2                  ggbio_1.55.0                      
#>  [3] ggplot2_3.5.1                      StructuralVariantAnnotation_1.23.0
#>  [5] VariantAnnotation_1.53.0           Rsamtools_2.23.0                  
#>  [7] Biostrings_2.75.1                  XVector_0.47.0                    
#>  [9] SummarizedExperiment_1.37.0        Biobase_2.67.0                    
#> [11] MatrixGenerics_1.19.0              matrixStats_1.4.1                 
#> [13] rtracklayer_1.67.0                 GenomicRanges_1.59.1              
#> [15] GenomeInfoDb_1.43.1                IRanges_2.41.1                    
#> [17] S4Vectors_0.45.2                   BiocGenerics_0.53.3               
#> [19] generics_0.1.3                     shiny_1.9.1                       
#> [21] shiny.gosling_1.3.0                rmarkdown_2.29                    
#> 
#> loaded via a namespace (and not attached):
#>   [1] RColorBrewer_1.1-3       sys_3.4.3                rstudioapi_0.17.1       
#>   [4] jsonlite_1.8.9           magrittr_2.0.3           GenomicFeatures_1.59.1  
#>   [7] fs_1.6.5                 BiocIO_1.17.0            zlibbioc_1.52.0         
#>  [10] vctrs_0.6.5              memoise_2.0.1            RCurl_1.98-1.16         
#>  [13] base64enc_0.1-3          progress_1.2.3           htmltools_0.5.8.1       
#>  [16] S4Arrays_1.7.1           curl_6.0.1               SparseArray_1.7.2       
#>  [19] Formula_1.2-5            sass_0.4.9               bslib_0.8.0             
#>  [22] fontawesome_0.5.3        htmlwidgets_1.6.4        httr2_1.0.6             
#>  [25] plyr_1.8.9               cachem_1.1.0             buildtools_1.0.0        
#>  [28] GenomicAlignments_1.43.0 shiny.react_0.4.0        mime_0.12               
#>  [31] lifecycle_1.0.4          pkgconfig_2.0.3          Matrix_1.7-1            
#>  [34] R6_2.5.1                 fastmap_1.2.0            GenomeInfoDbData_1.2.13 
#>  [37] digest_0.6.37            colorspace_2.1-1         GGally_2.2.1            
#>  [40] AnnotationDbi_1.69.0     OrganismDbi_1.49.0       Hmisc_5.2-0             
#>  [43] RSQLite_2.3.8            filelock_1.0.3           fansi_1.0.6             
#>  [46] httr_1.4.7               abind_1.4-8              compiler_4.4.2          
#>  [49] bit64_4.5.2              withr_3.0.2              htmlTable_2.4.3         
#>  [52] backports_1.5.0          BiocParallel_1.41.0      DBI_1.2.3               
#>  [55] ggstats_0.7.0            biomaRt_2.63.0           rappdirs_0.3.3          
#>  [58] DelayedArray_0.33.2      rjson_0.2.23             tools_4.4.2             
#>  [61] foreign_0.8-87           httpuv_1.6.15            nnet_7.3-19             
#>  [64] glue_1.8.0               restfulr_0.0.15          promises_1.3.0          
#>  [67] grid_4.4.2               checkmate_2.3.2          cluster_2.1.6           
#>  [70] reshape2_1.4.4           gtable_0.3.6             BSgenome_1.75.0         
#>  [73] tidyr_1.3.1              ensembldb_2.31.0         hms_1.1.3               
#>  [76] data.table_1.16.2        xml2_1.3.6               utf8_1.2.4              
#>  [79] pillar_1.9.0             stringr_1.5.1            later_1.3.2             
#>  [82] dplyr_1.1.4              BiocFileCache_2.15.0     lattice_0.22-6          
#>  [85] bit_4.5.0                biovizBase_1.55.0        RBGL_1.83.0             
#>  [88] tidyselect_1.2.1         maketools_1.3.1          knitr_1.49              
#>  [91] gridExtra_2.3            ProtGenerics_1.39.0      xfun_0.49               
#>  [94] stringi_1.8.4            UCSC.utils_1.3.0         lazyeval_0.2.2          
#>  [97] yaml_2.3.10              evaluate_1.0.1           codetools_0.2-20        
#> [100] tibble_3.2.1             graph_1.85.0             BiocManager_1.30.25     
#> [103] cli_3.6.3                rpart_4.1.23             xtable_1.8-4            
#> [106] munsell_0.5.1            jquerylib_0.1.4          dichromat_2.0-0.1       
#> [109] Rcpp_1.0.13-1            dbplyr_2.5.0             png_0.1-8               
#> [112] XML_3.99-0.17            parallel_4.4.2           assertthat_0.2.1        
#> [115] blob_1.2.4               prettyunits_1.2.0        AnnotationFilter_1.31.0 
#> [118] bitops_1.0-9             txdbmaker_1.3.0          pwalign_1.3.0           
#> [121] scales_1.3.0             purrr_1.0.2              crayon_1.5.3            
#> [124] rlang_1.1.4              KEGGREST_1.47.0