【发布时间】: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