How can I add multiple input variables in reactive function in shiny server?

691 Views Asked by At

I want to update my output according to 3 different input variables, but the output is just updated by "var". I know that I should update the data <- reactive({}) function for "var", "Group", and "Timepoint", but I need help! I tried multiple ways but non of them worked! Thanks for your support in advance.

> 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[,c("M0","M1","M2","M3","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)

1

There are 1 best solutions below

4
On

There's no real need for the whole reactive call here. You can just do something like:

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

And make a similar change in the renderPrint as well