如何在闪亮的模块中查看服务器的输出?

时间:2020-10-17 03:27:04

标签: r shiny

我试图根据一个示例创建一个简单的模块。但是,我无法查看输出。我猜我使用callModule的方式有问题。我还想知道如何使用moduleServer来代替。我尝试将importcontents用作id,但我觉得那没有用。

library(shiny)

importUI <- function(id) {
  ns <- NS(id)
  
  tagList(
    fileInput("file1", "Choose CSV File", accept = ".csv"),
    checkboxInput("header", "Header", TRUE),
    tableOutput("contents")
  )
  
}

importSE <- function(input, output, session) {
  
  output$contents <- renderTable({
    file <- input$file1
    ext <- tools::file_ext(file$datapath)
    
    req(file)
    validate(need(ext == "csv", "Please upload a csv file"))
    
    read.csv(file$datapath, header = input$header)
    
  })
}

ui <- fluidPage(
  importUI("import")
)


server <- function(input, output, session) {
  
  callModule(importSE, "import")
  
}

shinyApp(ui, server)

这是示例.csv file

的链接

1 个答案:

答案 0 :(得分:2)

命名元素时,需要在<Setter Property="ItemContainerStyle">函数中使用ns()函数。看起来应该像

importUI()

如果使用模块服务器,则同样适用。但是在那种情况下,您将不再需要importUI <- function(id) { ns <- NS(id) tagList( fileInput(ns("file1"), "Choose CSV File", accept = ".csv"), checkboxInput(ns("header"), "Header", TRUE), tableOutput(ns("contents")) ) } ,那么您就可以使用

importSE

然后您将在服务器功能中拥有

myModuleServer <- function(id) {
  moduleServer(id, 
    function(input, output, session) {
      output$contents <- renderTable({
        file <- input$file1
        ext <- tools::file_ext(file$datapath)
      
        req(file)
        validate(need(ext == "csv", "Please upload a csv file"))
    
        read.csv(file$datapath, header = input$header)
      })
    }
  )
}