【发布时间】:2018-03-23 21:03:09
【问题描述】:
我正在尝试将 ggplot2 与闪亮的反应对象一起使用。 所以,我知道反应提供了一个需要更改为值的反应对象,但我试图在反应部分和 renderPlot 部分将它分配给不同的对象,但没有得到任何结果......
所以,如果我不将它分配给不同的变量,我会收到消息:
ggplot2 doesn't know how to deal with data of class
reactiveExpr/reactive
我知道至少有 2 个问题(here 和 here)讨论了这个问题。我确实做了功课并尝试使用这些答案,但没有奏效。据我所知,这些答案建议将反应对象分配给另一个变量,我这样做了,并得到了消息:
Error: object is not a matrix
那里描述的答案并没有真正解释如何解决一般问题,它们只是提供了修复该特定示例的代码(一个答案是“我认为应该使用以下代码解决这个特定问题:”但没有'不要指出代码的哪一部分解决了问题)。
您能否解释一下,在更一般的情况下如何在此示例中解决此问题?
这是一个最小的例子:
服务器.R
library(shiny)
shinyServer(function(input, output) {
library(ggplot2)
library(lme4)
model1 <- lmList((conc) ~ time | Subject, data = Indometh)
newpoints <- reactive({data.frame("Subject" = c(1, 4, 2, 5, 6, 3),
"conc" = predict(model1, newdata = data.frame(time=input$sliderTime)),
"time" = rep(input$sliderTime, 6))
})
output$myPlot <- renderPlot({
newpoints2 <- newpoints()
g <- ggplot(Indometh, aes(time, (conc), color= Subject)) + geom_point() +
stat_smooth(method="lm",fullrange=TRUE, fill=NA)
newg <- g + geom_point(data = newpoints2, mapping =
aes(x = time, y = (conc), color= factor(Subject)))
print(newg)
})
})
还有 ui.R 文件:
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Concentration of Indomethacin"),
sidebarLayout(
sidebarPanel(
sliderInput("slidertime",
"Time:",
min = 8,
max = 15,
value = 8)),
mainPanel(
plotOutput("myPlot")
)
)
))
【问题讨论】: