如何在闪亮的服务器中的反应函数中添加多个输入变量?

问题描述

我想根据 3 个不同的输入变量更新我的输出,但输出只是由“var”更新。我知道我应该为“var”、“Group”和“Timepoint”更新数据

> head(Metab)
  Replicate Group Timepoint        M0       M1     M2 M3 M4
1         1  DM         0   462276966 18832499 123172  0  0
2         2  DM         0   389073706 16540080 104004  0  0
3         3  DM         0   440497937 18778082  74654  0  0
4         4  DM         0   339266538 14427218  89319  0  0
5         5  DM         0   476276612 20352483  51748  0  0
6         6  DM         0   392164060 16587310 111081  0  0

library(shiny)

Metab<- read.csv("Metab.csv")
Metab[,c("M0","M1","M2","M3","M4")] <- sapply(Metab[,"M4")],as.numeric)

ui <- fluidPage(

titlePanel("weight Expectation"),selectInput("var",label="Choose an ...",choice=c("M0"=4,"M1"=5,"M2"=6,"M3"=7,"M4"=8),selectize=FALSE),selectInput(inputId = "Group",label = strong("Group"),choices = unique(Metab$Group),selected = "DM"),selectInput(inputId = "Timepoint",label = strong("Timepoint"),choices = unique(Metab$Timepoint),selected = 30),verbatimtextoutput("statistic"),plotOutput("Box")
)

server <- function(input,output){

data <- reactive({
Metab[,as.numeric(input$var)]

})

output$statistic <- renderPrint({
summary(data())
})

output$Box <- renderPlot({
x<-summary(data())
Boxplot(x,col="sky blue",border="purple")
})
}

shinyApp(ui = ui,server = server)

解决方法

这里实际上不需要整个 reactive 调用。您可以执行以下操作:

output$box <- renderPlot({
x <- metab[metab$Group == input$Group & metab$Timepoint == input$Timepoint,which(colnames(metab)==input$var)] 
x <- summary(x)
boxplot(x,col="sky blue",border="purple")
})

并在 renderPrint 中进行类似的更改