R/Shiny 图不显示在浏览器中

编程入门 行业动态 更新时间:2024-10-20 07:40:35
本文介绍了R/Shiny 图不显示在浏览器中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近开始玩 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 图不显示在浏览器中

本文发布于:2023-07-18 12:51:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1144955.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器中   Shiny

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!