【发布时间】:2019-04-05 16:19:17
【问题描述】:
我是闪亮的新手,我正在尝试做一个简单的密度图,其中有 2 组数据,均值中的反应性“变化”等。
对此的简化总结是,一组数据的平均值为 0,方差为 1。第二组数据的平均值为 shift,在滑块中定义。
我曾尝试使用reactiveValues,如下面的代码所示来存储观察矩阵d1,由density函数y值生成,对应的x值存储在x中。
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("shift",
"shift of 2nd set",
min = -1,
max = 1,
value = 0)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
data <- reactiveValues({
d1 <- matrix(nrow=100, ncol=512)
for(i in 1:70){
d1[i,] <- density(rnorm(1000),from = -3, to = 3)$y
}
for(i in 71:100){
d1[i,] <- density(rnorm(1000, input$shift),from = -3, to = 3)$y
}
x <- density(rnorm(1000),from = -3, to = 3)$x
})
output$distPlot <- renderPlot({
matplot(data$x, t(data$d1), type = "l", lty = 1, col = c(rep(1,70),rep(2,30)))
})
}
# Run the application
shinyApp(ui = ui, server = server)
以上代码主要来自示例闪亮应用程序,因此请原谅任何通用引用。它应该仍然有效。
我期待一个闪亮的图,左边有一个滑块,右边有一个有 100 条密度线的 2 种颜色的图。当shift 滑块更改时,第二组数据(红色)将根据移位向左或向右滑动。
相反,我收到了错误消息
55: stop
54: .getReactiveEnvironment()$currentContext
53: .subset2(x, "impl")$get
52: $.reactivevalues
47: server [/beavis/Documents/test/app.R#37]
Error in .getReactiveEnvironment()$currentContext() :
Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
有人能帮我修复这个代码吗?任何帮助将不胜感激。玩了一个小时后,我相信问题出在reactiveValues 部分,但到目前为止没有任何效果。
【问题讨论】: