如何基于无功输入调用闪亮模块? [英] How to call shiny module based on reactive input?

查看:96
本文介绍了如何基于无功输入调用闪亮模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于反应式输入来调用嵌套模块服务器,并在主面板上显示返回输出.取决于响应输入,函数callModule在这里似乎不起作用.

I would like to call a nested module server based on a reactive input and display the return output on the main panel. The function callModule doesn't seem to work here when it is depending on a reactive input.

在您构建复杂的应用程序时,我认为这是常见的情况,但是我一直在努力寻找解决方案.

I assume this to be a common scenario when you’re building a complex app but I’ve been struggling to find a solution.

下面是我的代码,或者您可以通过在R中输入以下命令来运行该应用程序:

Below is my code or you can run the app by entering the following command in R:

shiny :: runGist("23abbb50a1df6a91af990a7401919044")

shiny::runGist("23abbb50a1df6a91af990a7401919044")

谢谢.

library(shiny)

# innerModule 1
csvFile_Input <- function(id, label = ".csv file") {
  # Create a namespace function using the provided id
  ns <- NS(id)
  tagList(
    fileInput(ns("file"), label),
    checkboxInput(ns("heading"), "Has heading")
  )
}


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


  userFile <- reactive({
    req(input$file)
  })

  dataframe <- reactive({

    read.csv(userFile()$datapath,
             header = input$heading)
  })

  observe({
    msg <- sprintf("File %s was uploaded", userFile()$name)
    cat(msg, "\n")
  })



  return(dataframe)

}

# innerModule 2
othersource_Input <- function(id) {
  ns <- NS(id)
  textInput("text", label = h3("From the web"), value = "http://www.stats.ox.ac.uk/pub/datasets/csb/ch11b.dat")
}

othersource <- function(input, output, session){
  library(data.table)

  dataframe <- reactive({
    req(input$text)
    fread(input$text)
  })

   return(dataframe)
}

loadDataInput <- function(id){
  ns <- NS(id)
  wellPanel(
    tagList(
      uiOutput(ns("selectSource")),
      uiOutput(ns("text"))
    )
  )


}



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

  ns <- session$ns

  # Select source from user or from wibi
  output$selectSource<-renderUI(
    radioButtons(
      ns("dt"), "Data source",
      c(csvFile = "fromCSV", web = "fromWeb"),
      inline = TRUE
    )
  )

  dtype <- reactive({
    req(input$dt)
  })


  output$text <- renderUI({
    req(dtype())
    if(dtype() == "fromCSV"){
      csvFile_Input(ns("user"))
    } else if (dtype() == "fromWeb"){
      othersource_Input(ns("web"))
    }
  })


 data <- reactive({
    req(dtype())
    if(dtype() == "fromCSV"){
      callModule(csvFile, "user")
    } else if (dtype() == "fromWeb"){
      callModule(othersource, "web")
    }
  })


  output$viewinput <- renderPrint({
    print(data())
  })


  return(data)

}

loadDataOutput <- function(id){
  ns <- NS(id)
  verbatimTextOutput(ns("viewinput"))
}


ui <- fluidPage(
  titlePanel(paste("Calling module based on reactive input")),
  sidebarLayout(
    sidebarPanel(
      loadDataInput("test")
    ),
    mainPanel(
      loadDataOutput("test")
    )
  )
)

server <- function(input, output) {
  callModule(loadData, "test")
}

shiny::shinyApp(ui = ui, server = server)

推荐答案

您不应在renderXXXreactive函数中调用callModule.以下更改对我有用.

You shouldn't invoke callModule inside renderXXX or reactive functions. The following changes worked for me.

csvinput <- callModule(csvFile, "user")
otherinput <- callModule(othersource, "web")

data <- reactive({
  req(dtype())
  if(dtype() == "fromCSV"){
    csvinput()
  } else if (dtype() == "fromWeb"){
    otherinput()
  }
})

此外,您应该在othersource_Input中使用ns("text").如果您需要进一步的说明,请提供一个最小的示例.

Also, you should use ns("text") in othersource_Input. If you want further explanations, please provide a minimal example.

这篇关于如何基于无功输入调用闪亮模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆