0

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)

Hossein
  • 1
  • 1

1 Answers1

0

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

  • Thank you for your help! I got this Error in the App: " < table of extent 0 x 0 > need finite 'ylim' values " and the following error in the Rstudio! "Listening on http://127.0.0.1:3723 Warning in min(x) : no non-missing arguments to min; returning Inf Warning in max(x) : no non-missing arguments to max; returning -Inf Warning: Error in plot.window: need finite 'ylim' values [No stack trace available]" Would you please see that! – Hossein Jun 01 '21 at 23:00
  • For one try setting a default in `input$var` for example adding `selected="M0"` as an option in the selectInput. It is trying to subset on a variable that is not selected yet on load. –  Jun 01 '21 at 23:19
  • I tried that, but did not work! Still this error: Warning: Error in plot.window: need finite 'ylim' values [No stack trace available] – Hossein Jun 01 '21 at 23:25
  • Also change choices in selectInput("var",...) to just the characters `choices=c("M0","M1",...)` –  Jun 01 '21 at 23:59