错误:无法将类“c(”reactiveExpr“,”reactive“)”强制转换为data.frame

时间:2017-09-12 12:41:05

标签: r dataframe shiny shiny-reactivity

我是R,Shiny和编码的新手并且有一个问题。我一直在阅读类似的问题,无法让我的Shiny应用程序根据答案运行。基本上,我希望用户能够输入数字输入,将这些输入放入数据框,然后输出图。以下是我的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)

这是服务器:

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
})
}

第一个图“IQplot”可以工作,我认为因为它只是一个输入,需要对图形进行响应,而不是需要放入数据帧的5个输入。第二个图“Indplot”一直出现错误:无法强制......从代码中可以看出,我尝试将()放在数据框之后,如newIndexdata()$Index,而不是{{ 1}}没有运气。非常感谢任何帮助,如果这个问题非常基本,我会道歉!

0 个答案:

没有答案