简体   繁体   中英

using reactive values within r shiny modules

Hello RShiny community -

I am still having a bit of a hard time wrapping my mind around how to pass reactive values between modules. Below, I have three modules along with the top level server and ui functions. The three modules would do this (in theory) 1) uploadTree module would allow the user to input and read phylogenetic tree file; 2) paramsTree module would be multiple tree visualization inputs that the user could select and would change on the imported tree from the uploadTree module; 3) displayTree module would be the module which displays the tree and adjusts visualization based on the user input (ie align = T or F).

However, I am still having a difficult time in getting this to work - ultimately probably because I am still unclear of the correct way to make modules talk to each other. As it stands, this code does not allow the tree to display and gives the error 'could not find function 'align''

Suggestions....

Below is an example which hard codes in the align = T:

library(shiny)
library(ggtree)

#top-level ui
ui <- 
    fluidPage(
      h1("phylogeny"),
      sidebarPanel(
        mod_uploadTree_ui("uploadTree_ui_1")),
        #mod_paramsTree_ui("paramsTree_ui_1")), #commented out as this is not working 
      mainPanel(mod_displayTree_ui("displayTree_ui_1"))
    )

#top-level server
server <- function(input, output,session) {

  treeDisplay <- callModule(mod_uploadTree_server, "uploadTree_ui_1")
  #params <- callModule(mod_paramsTree_server, "paramsTree_ui_1") #commented out as this is not working. 
  callModule(mod_displayTree_server, "displayTree_ui_1", treeDisplay)
}

#uploadTree module - This module will use the read.newick function to read in a phylogenetic tree
mod_uploadTree_ui <- function(id){
  ns <- NS(id)
  tagList(
    fileInput(ns("treefile"), label="Upload a newick file, please"))
}


mod_uploadTree_server <- function(input, output, session){
  ns <- session$ns

  treeFile <- reactive({
    req(input$treefile)
    treeio::read.newick(input$treefile$datapath)
  })
}

  #paramsTree module - This module *should* include multiple tree display parameters that can be selected by the user. Here, only one parameter is included for a smaller example. But I am unable to add this without an error occurring. 
  mod_paramsTree_ui <- function(id){
    ns <- NS(id)
    tagList(
      checkboxInput(ns("alignTips"), "Align tip labels", TRUE)
    )
  }

  mod_paramsTree_server <- function(input, output, session){
    ns <- session$ns

    observe({
      align = reactive(input$alignTips)
    })
  }

  #displayTree module - This module will plot the tree and will change the tree viz based on user inputs from param module 
  mod_displayTree_ui <- function(id, label = "Display Tree"){
    ns <- NS(id)
    tagList(
      label,
      plotOutput(ns("treeDisplay"))
    )
  }

  mod_displayTree_server <- function(input, output, session, treeFile, align){
    ns <- session$ns

    make_tree <- reactive({
      ggplot2::ggplot(treeFile()) + ggtree::geom_tree() + ggtree::geom_tiplab(align = T) #hardcoded align = T instead of based on user input
    })

    output$treeDisplay <- renderPlot({
      make_tree()
    })
  }

shinyApp(ui, server)

per the suggestion of bretauv, here is the code to read a phylogeny and plot the phylogeny with the tips aligned.

library(ggplot2)
library(treeio)
library(ggtree)

tree <- treeio::read.newick("1509MLJF6-1.dnd")
make_tree <- ggplot2::ggplot(tree) + ggtree::geom_tree() + ggtree::geom_tiplab(align = T)
make_tree

对齐提示的屏幕截图

If needed, a phylogenetic tree is located here

Each of your server functions must return a reactive (or reactiveValues or reactiveVal ), for example:

mod_uploadTree_server <- function(input, output, session){
  ns <- session$ns

  treeFile <- reactive({
    req(input$treefile)
    treeio::read.newick(input$treefile$datapath)
  })

return(treeFile)
}

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