Hen*_*ndy 5 r rstudio output shiny
我有一个闪亮的应用程序,我在其中定义了一个基于滑块的对象并data.frame从中创建了一个对象。这用于创建一个图,我想在其下方包含一个汇总表。我的问题是必须在反应对象内定义绘图才能根据滑块进行更新,但是我发现当我尝试访问该对象以在不同反应对象内打印摘要时,没有找到它。
我不想两次运行相同的计算代码只是为了将对象放入另一个反应对象中。我的两个初步想法:
save(object, file = "...")在第一个反应对象内部使用,然后load(file = "...")在第二个内部使用,但它没有更新。plotOutput定义之外定义我的对象,但我很确定当用户输入改变时它不会更新;它只会保留初始input对象默认设置的值。好吧,继续尝试后一个想法,它甚至没有那么远:
shinyServer(...),它无法生成数据,因为我的调用依赖于input它input在那时不知道。shinyServer(...)但之前renderPlot(...)尝试为所有输出对象创建一个“全局可用”对象,它责骂我试图做一些input在反应函数之外使用的东西。这里有一个例子来说明,修改自 Shiny 的“Hello Shiny!” 例子:
用户界面
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")
)
))
Run Code Online (Sandbox Code Playgroud)
服务器
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)))
})
})
Run Code Online (Sandbox Code Playgroud)
如果按原样运行它,cut将抛出并出错,因为它不知道dist. 如果您取消注释我们在 中重新定义dist的行plotOutput,那么它将起作用。
这是一个简单的代码——我的代码比较复杂,我真的不想运行它两次只是为了让另一个反应输出知道相同的结果。
建议?
只是为了根据我认为我的问题重复的另一个问题的答案充实上面的示例,代码如下所示:
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)))
})
})
Run Code Online (Sandbox Code Playgroud)