【问题标题】:How to call shiny module based on reactive input?如何根据反应输入调用闪亮模块?
【发布时间】:2018-03-29 10:18:37
【问题描述】:

我想根据响应式输入调用嵌套模块服务器,并在主面板上显示返回输出。函数 callModule 在依赖于响应式输入时似乎在这里不起作用。

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

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

闪亮::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)

【问题讨论】:

    标签: r module shiny reactive-programming


    【解决方案1】:

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

    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")。如果您想进一步解释,请提供一个最小示例。

    【讨论】:

      猜你喜欢
      • 2017-12-09
      • 2019-09-01
      • 2021-12-06
      • 2020-03-12
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-10-10
      • 2020-08-18
      相关资源
      最近更新 更多