R Shiny中是否有全局变量? [英] Are there global variables in R Shiny?

查看:701
本文介绍了R Shiny中是否有全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何用R Shiny声明全局变量,以便你不需要多次运行相同的代码片段?作为一个非常简单的例子,我有2个使用相同确切数据的图,但我只想计算一次数据。



以下是 ui.R 文件:

  
$定义用于绘制随机分布的应用程序的UI
shinyUI(pageWithSidebar(

#应用程序标题
headerPanel( Hello Shiny!),

#带侧滑块输入的观察点数量的边栏
sidebarPanel(
sliderInput(obs,
观察次数: ,
min = 1,
max = 1000,
value = 500)
),

#显示生成的分布图
mainPanel(
plotOutput(distPlot1),
plotOutput(distPlot2)

))

以下是 server.R 文件:

 <$ (函数(输入,输出)){

输出$ distPlot1 < - renderPlot({
dist < - rnorm(输入$ obs)
hist(dist)
})

输出$ distPlot2 < - rende rPlot({
dist < - rnorm(输入$ obs)
plot(dist)
})

})
输出$ distPlot1 和输出$ distPlot2 $ b> / code> do dist < - rnorm(input $ obs),它将重新运行相同的代码两次。你如何让dist向量运行一次并使其可用于所有的renderplot函数?我试图把dist放在函数之外,例如:

  library(shiny)

shinyServer函数(输入,输出){

dist < - rnorm(输入$ obs)

输出$ distPlot1 < - renderPlot({
hist(dist)
})

输出$ distPlot2 < - renderPlot({
plot(dist)
})

})

但是我收到一个错误,提示找不到dist对象。这是我真实代码中的一个玩具示例,我有50行代码,正在粘贴到多个Render ...函数中。任何帮助?

如果你想运行这段代码,只需创建一个文件并运行:
library(shiny)
getwd()
runApp(C:/ Desktop / R Projects / testShiny)



其中testShiny是我的R studio项目的名称。

解决方案

此页面位于 Shiny网页解释了Shiny变量的范围。



全局变量可以放在 server.R 中每个里卡多的答案)或 global.R


在全局中定义的对象。 R与shinyServer()中的server.R中定义的类似,但有一个重要区别:它们对ui.R中的代码也是可见的。这是因为它们被加载到R会话的全球环境中; Shiny应用程序中的所有R代码都运行在全球环境或其中的一个小孩中。

实际上,在server.R和ui.R之间共享变量是很有必要的。当启动Shiny应用程序时,ui.R中的代码会运行一次,并生成一个缓存的HTML文件并发送到每个连接的Web浏览器。这对设置一些共享的配置选项可能很有用。



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.

Here is the ui.R file:

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:

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:

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?

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

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

解决方案

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.

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.

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 Shiny中是否有全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆