Error: cannot coerce class "c("reactiveExpr", "reactive")" to a data.frame

4k views Asked by At

I'm new to R, Shiny, and coding in general and have a question. I've been reading through similar questions and cannot get my Shiny app to function based on the answers. Basically, I want the user to be able to enter in numeric inputs, put those inputs into a data frame, and then output plots. Here is the relevant part of my code for the UI:

ui <- fluidPage(
titlePanel("IQ Plots"),
sidebarLayout(
sidebarPanel(
  numericInput(inputId = "FSIQ",
               label = "FSIQ",
               min = 40, max = 160, value = 100),
  numericInput(inputId = "VCI",
               label = "VCI",
               min = 40, max = 160, value = 100),
  numericInput(inputId = "VSI",
               label = "VSI",
               min = 40, max = 160, value = 100),
  numericInput(inputId = "FRI",
               label = "FRI",
               min = 40, max = 160, value = 100),
  numericInput(inputId = "WMI",
               label = "WMI",
               min = 40, max = 160, value = 100),
  numericInput(inputId = "PSI",
               label = "PSI",
               min = 40, max = 160, value = 100)

And here is the Server:

server <- function(input, output) {
### Set dataframe
Index <- c("VCI", "VSI", "FRI", "WMI", "PSI")
Index_Score <- c(VCI, VSI, FRI, WMI, PSI)
Index_Verbal <- c("Verbal", "Non-verbal", "Non-verbal", "Verbal", "Non-verbal")
Index_Auto <- c("Mediated", "Mediated", "Mediated", "Automatic", "Automatic")
Index.data <- data.frame(Index, Index_Score, Index_Verbal, Index_Auto)

newIndex_Score <- reactive({c(input$VCI, input$VSI, input$FRI, input$WMI, input$PSI)})
newIndex.data <- reactive({data.frame(Index, newIndex_Score, Index_Verbal, Index_Auto)})

output$IQplot <- renderPlot({
library(ggplot2)
ggplot(data = IQdist, aes(x = ndist, y = IQy)) +
  fdist + 
  ggtitle("Full Scale IQ") +
  xlab("IQ score") +
  ylab("Frequency") +
  scale_x_continuous(limits = c(40,160), breaks = seq(55,145,15)) +
  geom_segment(aes(x=input$FSIQ, y = 0, xend=input$FSIQ,
                   yend = dnorm(input$FSIQ, IQmean, IQsd)),
               color = "blue", size = 2) +
  theme(axis.text.y = element_blank(),
        axis.ticks = element_blank()) +
  geom_ribbon(data=subset(IQdist, ndist > 85 & ndist < 115),
              aes(ymax=IQy),ymin=0,fill="green", alpha = 0.3) +
  geom_ribbon(data=subset(IQdist, ndist > 70 & ndist < 85),
              aes(ymax=IQy),ymin=0,fill="yellow", alpha = 0.3) +
  geom_ribbon(data=subset(IQdist, ndist > 115 & ndist < 130),
              aes(ymax=IQy),ymin=0,fill="yellow", alpha = 0.3) +
  geom_ribbon(data=subset(IQdist, ndist <= 70.5),
              aes(ymax=IQy),ymin=0,fill="red", alpha = 0.3) +
  geom_ribbon(data=subset(IQdist, ndist > 130),
              aes(ymax=IQy),ymin=0,fill="red", alpha = 0.3) +
  scale_y_continuous(limits = c(0,.03), expand = c(0,0)) +
  expand_limits(y=0) +
  geom_segment(aes(x = 100, y = 0, xend = 100,
                   yend = IQy), color = "red", size = 1) +
  annotate('text', x = 100, y = 0.01,
           label = "<------------Average------------>", col = "black")
})

output$Indplot <- renderPlot({
library(ggplot2)
newIndex.data()$Index <- factor(newIndex.data()$Index, levels = c("VCI", "VSI", "FRI", "WMI", "PSI"))
newIndex.data()$Index <- factor(newIndex.data()$Index,
                           levels = newIndex.data()$Index[order(newIndex.data()$Index)])
Indplot <- ggplot(data = newIndex.data(), x = Index, y = newIndex_Score()) +
  ggtitle("Index Scores") +
  xlab("WISC-V Index") +
  ylab("Standard Score") +
  scale_y_continuous(limits = c(0,160), breaks = seq(10,160,15),
                     expand = c(0,0)) +
  geom_rect(data = newIndex.data(), aes(x = Index, y = newIndex_Score()),
            xmin = as.numeric(newIndex.data()$Index[[1]]) - 1,
            xmax = as.numeric(newIndex.data()$Index[[5]]) + 1,
            ymin = 85,
            ymax = 115,
            fill = "green",
            alpha = 0.1) +
  geom_rect(data = newIndex.data, aes(x = Index, y = newIndex_Score()),
            xmin = as.numeric(newIndex.data()$Index[[1]]) - 1,
            xmax = as.numeric(newIndex.data()$Index[[5]]) + 1,
            ymin = 70,
            ymax = 85,
            fill = "yellow",
            alpha = 0.1) +
  geom_rect(data = newIndex.data(), aes(x = Index, y = newIndex_Score()),
            xmin = as.numeric(newIndex.data()$Index[[1]]) - 1,
            xmax = as.numeric(newIndex.data()$Index[[5]]) + 1,
            ymin = 115,
            ymax = 130,
            fill = "yellow",
            alpha = 0.1) +
  geom_col(aes(x = Index, y = newIndex_Score()), fill = "blue4") +
  geom_hline(yintercept = 100, color = "red", size = 2)
Indplot
})
}

The first plot "IQplot" works, I think because it's just one input that the plot needs to react to rather than 5 inputs that need to be put into a data frame. The second plot, "Indplot" keeps appearing with Error: cannot coerce... As can be seen in the code, I tried putting () after the data frame, as in newIndexdata()$Index, rather than newIndexdata$Index with no luck. Any help would be greatly appreciated and I apologize if this question is very basic!

0

There are 0 answers