0
votes

Using the mtcars dataset in R, I am tying to use the input variable (cyl, am etc...) to calculate the mean mpg by that variable.

My code in the ui.R is like:

verbatimTextOutput("Avg_Mileage")

My code in the shinServer function in server.R is like:

carsdata <- mtcars

output$Avg_Mileage <- renderPrint({aggregate(mpg~input$variable, carsdata,mean)})

I have tried to change the code in server.R in various ways. But I keep getting the message about conflicting variable lengths: "variable lengths differ (found for 'input$variable')"

I would appreciate any help in trying to see how can this mean mpg by input variable be implemented in Shiny

1

1 Answers

0
votes

I managed to figure out this one.

Changed the server.R to:

output$Avg_Mileage <- renderTable({aggregate(as.formula(formulaText()), carsdata,mean)})

and ui.R to:

tableOutput(outputId = "Avg_Mileage")