【问题标题】:using reactive values within r shiny modules在 r 闪亮的模块中使用反应值
【发布时间】:2020-05-17 13:11:39
【问题描述】:

你好 RShiny 社区 -

我仍然有点难以思考如何在模块之间传递反应值。下面,我有三个模块以及顶级服务器和 ui 功能。 三个模块会这样做(理论上) 1)uploadTree 模块将允许用户输入和读取系统发育树文件; 2) paramsTree 模块将是多个树可视化输入,用户可以选择并在从 uploadTree 模块导入的树上更改; 3) displayTree 模块将是显示树并根据用户输入(即对齐 = T 或 F)调整可视化的模块。

但是,我仍然很难让它工作 - 最终可能是因为我仍然不清楚使模块相互通信的正确方法。就目前而言,此代码不允许树显示并给出错误'找不到函数'align''

建议....

下面是一个在 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)

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

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

【问题讨论】:

  • 嗨,我不熟悉包ggtree,但显然由于它的某些功能导致了一些错误。你有没有尝试在 Shiny 之外做你想做的事情(即以一种非反应性的方式)?如果是这样,您可以将其添加到您的帖子中吗?如果没有,我认为您应该在使用 Shiny 之前在 R Shiny 之外创建一个功能示例
  • bretauv - 添加了一个对齐提示的无光泽示例。

标签: r shiny module reactive


【解决方案1】:

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

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

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

return(treeFile)
}

【讨论】:

  • 嗯,好吧,我添加了 return(treeFile) 并发生了同样的错误。如果使用两个模块(uploadTree 和 displayTree),我可以在不使用您的 return(treeFile) 建议的情况下显示系统发育。当我尝试将反应值添加到树可视化中时发生错误。
  • 你能添加一个可重现的例子,更简单的吗?我怀疑传递响应式不是代码的(唯一)问题。
  • 调整了上面的例子,注释掉了模块中不工作的部分(paramsTree模块)。
猜你喜欢
  • 2019-09-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-15
  • 2018-03-17
  • 2018-01-10
  • 2018-07-13
  • 2021-01-10
  • 1970-01-01
相关资源
最近更新 更多