首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在r个闪亮模块中使用反应值

在r个闪亮模块中使用反应值
EN

Stack Overflow用户
提问于 2020-02-01 04:57:21
回答 1查看 222关注 0票数 1

你好,RShiny社区-

对于如何在模块之间传递反应值,我仍然有一点困难。下面,我有三个模块以及顶层服务器和ui函数。这三个模块(理论上)将完成以下工作: 1) uploadTree模块将允许用户输入和读取系统发育树文件;2) paramsTree模块将是用户可以选择的多个树可视化输入,并将在从uploadTree模块导入的树上进行更改;3) displayTree模块将是显示树并基于用户输入(即align =T或F)调整可视化的模块。

然而,我仍然有一段困难的时间让它工作-最终可能是因为我仍然不清楚让模块相互对话的正确方式。按照原样,这段代码不允许显示树,并给出错误“找不到函数”align“”

建议...

下面是一个在align =T中硬编码的例子:

代码语言:javascript
复制
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)

根据bretauv的建议,这里是读取系统发展史并绘制系统发展图的代码,提示对齐。

代码语言:javascript
复制
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

如果需要,系统发生树位于here

EN

回答 1

Stack Overflow用户

发布于 2020-02-02 18:59:20

每个服务器函数都必须返回一个reactive (或reactiveValuesreactiveVal),例如:

代码语言:javascript
复制
mod_uploadTree_server <- function(input, output, session){
  ns <- session$ns

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

return(treeFile)
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60011116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档