热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

闪亮的R中有全局变量吗?-ArethereglobalvariablesinRShiny?

HowdoyoudeclareglobalvariablesinwithRShinysothatyoudonotneedtorunthesamepieceso

How do you declare global variables in with R Shiny so that you do not need to run the same pieces of code multiple times? As a very simple example I have 2 plots that use the same exact data but I only want to calculate the data ONCE.

如何在R中声明全局变量,从而不需要多次运行相同的代码片段?作为一个非常简单的例子,我有两个图使用相同的数据但是我只想计算一次数据。

Here is the ui.R file:

这是ui。R文件:

library(shiny)

# Define UI for application that plots random distributions 
shinyUI(pageWithSidebar(

# Application title
headerPanel("Hello Shiny!"),

# Sidebar with a slider input for number of observations
sidebarPanel(
sliderInput("obs", 
            "Number of observations:", 
            min = 1,
            max = 1000, 
            value = 500)
  ),

# Show a plot of the generated distribution
 mainPanel(
   plotOutput("distPlot1"),
  plotOutput("distPlot2")
 )
))

Here is the server.R file:

这是服务器。R文件:

library(shiny)

shinyServer(function(input, output) {

  output$distPlot1 <- renderPlot({ 
    dist <- rnorm(input$obs)
    hist(dist)
  })

  output$distPlot2 <- renderPlot({ 
    dist <- rnorm(input$obs)
    plot(dist)
  })

})

Notice that both output$distPlot1 and output$distPlot2 do dist <- rnorm(input$obs) which is re-running the same code twice. How do you make the "dist" vector run once and make it available to all the renderplot functions? I have tried to put the dist outside the functions like:

注意,输出$distPlot1和输出$distPlot2都dist <- rnorm(输入$obs),它们正在重新运行相同的代码两次。如何使“dist”向量运行一次并使其对所有的renderplot函数都可用?我试过把区间放在函数之外,比如:

library(shiny)

shinyServer(function(input, output) {

  dist <- rnorm(input$obs)

  output$distPlot1 <- renderPlot({ 
    hist(dist)
  })

  output$distPlot2 <- renderPlot({ 
    plot(dist)
  })

})

But I get an error saying the "dist" object is not found. This is a toy example in my real code I have 50 lines of code that I am pasting into multiple "Render..." function. Any help?

但是我得到一个错误,说“dist”对象没有找到。这是我的真实代码中的一个玩具示例,我将50行代码粘贴到多个“呈现…”函数中。任何帮助吗?

Oh yea if you want to run this code just create a file and run this: library(shiny) getwd() runApp("C:/Desktop/R Projects/testShiny")

噢,如果你想运行这段代码,只要创建一个文件并运行它:library(闪亮的)getwd() runApp(“C:/Desktop/R Projects/ testbling”)

where "testShiny" is the name of my R studio project.

我的R studio项目的名字是“testbling”。

2 个解决方案

#1


18  

This page on the Shiny webpage explains scoping of Shiny variables.

闪亮网页上的这一页解释闪亮变量的范围。

Global variables can either be put in server.R (as per Ricardo's answer) or in global.R.

全局变量可以放在服务器中。R(根据李嘉图的回答)或全球R。

Objects defined in global.R are similar to those defined in server.R outside shinyServer(), with one important difference: they are also visible to the code in ui.R. This is because they are loaded into the global environment of the R session; all R code in a Shiny app is run in the global environment or a child of it.

全球中定义的对象。R与服务器中定义的类似。在shinyServer()之外的R,有一个重要的区别:它们对ui.R中的代码也是可见的。这是因为它们被加载到R会话的全局环境中;闪亮的应用程序中的所有R代码都在全局环境中运行,或者是它的子环境。

In practice, there aren’t many times where it’s necessary to share variables between server.R and ui.R. The code in ui.R is run once, when the Shiny app is started and it generates an HTML file which is cached and sent to each web browser that connects. This may be useful for setting some shared configuration options.

实际上,在服务器之间共享变量的情况并不多见。R和ui.R。ui的代码。当闪亮的应用程序启动并生成HTML文件时,R将运行一次,该文件将被缓存并发送到连接的每个web浏览器。这对于设置一些共享配置选项可能很有用。

#2


6  

As mentioned in the link that @nico lists above, you can also use the <<- classifier inside your functions to make variables accessible outside of the function.

正如上面@nico列出的链接中提到的,您还可以在函数内部使用<- classifier来使函数之外的变量可访问。

foo <<- runif(10)

instead of

而不是

foo <- runif(10)

The link says "If the objects change, then the changed objects will be visible in every user session. But note that you would need to use the <<- assignment operator to change bigDataSet, because the <- operator only assigns values in the local environment."

这个链接说“如果对象发生了变化,那么在每个用户会话中都可以看到被改变的对象。”但是请注意,您需要使用<<赋值操作符来更改bigdataset,因为<-操作符只在本地环境中赋值。”

I have used this to varying levels of success in shiny. As always, be careful with global variables.

我在《闪亮》中运用了不同程度的成功。和往常一样,要小心全局变量。


推荐阅读
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文介绍了在使用Python中的aiohttp模块模拟服务器时出现的连接失败问题,并提供了相应的解决方法。文章中详细说明了出错的代码以及相关的软件版本和环境信息,同时也提到了相关的警告信息和函数的替代方案。通过阅读本文,读者可以了解到如何解决Python连接服务器失败的问题,并对aiohttp模块有更深入的了解。 ... [详细]
  • Python瓦片图下载、合并、绘图、标记的代码示例
    本文提供了Python瓦片图下载、合并、绘图、标记的代码示例,包括下载代码、多线程下载、图像处理等功能。通过参考geoserver,使用PIL、cv2、numpy、gdal、osr等库实现了瓦片图的下载、合并、绘图和标记功能。代码示例详细介绍了各个功能的实现方法,供读者参考使用。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了南邮ctf-web的writeup,包括签到题和md5 collision。在CTF比赛和渗透测试中,可以通过查看源代码、代码注释、页面隐藏元素、超链接和HTTP响应头部来寻找flag或提示信息。利用PHP弱类型,可以发现md5('QNKCDZO')='0e830400451993494058024219903391'和md5('240610708')='0e462097431906509019562988736854'。 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • importjava.util.ArrayList;publicclassPageIndex{privateintpageSize;每页要显示的行privateintpageNum ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 本文介绍了一个题目的解法,通过二分答案来解决问题,但困难在于如何进行检查。文章提供了一种逃逸方式,通过移动最慢的宿管来锁门时跑到更居中的位置,从而使所有合格的寝室都居中。文章还提到可以分开判断两边的情况,并使用前缀和的方式来求出在任意时刻能够到达宿管即将锁门的寝室的人数。最后,文章提到可以改成O(n)的直接枚举来解决问题。 ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
author-avatar
Cherish雪蕊诗_425
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有