1

我是 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,而不是newIndexdata$Index没有运气。任何帮助将不胜感激,如果这个问题非常基本,我深表歉意!

4

0 回答 0