首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >当使用闪亮的模块时,无法在动态UI中呈现绘图

当使用闪亮的模块时,无法在动态UI中呈现绘图
EN

Stack Overflow用户
提问于 2016-08-07 18:07:56
回答 1查看 1.4K关注 0票数 2

通常,从UI内部生成图形没有问题,用户会主动调用这些图形,但我注意到,当使用闪亮模块动态生成UI时,图形不会出现。

下面是一个复制问题的模块化闪亮应用程序的最小示例;当用户按下"go“按钮时,它应该生成一个包含情节的ui:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
library("shiny")

exampleUI <- function(id, label = "example") {
        ns <- NS(id)
        tagList(   actionButton(ns("go"), "Go"),   uiOutput(ns("ui"))   )
}


ui <- fluidPage(   exampleUI("example")   )

example <- function(input, output, session) {
        observeEvent(input$go, {
                output$plot <- renderPlot(   plot(1:5, 1:5)   )
                output$ui <- renderUI(   plotOutput("plot")   )
        })
}

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

shinyApp(ui, server)

相反,非模块化版本的工作方式与预期的相同:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ui <- fluidPage(   
        tagList(   actionButton("go", "Go"),   uiOutput("ui")   )
)

server <- function(input, output) {     

        observeEvent(input$go, {
                output$plot <- renderPlot(   plot(1:5, 1:5)   )
                output$ui <- renderUI(   plotOutput("plot")   )
        })  
}

shinyApp(ui, server)

我希望有任何关于如何使第一个代码块像第二个代码块那样工作的提示。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-07 20:01:05

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
library(shiny)

exampleUI <- function(id, label = "example") {
    ns <- NS(id)
    tagList(   actionButton(ns("go"), "Go"),   uiOutput(ns("ui"))   )
}


example <- function(input, output, session) {
    observeEvent(input$go, {
        output$plot <- renderPlot(   plot(1:5, 1:5)   )
        ns <- session$ns
        output$ui <- renderUI(   plotOutput(ns("plot"))   )
    })
}


ui <- fluidPage(   exampleUI("example")   )

server <- function(input, output, session) {
    callModule(example, "example") 
}
shinyApp(ui, server)

有两件事:

  1. 您在定义exampleUI之前参考了它(在实际示例中,您可能会source该模块,因此这不应该是一个问题)
  2. 在包含输入/输出的模块中使用renderUI时,需要将其包装在ns中。请阅读有关在模块中使用renderUI的部分以获得更多信息
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38820375

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文