闪亮的不同模块的一般输入
General input for different modules in shiny
我有一个 ui 和一个闪亮的服务器模块,就像 Hadley Wickham 在 'Mastering Shiny' 中的示例一样:
histogramUI <-
function(id){
tagList(
selectInput(NS(id, "var"), "Variable", choices = names(mtcars)),
numericInput(NS(id, "bins"), "bins", value = 10, min = 1),
plotOutput(NS(id, "hist"))
)
}
histogramServer <-
function(id){
moduleServer(id, function(input, output, session){
data <- reactive(mtcars[[input$var]])
output$hist <- renderPlot({
hist(data(), breaks = input$bins, main = input$var)
}, res = 96)
})
}
现在我想创建具有两个输入和输出的应用程序,分别命名为 "hist1"
和 "hist2"
。
使用以下代码可以正常工作:
histogrammApp <-
function(){
ui <- fluidPage(
histogramUI("hist1"),
histogramUI("hist2")
)
server <- function(input, output, session){
histogramServer("hist1")
histogramServer("hist2")
}
shinyApp(ui, server)
}
每个地块都有自己的输入参数。
假设我想要一个通用输入 bins
,这样两个图在 numericInput
中会有相同数量的中断。我怎样才能做到这一点?
我的第一次尝试是删除行 numericInput(NS(id, "bins"), "bins", value = 10, min = 1),
并将行 tagList(numericInput("bins", "bins", value = 10, min = 1)),
放在行 histogramUI("hist1"),
之前,但这没有用。我收到以下错误:Invalid breakpoints produced by 'breaks(x)': NULL
。 input$bins
是 NULL
,我猜。我想是因为它在不同的命名空间中?我怎么会想到这个问题?
您应该考虑将 input$bins
作为反应传递给 histogramServer("hist1",reactive(input$bins))
。试试这个
histogramUI <- function(id){
tagList(
selectInput(NS(id, "var"), "Variable", choices = names(mtcars)),
#numericInput(NS(id, "bins"), "bins", value = 10, min = 1),
plotOutput(NS(id, "hist"))
)
}
histogramServer <- function(id,bins){
moduleServer(id, function(input, output, session){
data <- reactive(mtcars[[input$var]])
output$hist <- renderPlot({
hist(data(), breaks = bins(), main = input$var)
}, res = 96)
})
}
#histogrammApp <- function(){
ui <- fluidPage(
numericInput("bins", "Bins", value = 10, min = 1),
histogramUI("hist1"),
histogramUI("hist2")
)
server <- function(input, output, session){
histogramServer("hist1",reactive(input$bins))
histogramServer("hist2",reactive(input$bins))
}
shinyApp(ui, server)
# }
#
# histogrammApp()
我有一个 ui 和一个闪亮的服务器模块,就像 Hadley Wickham 在 'Mastering Shiny' 中的示例一样:
histogramUI <-
function(id){
tagList(
selectInput(NS(id, "var"), "Variable", choices = names(mtcars)),
numericInput(NS(id, "bins"), "bins", value = 10, min = 1),
plotOutput(NS(id, "hist"))
)
}
histogramServer <-
function(id){
moduleServer(id, function(input, output, session){
data <- reactive(mtcars[[input$var]])
output$hist <- renderPlot({
hist(data(), breaks = input$bins, main = input$var)
}, res = 96)
})
}
现在我想创建具有两个输入和输出的应用程序,分别命名为 "hist1"
和 "hist2"
。
使用以下代码可以正常工作:
histogrammApp <-
function(){
ui <- fluidPage(
histogramUI("hist1"),
histogramUI("hist2")
)
server <- function(input, output, session){
histogramServer("hist1")
histogramServer("hist2")
}
shinyApp(ui, server)
}
每个地块都有自己的输入参数。
假设我想要一个通用输入 bins
,这样两个图在 numericInput
中会有相同数量的中断。我怎样才能做到这一点?
我的第一次尝试是删除行 numericInput(NS(id, "bins"), "bins", value = 10, min = 1),
并将行 tagList(numericInput("bins", "bins", value = 10, min = 1)),
放在行 histogramUI("hist1"),
之前,但这没有用。我收到以下错误:Invalid breakpoints produced by 'breaks(x)': NULL
。 input$bins
是 NULL
,我猜。我想是因为它在不同的命名空间中?我怎么会想到这个问题?
您应该考虑将 input$bins
作为反应传递给 histogramServer("hist1",reactive(input$bins))
。试试这个
histogramUI <- function(id){
tagList(
selectInput(NS(id, "var"), "Variable", choices = names(mtcars)),
#numericInput(NS(id, "bins"), "bins", value = 10, min = 1),
plotOutput(NS(id, "hist"))
)
}
histogramServer <- function(id,bins){
moduleServer(id, function(input, output, session){
data <- reactive(mtcars[[input$var]])
output$hist <- renderPlot({
hist(data(), breaks = bins(), main = input$var)
}, res = 96)
})
}
#histogrammApp <- function(){
ui <- fluidPage(
numericInput("bins", "Bins", value = 10, min = 1),
histogramUI("hist1"),
histogramUI("hist2")
)
server <- function(input, output, session){
histogramServer("hist1",reactive(input$bins))
histogramServer("hist2",reactive(input$bins))
}
shinyApp(ui, server)
# }
#
# histogrammApp()