R/Shiny 图不显示在浏览器中 [英] R/Shiny plots do not show in the browser
问题描述
我最近开始玩 Shiny.我试图写一些东西来证明中心极限定理.我的代码如下:
I started playing with Shiny recently. I was trying to write something to demonstrate central limit theorem. my code is as follows:
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()
运行代码时,下面是我得到的.如您所见,该图未显示.
When I tried to run the code using runApp()
, below is what I got. As you can see, the plot is not displayed.
奇怪的是,当我回到我的 Rstudio 并按Esc"退出应用程序时,我的 Rstudio 中显示的情节如下图所示:
The weird part is that, when I went back to my Rstudio, and pressed "Esc" to exit the app, the plot displayed in my Rstudio as shown below:
我想知道是否有人知道我的代码有什么问题.谢谢!!
I wonder if anyone knows what the problem is with my code. Thanks!!
推荐答案
你想用 reactivePlot(...)
包装你的绘图函数,而不仅仅是 reactive(...)
.
You want to wrap your plotting function with reactivePlot(...)
, rather than just reactive(...)
.
一般来说,reactive(...)
应该用于服务器中的辅助函数,用于将 input
相关数据传送到 output
职能.然而,实际生成 output
对象的函数应该用专门的反应性函数包装,例如 reactiveText
、reactivePrint
、reactiveTable
和 reactivePlot
.
In general, reactive(...)
should be used for helper functions in your server that deliver input
-dependent data to output
functions. Functions that actually generate output
objects, however, should be wrapped with the specialized reactive functions, such as reactiveText
, reactivePrint
, reactiveTable
, and reactivePlot
.
这篇关于R/Shiny 图不显示在浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!