简体   繁体   English

R Shiny:如何将闪亮模块的无功值返回到主服务器功能?

[英]R Shiny: How can I return reactive values from a shiny module to the master server function?

I have a simple toy example that uses an add/removeBtn module to add and remove UI from "first" module. 我有一个简单的玩具示例,它使用add / removeBtn模块在“first”模块中添加和删除UI。 I need to keep track of the number of times add/remove has been clicked. 我需要跟踪单击添加/删除的次数。 If I do not use modules, it is easy, but I am trying to do this in the context of nested modules. 如果我不使用模块,这很容易,但我试图在嵌套模块的上下文中这样做。 Code is below, but basically, I cannot seem to get access to the return from the addRmBtnServer() in the main server function. 代码如下,但基本上,我似乎无法访问主服务器函数中addRmBtnServer()的返回。 I am sure it is a simple fix, but I have tried many ways around this, but cannot seem to get access to the result from my call to addRmBtnServer(). 我确信这是一个简单的修复,但我已经尝试了很多方法,但似乎无法访问我调用addRmBtnServer()的结果。 Thanks! 谢谢!

library(shiny)

firstUI <- function(id) { uiOutput(NS(id, "first")) }

firstServer <- function(input, output, session, a) {

    output$first <- renderUI({
        selectInput(session$ns("select"), h4("Select"), paste0(isolate(a()),letters[1:4]))
    })
}

removeFirstUI <- function(id) {
    removeUI(selector = paste0('#', NS(id, "first")))
}

addRmBtnUI <- function(id) {
    ns <- NS(id)

    tags$div(
    actionButton(inputId = ns('insertParamBtn'), label = "Add"),
    actionButton(ns('removeParamBtn'), label = "Remove"),
    hr(),
    tags$div(id = ns('placeholder'))
  )
}

addRmBtnServer <- function(input, output, session, moduleToReplicate, ...) {
    ns = session$ns

    params <- reactiveValues(btn = 0)

    observeEvent(input$insertParamBtn, {
        params$btn <- params$btn + 1

        callModule(moduleToReplicate$server, id = params$btn, ...)
        insertUI(
                selector = paste0('#', ns('placeholder')),
                ui = moduleToReplicate$ui(ns(params$btn))
                )
    })

    observeEvent(input$removeParamBtn, {
        moduleToReplicate$remover(ns(params$btn))
        params$btn <- params$btn - 1
    })

    return(params$btn)
}

ui <- fluidPage(
          addRmBtnUI("addRm"),
          textInput("a", label = "a", value = 1, width = '150px'),
          verbatimTextOutput("view", placeholder = TRUE)
          )

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

    pars <- callModule(
    addRmBtnServer, id = "addRm",
    moduleToReplicate = list(
      ui = firstUI,
      server = firstServer,
      remover = removeFirstUI
        ), 
    )

    output$view <- renderText({ pars() })

}

shinyApp(ui = ui, server = server)

As said in the comment, you can pass the values as return values in the corresponding server functions. 如注释中所述,您可以将值作为返回值传递到相应的服务器函数中。 There is a working example below. 下面有一个工作示例。 I left out the firstUI , firstServer and removeFirstUI implementations since they are irrelevant for your problem. 我遗漏了firstUIfirstServerremoveFirstUI实现,因为它们与您的问题无关。

library(shiny)

addRmBtnUI <- function(id) {
    ns <- NS(id)

    tags$div(
    actionButton(inputId = ns('insertParamBtn'), label = "Add"),
    actionButton(ns('removeParamBtn'), label = "Remove"),
    hr(),
    tags$div(id = ns('placeholder'))
  )
}

addRmBtnServer <- function(input, output, session, moduleToReplicate, ...) {
  ns = session$ns

  params <- reactiveValues(btn = 0)

  observeEvent(input$insertParamBtn, {
    params$btn <- params$btn + 1

    callModule(moduleToReplicate$server, id = params$btn, ...)
    insertUI(
      selector = paste0('#', ns('placeholder')),
      ui = moduleToReplicate$ui(ns(params$btn))
    )
  })

  observeEvent(input$removeParamBtn, {
    moduleToReplicate$remover(ns(params$btn))
    params$btn <- params$btn - 1
  })

  return(reactive({params$btn}))
}

ui <- fluidPage(
  addRmBtnUI("addRm"),
  verbatimTextOutput("view", placeholder = TRUE)
)

server <- function(input, output, session) {
  a <- reactive({input$a})

  pars <- callModule(
    addRmBtnServer, id = "addRm",
    moduleToReplicate = list(
      ui = function(...){},
      server = function(...){},
      remover = function(...){}
    )
  )
  output$view <- renderText({ pars() })
}

shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R Shiny 可以reactive() 函数返回多个值吗? - R shiny can reactive() function return multiple values? 如何在 R shiny 中保存以前的无功值? - How can I save previous reactive values in R shiny? R Shiny:如何通过服务器函数中的 uiOutput() 引用 renderUI 中的反应元素? - R Shiny: How can I reference reactive elements in renderUI through uiOutput() in server function? 如何在R Shiny应用程序中将变量从模块返回到服务器? - How to return a variable from a module to the server in an R Shiny app? R Shiny模块:从父服务器调用响应数据 - R shiny module: call reactive data from parent server R Shiny:将反应值传递给模块的问题 - R Shiny: Issue passing reactive values to module 如何从服务器中的反应式功能中进行选择来创建闪亮的输入 - How can I create shiny inputs with choices from a reactive function in the server 如何将图标题的非反应性值与反应性值连接起来? R闪亮 - How can I concatenate non-reactive values with reactive value for a graph title? R Shiny 如何在另一个发光模块中调用发光模块(一个模块到另一个模块的响应函数) - how to call a shiny module in another shiny module (reactive function from one module to the other) 如何在 r Shiny 中使用反应式代码创建可重复的函数? - How do I create a repeatable function with reactive code in r shiny?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM