【问题标题】:R/Shiny plots do not show in the browserR/Shiny 图不显示在浏览器中
【发布时间】:2012-12-03 23:04:49
【问题描述】:

我最近开始玩 Shiny。我试图写一些东西来证明中心极限定理。我的代码如下:

ui.R:

#****************************************ui.R file code*****************************

library(shiny)
shinyUI(pageWithSidebar(headerPanel("Central Limit Theorem"),
                        sidebarPanel(selectInput("Distribution", 
                                                 "Distribution:", 
                                                 list("normal", "lognormal")),
                                                 br(),
                                                 sliderInput("sam_size", 
                                                              "Sample size:", 
                                                              min = 5, 
                                                              max = 500, 
                                                              value = 5)
                        ),
                        mainPanel(tabPanel("Plot", plotOutput("plot")))
))

服务器.R:

#****************************************server.R file code**************************
library(shiny)
shinyServer(function(input, output){
        data <- reactive(function(){Distribution <- switch(input$Distribution,
                                                           normal = rnorm,
                                                           lognormal = rlnorm,
                                                           rnorm
                                                           )
                                    Distribution(input$sam_size*2000)})

        output$plot <- reactive(function(){
                            Distribution <- input$Distribution
                            sam_size <- input$sam_size
                            temp <- matrix(data(), ncol=2000)
                            xbars <- colMeans(temp)
                            hist(xbars, main=paste("Sampling Distribution of the Mean Based on a", Distribution,
                         "distribution with n =", sam_size))})
})

当我尝试使用runApp() 运行代码时,我得到了以下结果。如您所见,该图未显示。

奇怪的是,当我回到我的 Rstudio 并按“Esc”退出应用程序时,我的 Rstudio 中显示的绘图如下所示:

我想知道是否有人知道我的代码有什么问题。谢谢!!

【问题讨论】:

    标签: r plot rstudio shiny


    【解决方案1】:

    您想用reactivePlot(...) 包装绘图函数,而不仅仅是reactive(...)

    一般来说,reactive(...) 应该用于服务器中的辅助函数,这些函数将依赖于input 的数据传递给output 函数。但是,实际生成 output 对象的函数应该用专门的反应函数包装,例如 reactiveTextreactivePrintreactiveTablereactivePlot

    【讨论】:

      猜你喜欢
      • 2021-11-25
      • 2011-09-18
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多