I am trying to use ggplot2 with a reactive object in shiny. So, I understand that reactive gives a reactive object that needs to be changed to be a value, but I tried to assign it to a different object both in the reactive part and in the renderPlot part and got no results...
So, if I don't assign it to a different variable, I get the message:
ggplot2 doesn't know how to deal with data of class
reactiveExpr/reactive
I know there are at least 2 questions (here and here) that talk about this problem. I did do my homework and try to use those answers and it didn't work. As far as I can tell, those answers suggest to assign the reactive object to another variable, which I did, and got the message:
Error: object is not a matrix
The answers delineated there do not really explain how to fix the problem in general, they just provide the code to fix that particular example (one answer reads " I think this particular problem should be solved with the following code:" but doesn't point out what part of the code solves the issue).
Can you please explain, how to fix this in this example in the more general case?
here is a Minimal example:
server.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)
})
})
and the ui.R file:
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Concentration of Indomethacin"),
sidebarLayout(
sidebarPanel(
sliderInput("slidertime",
"Time:",
min = 8,
max = 15,
value = 8)),
mainPanel(
plotOutput("myPlot")
)
)
))