2017-09-05 50 views
0

我試圖從模塊獲取輸入到應用服務器。Shiny:從模塊獲取輸入

這裏是我想要的一個例子中,app.R:

library(shiny) 
source("test.R") 
ui <- fluidPage(
    tabPanel("tt", testUI("t")) 
) 

server <- function(input, output) { 
    ret <- callModule(test, "testm") 
    print(ret) 
} 

shinyApp(ui = ui, server = server) 

測試模塊:

testUI <- function(id) { 
    ns <- NS(id) 
    tabPanel(NULL, 
     textInput(ns("textInput1"), label = h5(""), value = "") 
)} 

    test <- function(input, output, session, data) { 
    reactive({input$textInput1}) 
    } 

我想從應用程序的服務器功能打印textInput1的內容.R並觀察它。

回答

0

您的代碼有兩個問題:1)在UI中,您調用模塊實例"t",在服務器"testm"中。這些需要相同。 2)模塊返回一個反應;這需要在被動環境中進行評估:reactive,observerender

library(shiny) 
source("test.R") 
ui <- fluidPage(
    tabPanel("tt", testUI("testm")) 
) 

server <- function(input, output) { 
    ret <- callModule(test, "testm") 
    observe(print(ret())) 
} 

shinyApp(ui = ui, server = server) 
+0

坦克您的回覆,現在的作品! – Grundoc

0

上面的解決方案解決了我提供的示例中的問題。

但是,我不解決它爲我的應用程序:

我我的應用我有app.R,一個module1.R和module2.R該模塊2是這是自己調用由模塊1電話app.R.我想從module1函數中打印'textInput1'的值。

app.R:

library(shiny) 
source("module1.R") 
ui <- fluidPage(
    tabPanel("Module 1", module1UI("module1")) 
) 

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

shinyApp(ui = ui, server = server) 

module1.R:

source("module2.R") 

module1UI <- function(id) { 
    ns <- NS(id) 
    tabPanel(NULL, 
    tabPanel("Module 2", module2UI("module2")) 
)} 

module1 <- function(input, output, session, data) { 
    reactive({input$textInput1}) 
    ret <- callModule(module2, "module2") 
    observe(print(ret())) 
} 

module2.R:

module2UI <- function(id) { 
    ns <- NS(id) 
    tabPanel(NULL, 
    textInput(ns("textInput1"), label = h5(""), value = "") 
)} 

module2 <- function(input, output, session, data) { 
    reactive({input$textInput1}) 
}