简体   繁体   中英

Rshiny app not displaying plot, just empty placeholder

I'm trying to adapt one of the Rshiny app examples for my own use, (treesplit) but I'm struggling to incorporate my own data into the functions. I believe I have successfully incorporated my own data, but the plot itself isn't displaying. The webpage updates with the new space for the plot, but doesn't actually display it.

The functionality is as follows: Choosing yes will take you through the original app's pipeline. Choosing No will take you through my branch.

Choose "No", Then choose "Single", click on any row in the dataset, and click submit. The serverside has a hardcoded test dataset that will be used for the plot function.

I'm hoping someone can see whatever obvious thing I missed. (Pardon all of the libraries, they shouldn't be needed for the example I provided)

GLOBAL

if (!"treeio" %in% installed.packages()) {
  if (!"devtools" %in% installed.packages()) install.packages("devtools")
  devtools::install_github("GuangchuangYu/treeio")
} else if (packageVersion("treeio") < "1.5.1.2") {
  if (!"devtools" %in% installed.packages()) install.packages("devtools")
  devtools::install_github("GuangchuangYu/treeio")
}

if (!"purrr" %in% installed.packages()) install.packages("purrr")


purrr::walk(c("shiny", "shinyjs", "tidyverse", 
              "ggtree", "tidytree", "shinyalert"), ~{
                if (!.x %in% installed.packages()) install.packages(.x)
              })


suppressWarnings({
  suppressPackageStartupMessages({
    library(shiny)
    library(shinyjs)
    library(tidyverse)
    library(ggtree)
    library(tidytree)
    library(treeio)
    library(shinyalert)
    library(DBI)
    library(DT)
    library(shinycustomloader)
    library(taxize)
    library(dplyr)
    library(tidyr)
    library(data.table)
    library(RMySQL)
    library(phyloseq)
    library(ggplot2)
    library(ape)
    library(plyr)
    library(vegan)
    library(scales)
    library(grid)
    library(reshape2)
    library(promises)
    library(future)
    plan(multisession)
  })
})

UI

shinyUI(tagList(
  useShinyalert(),
  useShinyjs(),


  navbarPage(


    title = "Microbial Community Tree Visualizer",
    tabsetPanel(
      id = "mainTabset",
      tabPanel(
        title = "Explore Tree",
        class = "inputs",
        column(
          12,
          selectInput(
            inputId = "own_file",
            label = "Do you already have a tree file to upload?",
            choices = c(Choose  = '',
                        No = 'no',
                        Yes = 'yes'),
            selectize = FALSE
          ),

          conditionalPanel(
            condition = "input.own_file == 'yes'",
            selectInput(
              inputId = "file_type",
              label = "Select Tree File Type:",
              choices = c(
                "Tree" = "tree",
                "Beast" = "beast",
                "CodeML mlc" = "mlc",
                "jplace" = "jplace",
                "MrBayes" = "mrbayes",
                "NHX" = "nhx",
                "rst (CODEML/BASEML)" = "rst",
                "phylip" = "phylip",
                "RAxML" = "raxml"
              ),
              selected = "tree"
            ),
            fileInput(inputId = "upload_tree",
                      label = "Select Tree File:")
          ),

          conditionalPanel(
            condition = "input.own_file == 'no'",
            selectInput(
              inputId = "tree_type",
              label = "Would you like to view a single sample, or cluster multiple samples?",
              choices = c(
                Choose = '',
                Single = 'single',
                Multiple = 'multiple'
              ),
              selectize = FALSE
            ),

            conditionalPanel(
              condition = "input.tree_type == 'single'",
              DT::dataTableOutput("tbl1"),
              actionButton(
                "button1",
                "SUBMIT",
                style = "background-color:#221B70;
                color:#E0EB15;
                border-color:#E61029;
                border-style:double;
                border-width:4px;
                border-radius:50%;
                font-size:19px;"
              )
              )
                    )
                  ),

        uiOutput("select_node_render"),
        fluidRow(uiOutput("subtree_render")),
        fluidRow(uiOutput("subtree_render2"))
                  )
                )
            )
          ))

SERVER

shinyServer(
  function(input, output, session) {
    session$onSessionEnded(stopApp)

   output$tbl1 <- DT::renderDataTable({mtcars})
   output$tbl2 <- DT::renderDataTable({mtcars})


    #button1
    observeEvent(input$button1, {

      test_phylo1 <- read.tree(text = "(uncultured_prokaryote:50,(Trachelomonas_bernardinensis:44,(uncultured_Hydrogenothermus_sp.:40,uncultured_Verrucomicrobia_bacterium:40,(uncultured_Fibrobacter_sp.:31,((uncultured_Flexibacter_sp.:14,uncultured_Flexibacteraceae_bacterium:14,Rudanella_lutea:14)Cytophagaceae:12,uncultured_Bacteroidetes_bacterium:26,(uncultured_Flavobacteriia_bacterium:22,(uncultured_Fluviicola_sp.:17,(Saonia_flava:14,(Flavobacterium_columnare:8.1,uncultured_Flavobacterium_sp.:8.1)Flavobacterium:6.1)Flavobacteriaceae:3.2)Flavobacteriales:4.7)Flavobacteriia:3.8)Bacteroidetes:5.2)FCB_group:8.4,(Deinococcus_piscis:29,(uncultured_actinobacterium:22,(Streptomyces_polyantibioticus_SPR:8.1,Streptomyces_roseoverticillatus:8.1)Streptomyces:14,(Arthrobacter_sp._cryopeg_2:17,Cryobacterium_sp._PIC-C9:17)Micrococcales:4.7)Actinobacteria:6.6,(Enterococcus_faecalis:22,(Staphylococcus_saprophyticus:17,Bacillus_sp._VA2:17)Bacillales:4.7)Bacilli:6.6)Terrabacteria_group:11,(Antarctic_bacterium_CA1:30,(uncultured_candidate_division_GN04_bacterium:27,uncultured_Candidatus_Saccharibacteria_bacterium:27)Bacteria_candidate_phyla:3.1)unclassified_Bacteria:9.6,((uncultured_rumen_bacterium:1.8,uncultured_deep-sea_bacterium:1.8,uncultured_soil_bacterium:1.8,uncultured_marine_bacterium:1.8,uncultured_bacterium:1.8,uncultured_sulfur-oxidizing_symbiont_bacterium:1.8)environmental_samples:19,(uncultured_beta_proteobacterium:3.6,uncultured_synthetic_wastewater_bacterium_tmbr15-22:3.6)environmental_samples:17)environmental_samples:19,(uncultured_Desulfomonile_sp.:26,(Candidatus_Tremblaya_princeps:22,((uncultured_Alcaligenaceae_bacterium:14,Bordetella_sp._M1-6:14,(Alcaligenes_sp._ACS1:3.3,Alcaligenes_sp._R-3:3.3,Alcaligenes_sp._RCT1:3.3)unclassified_Alcaligenes:11)Alcaligenaceae:3.2,(Oxalobacter_formigenes:14,(Herbaspirillum_sp._P-64:8.1,Herbaspirillum_huttiense:8.1,Herbaspirillum_rubrisubalbicans:8.1,Herbaspirillum_seropedicae:8.1,Herbaspirillum_frisingense:8.1)Herbaspirillum:6.1)Oxalobacteraceae:3.2,(uncultured_Burkholderiaceae_bacterium:14,(Cupriavidus_metallidurans:0.68,Cupriavidus_metallidurans_CH34:0.68)Cupriavidus_metallidurans:14,(Caballeronia_sordidicola:8.1,Caballeronia_glathei:8.1,Caballeronia_udeis:8.1)Caballeronia:6.1,(Pandoraea_sputorum:8.1,Pandoraea_thiooxydans:8.1)Pandoraea:6.1,(Paraburkholderia_tropica:8.1,Paraburkholderia_heleia:8.1,Paraburkholderia_hospita:8.1,Paraburkholderia_fungorum:8.1,Paraburkholderia_phytofirmans:8.1,Paraburkholderia_unamae:8.1,Paraburkholderia_kururiensis:8.1,Candidatus_Paraburkholderia_kirkii:8.1,Paraburkholderia_phenazinium:8.1)Paraburkholderia:6.1,((Burkholderia_pyrrocinia:1.4,Burkholderia_multivorans:1.4,(Burkholderia_ambifaria_MC40-6:0.68,Burkholderia_ambifaria:0.68)Burkholderia_ambifaria:0.72,Burkholderia_cenocepacia:1.4,(Burkholderia_cepacia:0.68,Burkholderia_cepacia_ATCC_25416:0.68)Burkholderia_cepacia:0.72)Burkholderia_cepacia_complex:6.7,(Burkholderia_sp.:2.9,Burkholderia_sp._CAF324:2.9,Burkholderia_sp._IMER-A1-16:2.9,Burkholderia_sp._KTC-1:2.9,Burkholderia_sp._IMER-A1-18:2.9,Burkholderia_sp._IMER-A1-15:2.9,Burkholderia_sp._m35b:2.9,Burkholderia_sp._70-VN5-1W:2.9,Burkholderia_sp._TNe-862:2.9,Burkholderia_sp._Br3464:2.9,Burkholderia_sp._N1MM10:2.9,Burkholderia_sp._NF100:2.9)unclassified_Burkholderia:5.3,(uncultured_Burkholderia_sp.:3.4,Burkholderia_sp._enrichment_culture_clone_F20_07a_B:3.4)environmental_samples:4.7,Burkholderia_plantarii:8.1,Burkholderia_gladioli:8.1,((Burkholderia_oklahomensis_EO147:0.68,Burkholderia_oklahomensis_C6786:0.68)Burkholderia_oklahomensis:0.72,(Burkholderia_mallei:0.68,Burkholderia_mallei_ATCC_23344:0.68)Burkholderia_mallei:0.72,(Burkholderia_thailandensis:0.68,Burkholderia_thailandensis_E264:0.68)Burkholderia_thailandensis:0.72,(Burkholderia_pseudomallei_305:0.68,Burkholderia_pseudomallei:0.68)Burkholderia_pseudomallei:0.72)pseudomallei_group:6.7)Burkholderia:6.1,(Ralstonia_solanacearum:8.1,uncultured_Ralstonia_sp.:8.1,(Ralstonia_sp._EF38:3.1,Ralstonia_sp._IFA3:3.1,Ralstonia_sp._DUT_AHX:3.1)unclassified_Ralstonia:5.1)Ralstonia:6.1)Burkholderiaceae:3.2,uncultured_Burkholderiales_bacterium:17,(Rivibacter_subsaxonicus:8.8,(Aquabacterium_sp._P-130:8.1,uncultured_Aquabacterium_sp.:8.1)Aquabacterium:0.63,Methylibium_sp._BAC115:8.8,Xylophilus_ampelinus:8.8,(uncultured_Leptothrix_sp.:8.1,Leptothrix_discophora:8.1)Leptothrix:0.63)Burkholderiales_Genera_incertae_sedis:8.7,((Comamonas_kerstersii:8.1,Comamonas_sp._46:8.1,Comamonas_aquatica:8.1)Comamonas:6.1,Brachymonas_denitrificans:14,Pelomonas_saccharophila:14,Hylemonella_sp._WQH1:14,Pseudomonas_sp._P51:14,(Verminephrobacter_eiseniae:0.68,Verminephrobacter_eiseniae_EF01-2:0.68)Verminephrobacter_eiseniae:14,Diaphorobacter_nitroreducens:14,(Polaromonas_sp._tsz24:1.9,Polaromonas_sp._01WB02.3-26:1.9)unclassified_Polaromonas:12,uncultured_Comamonadaceae_bacterium:14,(Acidovorax_valerianellae:8.1,uncultured_Acidovorax_sp.:8.1,Acidovorax_defluvii:8.1)Acidovorax:6.1,Delftia_acidovorans:14,Rhodoferax_antarcticus_ANT.BR:14,(Variovorax_paradoxus:8.1,Variovorax_soli_NBRC_106424:8.1)Variovorax:6.1)Comamonadaceae:3.2)Burkholderiales:4.7)Betaproteobacteria:3.8,Acidithiobacillus_ferrooxidans:26,(Halomonas_shengliensis:22,(Pararheinheimera_chironomi:8.1,Pararheinheimera_texasensis:8.1)Pararheinheimera:14,(Azotobacter_vinelandii:14,(Pseudomonas_savastanoi_pv._nerii:8.1,Pseudomonas_sp._6C_10:8.1,Pseudomonas_frederiksbergensis:8.1,Pseudomonas_putida:8.1,(Pseudomonas_fluorescens:1.4,Pseudomonas_mandelii:1.4,Pseudomonas_migulae:1.4)Pseudomonas_fluorescens_group:6.7)Pseudomonas:6.1)Pseudomonadaceae:7.9,(Klebsiella_aerogenes:17,Serratia_marcescens:17)Enterobacterales:4.7)Gammaproteobacteria:3.8,((Paracoccus_sp._T231:14,Phaeobacter_sp._M3-1.1:14,Roseovarius_sp._S6V:14,Rhodobacter_gluconicum:14)Rhodobacteraceae:7.9,(uncultured_Sphingomonas_sp.:8.1,(Sphingomonas_sp._N2:3.7,Sphingomonas_sp._Ant20:3.7)unclassified_Sphingomonas:4.4)Sphingomonas:14,(Rhodocista_sp._GP-7:14,Azospirillum_sp._DA2-3-1:14)Rhodospirillaceae:7.9,uncultured_Alphaproteobacteria_bacterium:22,((Afipia_broomeae:14,uncultured_Bradyrhizobium_sp.:14,Rhodopseudomonas_palustris:14)Bradyrhizobiaceae:3.2,(Candidatus_Liberibacter_africanus:14,(Rhizobium_multihospitium:8.1,Rhizobium_sp._SPC_RN2:8.1,Rhizobium_etli:8.1)Rhizobium:6.1)Rhizobiaceae:3.2,((Methylorubrum_thiocyanatum:8.1,(Methylorubrum_populi_BJ001:0.68,Methylorubrum_populi:0.68)Methylorubrum_populi:7.5)Methylorubrum:6.1,(Methylobacterium_oryzae:8.1,Methylobacterium_gregans:8.1,uncultured_Methylobacterium_sp.:8.1,Methylobacterium_fujisawaense:8.1,Methylobacterium_sp._ST4.9:8.1)Methylobacterium:6.1)Methylobacteriaceae:3.2)Rhizobiales:4.7)Alphaproteobacteria:3.8)Proteobacteria:14)Bacteria:4.2)cellular_organisms:6.3)NA;")

      str(test_phylo1)


      tree_df <- reactive({
        output <- test_phylo1 %>% 
          as_data_frame()
      })


      output$select_node_render <- renderUI({
        output <- tagList(
          fluidRow(
            column(
              12,
              selectizeInput(
                inputId = "select_node",
                label = "Select Node:",
                choices = tree_df() %>% 
                  select(label) %>% 
                  arrange(label) %>% 
                  pull(label),
                width = "100%"
              )
            )
          ),
          fluidRow(
            column(
              3, 
              numericInput(
                inputId = "subtree_levels_back",
                label = "Select Number of Ancestral Levels:",
                min = 1,
                value = 10
              )
            ), 
            column(
              3,
              numericInput(
                inputId = "subtree_text_size",
                label = "Select label text size:",
                min = 2,
                value = 3
              )
            ),
            column(
              3,
              numericInput(
                inputId = "subtree_plot_height",
                label = "Select plot height",
                value = 1200
              )
            ),
            column(
              3, 
              numericInput(
                inputId = "subtree_width_multiply",
                label = "Select plot width multiplier:",
                value = 1.4,
                min = 1,
                step = 0.1
              )
            )
          )


        )


        return(output)
      })

      # creating the subtree
      output$subtree <- renderPlot({
        req(input$select_node, tree(), 
            input$subtree_width_multiply, 
            input$subtree_text_size,
            input$subtree_plot_height)

        # getting the subtree phylo or treedata object
        sub_tree <- tree_subset(test_phylo1, node = input$select_node,
                                levels_back = input$subtree_levels_back)

        # extracting the tip labels from the sub tree
        if (isS4(sub_tree)) {
          labels <- sub_tree@phylo$tip.label
        } else {
          labels <- sub_tree$tip.label
        }

        # doing some basic manipulation on labels 
        # this will only really work for labels of the format
        # ;k__;p__;c__;o__;f__;g__;s__
        labels_df <- tibble(
          label = labels,
          genus = str_extract(label, "[^;]+;[^;]+$") %>% str_replace(";[^;]+$", ""),
          species = str_extract(label, "[^;]+$")
        )  %>% 
          mutate(
            species = if_else(is.na(genus), "", str_replace(species, "s__", "")),
            genus = if_else(is.na(genus), label, str_replace(genus, "g__", ""))
          )

        # creating the plot
        p <- sub_tree %>% 
          ggtree(aes(color = group))  %<+% labels_df +
          geom_tiplab(aes(label = paste(genus, species)), 
                      size = input$subtree_text_size) +
          theme_tree2() +
          scale_color_manual(values = c(`1` = "red", `0` = "black"))

        p + lims(x = c(0, max(p$data$x) * input$subtree_width_multiply))
      })

      # creating the ui element for the subtree 
      output$subtree_render2 <- renderUI({
        req(input$subtree_plot_height)
        plotOutput("subtree", height = input$subtree_plot_height)
      })


    })


    #button2
    observeEvent(input$button2, {
      output$x5 = renderPrint({
        validate(need(
          length(input$tbl2_rows_selected) > 1,
          "Please choose two or more samples."
        ))
        cat('\n\nSelected rows:\n\n')
        cat(input$tbl2_rows_selected, sep = ', ')
      })
    })

    # reactive values are used to reset the file input when the 
    # file type is changed. This is done to prevent errors from
    # popping up before a new file can be uploaded.
    rv <- reactiveValues(
      data = NULL,
      clear = FALSE
    )

    observeEvent(input$upload_tree, {
      rv$clear <- FALSE
    }, priority = 1000)

    observeEvent(input$file_type, {
      rv$data <- NULL
      rv$clear <- TRUE
      reset('upload_tree')
    }, priority = 1000)

    # This reactive value reads in the tree object using one of the
    # treeio import functions. If the function called, based on input$file_type
    # fails, NULL is returned
    tree <- reactive({
      req(input$upload_tree, input$file_type,
          input$upload_tree, !rv$clear)

      file <- input$upload_tree$datapath

      output <- switch(
        input$file_type,
        tree = possibly(read.tree, otherwise = NULL)(file),
        beast = possibly(read.beast, otherwise = NULL)(file),
        # codeml = possibly(read.beast, otherwise = NULL)(file),
        mlc = possibly(read.codeml_mlc, otherwise = NULL)(file),
        # hyphy = possibly(read.hyphy, otherwise = NULL)(file),
        jplace = possibly(read.jplace, otherwise = NULL)(file),
        mrbayes = possibly(read.mrbayes, otherwise = NULL)(file),
        nhx = possibly(read.nhx, otherwise = NULL)(file),
        rst = possibly(read.paml_rst, otherwise = NULL)(file),
        phylip = possibly(read.phylip, otherwise = NULL)(file),
        r8s = possibly(read.r8s, otherwise = NULL)(file),
        raxml = possibly(read.raxml, otherwise = NULL)(file)

      )

      # read.tree(input$upload_tree$datapath)

      return(output)
    })


    # This tree_df function 
    tree_df <- reactive({
      req(tree())
      output <- tree() %>% 
        as_data_frame()
    })

    observe({
      req(input$upload_tree)

      if (is.null(tree())) {
        shinyalert("Tree import error", paste("There was an error when trying to read your tree!",
                                              "Did you select the correct file format?"),
                   type = "error")
      }
    })


    # Rendering the ui elements to select the node to subset, 
    # how far back to subset, and tree options (text size, height, width)
    output$select_node_render <- renderUI({
      req(input$upload_tree, tree())
      output <- tagList(
        fluidRow(
          column(
            12,
            selectizeInput(
              inputId = "select_node",
              label = "Select Node:",
              choices = tree_df() %>% 
                select(label) %>% 
                arrange(label) %>% 
                pull(label),
              width = "100%"
            )
          )
        ),
        fluidRow(
          column(
            3, 
            numericInput(
              inputId = "subtree_levels_back",
              label = "Select Number of Ancestral Levels:",
              min = 1,
              value = 10
            )
          ), 
          column(
            3,
            numericInput(
              inputId = "subtree_text_size",
              label = "Select label text size:",
              min = 2,
              value = 3
            )
          ),
          column(
            3,
            numericInput(
              inputId = "subtree_plot_height",
              label = "Select plot height",
              value = 1200
            )
          ),
          column(
            3, 
            numericInput(
              inputId = "subtree_width_multiply",
              label = "Select plot width multiplier:",
              value = 1.4,
              min = 1,
              step = 0.1
            )
          )
        )


      )


      return(output)
    })

    # creating the subtree
    output$subtree <- renderPlot({
      req(input$select_node, tree(), 
          input$subtree_width_multiply, 
          input$subtree_text_size,
          input$subtree_plot_height)

      # getting the subtree phylo or treedata object
      sub_tree <- tree_subset(tree(), node = input$select_node,
                              levels_back = input$subtree_levels_back)

      # extracting the tip labels from the sub tree
      if (isS4(sub_tree)) {
        labels <- sub_tree@phylo$tip.label
      } else {
        labels <- sub_tree$tip.label
      }

      # doing some basic manipulation on labels 
      # this will only really work for labels of the format
      # ;k__;p__;c__;o__;f__;g__;s__
      labels_df <- tibble(
        label = labels,
        genus = str_extract(label, "[^;]+;[^;]+$") %>% str_replace(";[^;]+$", ""),
        species = str_extract(label, "[^;]+$")
      )  %>% 
        mutate(
          species = if_else(is.na(genus), "", str_replace(species, "s__", "")),
          genus = if_else(is.na(genus), label, str_replace(genus, "g__", ""))
        )

      # creating the plot
      p <- sub_tree %>% 
        ggtree(aes(color = group))  %<+% labels_df +
        geom_tiplab(aes(label = paste(genus, species)), 
                    size = input$subtree_text_size) +
        theme_tree2() +
        scale_color_manual(values = c(`1` = "red", `0` = "black"))

      p + lims(x = c(0, max(p$data$x) * input$subtree_width_multiply))
    })

    # creating the ui element for the subtree 
    output$subtree_render <- renderUI({
      req(input$subtree_plot_height,tree())
      plotOutput("subtree", height = input$subtree_plot_height)
    })


  }
)

Found it! I had missed one dependencey in this block of the server code:

# creating the subtree
      output$subtree <- renderPlot({
        req(input$select_node, **tree()**, 
            input$subtree_width_multiply, 
            input$subtree_text_size,
            input$subtree_plot_height)

Removing "tree()" fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM