2012-12-16 26 views
3

我最近開始玩Shiny。我試圖寫些東西來證明中心極限定理。我的代碼如下:R /發光的情節不顯示在瀏覽器

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"))) 
)) 

server.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()的代碼,下面是我得到了什麼。正如你所看到的,該圖不會顯示。

enter image description here

怪異的一部分是,當我回到我的Rstudio,並按下「ESC」退出應用程序,在我Rstudio中顯示的情節,如下圖所示:

enter image description here

我想知道是否有人知道我的代碼有什麼問題。謝謝!!

回答

8

你想打包你的繪圖功能reactivePlot(...),而不僅僅是reactive(...)

一般而言,reactive(...)應該用於服務器中的輔助函數,該函數將input相關數據提供給output函數。但是,實際上生成output對象的函數應該使用特定的反應函數進行封裝,例如reactiveText,reactivePrint,reactiveTablereactivePlot

+0

非常感謝! – Alex