繁体   English   中英

模块内部模块 shiny

[英]Module inside module shiny

我正在尝试从模块内部调用模块并遇到一些问题。

第一个代码正在运行,它显示了一个带有创建弹出窗口的按钮的应用程序。 弹出窗口内部是一个 plot 和一个 slider 输入。 弹出图在它自己的模块中定义。

library(shiny)
library(shinyWidgets)

uiForModal <<- function(id) {
  ns <- NS(id)
    tagList(
        fluidRow(
            plotOutput(outputId = ns("plot")),
            sliderInput(
                inputId =ns( "clusters"),
                label = "Number of clusters",
                min = 2, max = 6, value = 3, width = "100%"
            )
        )
    )   
}

serverForModal <<- function(input, output, session) {
    output$plot <- renderPlot({
        print(head(iris))
        plot(Sepal.Width ~ Sepal.Length,
            data = iris, col = Species,
            pch = 20, cex = 2)
        points(kmeans(iris[, 1:2], input$clusters)$centers,
            pch = 4, cex = 4, lwd = 4)
    })
}

ui <- fluidPage(
  actionButton("showPlot", "showPlot")
)

server <- function(input, output){
    observeEvent(input$showPlot, {
        show_alert(
            title = "Some Title",
            text = tags$div(
                uiForModal("test1")
            ),
            html = TRUE,
            width = "80%"
        )
    })
    callModule(serverForModal, "test1")
}
runApp(shinyApp(ui, server))

当我尝试将按钮放在它自己的模块中时,就会出现问题。 下面的代码是我失败的尝试。 我认为问题与命名空间有关。 在下面的代码中,按钮使用弹出窗口和 slider 调用 UI,但 plot 不显示。 所以我认为问题出在 plot 的服务器命名空间中。 有人可以帮我吗?


library(shiny)
library(shinyWidgets)

uiForModal <<- function(id) {
    print(id)
    ns <- NS(id)
    print(ns("plot"))
    tagList(
        fluidRow(
            plotOutput(outputId = ns("plot")),
            sliderInput(
                inputId =ns( "clusters"),
                label = "Number of clusters",
                min = 2, max = 6, value = 3, width = "100%"
            )
        )
    )   
}

serverForModal <<- function(input, output, session) {
    output$plot <- renderPlot({
        print(head(iris))
        plot(Sepal.Width ~ Sepal.Length,
            data = iris, col = Species,
            pch = 20, cex = 2)
        points(kmeans(iris[, 1:2], input$clusters)$centers,
            pch = 4, cex = 4, lwd = 4)
    })
}

uiForButton <<- function(id) {
    ns <- NS(id)
    tagList(
        fluidRow(
            actionButton(ns("showPlot"), "showPlot")
        )
    )
}

serverForButton <<- function(input, output, session, ns) {
    observeEvent(input$showPlot, {
        show_alert(
            title = "Some Title",
            text = tags$div(
                uiForModal(ns("test2"))
            ),
            html = TRUE,
            width = "80%"
        )
    })
    callModule(serverForModal, ns("test2"))
}

ui <- fluidPage(
    uiForButton("test1")
)
server <- function(input, output){
    callModule(serverForButton, "test1", NS("test1"))
}
    
runApp(shinyApp(ui, server))

改变

callModule(serverForModal, ns("test2"))

callModule(serverForModal, "test2")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM