R 使在一个反应对象中创建的对象可用于另一个反应对象
我有一个闪亮的应用程序,其中我定义了一个基于滑块的对象,并从中创建了一个R 使在一个反应对象中创建的对象可用于另一个反应对象,r,output,rstudio,shiny,R,Output,Rstudio,Shiny,我有一个闪亮的应用程序,其中我定义了一个基于滑块的对象,并从中创建了一个data.frame。这被用来创建一个绘图,下面我想包括一个汇总表。我的问题是,必须在反应对象内部定义绘图,以便根据滑块进行更新,但我发现,当我尝试访问该对象以打印另一个反应对象内部的摘要时,没有找到它 我不想仅仅为了让对象进入另一个反应对象而对同一个计算代码运行两次。我最初的两个想法: 我尝试在第一个被动对象中使用save(object,file=“…”),然后在第二个被动对象中使用load(file=“…”),但它没有
data.frame
。这被用来创建一个绘图,下面我想包括一个汇总表。我的问题是,必须在反应对象内部定义绘图,以便根据滑块进行更新,但我发现,当我尝试访问该对象以打印另一个反应对象内部的摘要时,没有找到它
我不想仅仅为了让对象进入另一个反应对象而对同一个计算代码运行两次。我最初的两个想法:
- 我尝试在第一个被动对象中使用
,然后在第二个被动对象中使用save(object,file=“…”)
,但它没有更新load(file=“…”)
- 我想在我的反应式
定义之外定义我的对象,但我很确定当用户输入发生变化时它不会更新;它将只保留初始plotOutput
对象默认设置的值输入
- 我把它放在
之前,它无法生成数据,因为我的调用依赖于shinyServer(…)
,而此时它不知道input
input
- 我把它放在
之后,但放在shinyServer(…)
之前,试图为所有输出对象创建某种“全局可用”的对象,它责怪我试图在反应函数之外使用renderPlot(…)
输入
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("distPlot"),
tableOutput("summary")
)
))
library(shiny)
# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# generate an rnorm distribution and plot it
dist <- rnorm(input$obs)
hist(dist)
})
output$summary <- renderTable({
#dist <- rnorm(input$obs)
summary <- table(data.frame(cut(dist, 10)))
})
})
server.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("distPlot"),
tableOutput("summary")
)
))
library(shiny)
# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
# generate an rnorm distribution and plot it
dist <- rnorm(input$obs)
hist(dist)
})
output$summary <- renderTable({
#dist <- rnorm(input$obs)
summary <- table(data.frame(cut(dist, 10)))
})
})
库(闪亮)
#定义生成和绘制随机分布所需的服务器逻辑
shinyServer(功能(输入、输出){
输出$distPlot根据中的答案充实上述示例,代码如下所示:
library(shiny)
# Define server logic required to generate and plot a random distribution
shinyServer(function(input, output) {
dist <- reactive(rnorm(input$obs))
output$distPlot <- renderPlot({
# generate an rnorm distribution and plot it
dist <- dist()
hist(dist)
})
output$summary <- renderTable({
dist <- dist()
summary <- table(data.frame(cut(dist, 10)))
})
})
库(闪亮)
#定义生成和绘制随机分布所需的服务器逻辑
shinyServer(功能(输入、输出){
在我问了我的问题之后,我试着用更好的术语搜索(打电话给我正在寻找一个“全局对象”上面的我的外行短语),找到了我认为是回答问题的复制品。