ggplot2和shiny中的反应数据框:ggplot2不知道如何处理class reactiveExpr / reactive类的数据

时间:2017-09-13 00:24:50

标签: r ggplot2 reactive-programming shiny

R和Shiny的新手。尝试创建用户输入数据和绘图的简单应用程序。以下是相关的用户界面:

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),
   ),
mainPanel(
  plotOutput(outputId = "IQplot", width = "500px", height = "400px"),
  plotOutput(outputId = "Indplot", width = "500px", height = "400px"),

服务器:

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

### Set Dataframe
Subtest <- c("BD", "S", "MR", "DS", "CD", "V", "FW", "VP", "PS", "SS")
newSubtest_Score <-reactive({c(input$BD, input$S, input$MR, input$DS, input$CD,
                    input$V, input$FW, input$VP, input$PS, input$SS)})
SubtypeVerb <- c("NV", "V", "NV", "V", "NV", "V", "NV", "NV", "NV", "NV")
SubtypeInd <- c("VSI", "VCI", "FRI", "WMI", "PSI", "VCI", "FRI", "VSI", "WMI", "PSI")
SubtypeAuto <- c("M", "M", "M", "A", "A", "M", "M", "M", "A", "A")
newSubtest.data <- reactive({data.frame(Subtest, newSubtest_Score(), SubtypeVerb, SubtypeInd, SubtypeAuto)})

output$IQplot <- renderPlot({
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({
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
})

我从ggplot无法处理响应数据的错误中收集,但尝试将数据框newIndexdata()保存到新变量并将其放入ggplot但没有成功。任何想法都将不胜感激!

0 个答案:

没有答案