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

时间:2021-09-08 16:46:56

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,因为<-操作符只在本地环境中赋值。”< p>

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

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

#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,因为<-操作符只在本地环境中赋值。”< p>

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

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