Using plotmeans or geom_bar for a Shiny web page

Good day

I am trying to plot the means and 95% confidence intervals for my shiny webpage but I can't seem to get it right. I am fairly new to writing Shiny code so I apologize if this is a very low level question.

I would like output similar to this
enter image description here

I have tried two methods

  1. Using geom_errorbar
    Here I tried creating a summary table that calculates the 95% CI and then plotting from there.
    My code follows
ui <- fluidPage(
  titlePanel("questionnaire"),
  sidebarLayout(
    sidebarPanel(
      selectInput("question", "Choose a question",
                  colnames(Data[,3:(ncol(Data)-1)]))
    ),
    mainPanel(
      plotOutput("meanCI")
    )
  )
)

server <- function(input, output) {
  
  ci <- reactive({
    groupwiseMean(input$question ~ Date,
                  data   = Data,
                  conf   = 0.95,
                  digits = 3)
  })
  output$meanCI <- renderPlot(
    ggplot(ci, aes(x=Date, y=Mean)) + 
      geom_errorbar(aes(ymin=Trad.lower, ymax=Trad.upper), width=.1) +
      geom_point()
    
  )


}

shinyApp(ui = ui, server = server)

But it gives me this error,

data must be a data frame, or other object coercible by fortify(), not an S3 object with class reactiveExpr/reactive

  1. Option 2 was to use plotmeans from the gplot package

ui <- fluidPage(
  titlePanel("questionnaire"),
  sidebarLayout(
    sidebarPanel(
      selectInput("question", "Choose a question",
                  colnames(Data[,3:(ncol(Data)-1)]))
    ),
    mainPanel(
      plotOutput("meanCI")
    )
  )
)

server <- function(input, output) {
  output$meanCI <- renderPlot(
plotmeans(input$question~Data$Date, connect = FALSE)
    
  )


}

shinyApp(ui = ui, server = server)

But it results is this error,

variable lengths differ (found for 'Data$Date')

Any help will be greatly appreciated!

HI @Susanl3p

Regarding your ggplot(...) implementation. You are very close to getting it to work, but you have one small issue with your server-side code. When you assign a value/values to a reactive(), you then must call that reactive like a function when you use it in some calculation or plot.

Assuming groupwiseMean() returns a data.frame object. Then when you use ci in your ggplot() call, you need to refer to it as ci(), see below.

server <- function(input, output) {
  
  ci <- reactive({
    groupwiseMean(input$question ~ Date,
                  data   = Data,
                  conf   = 0.95,
                  digits = 3)
  })
  output$meanCI <- renderPlot(
    ggplot(ci(), aes(x=Date, y=Mean)) + 
      geom_errorbar(aes(ymin=Trad.lower, ymax=Trad.upper), width=.1) +
      geom_point()
    
  )


}

Ooh ok thank you.

I did that now and got this error

undefined columns selected

I commented out the renderPlot part and attempted to just display the table using tableOutput and got the same error.
Code below

    ci <- reactive({
        groupwiseMean(input$question ~ Date,
                      data   = Data,
                      conf   = 0.95,
                      digits = 3)
    })
    
    output$summary <- renderTable(
        ci()
    )

So the mistake has to be in the reactive variable.
Do you know why I am getting this error?

I made a boxplot using the same column names and it worked so I'm not sure why it's not working here.

I also tried using get(input$question) but it made no difference.

My guess would be that groupwiseMean() is trying to find input$question inside Data since you are using a formula method in the function call.

Try something like:

ci <- reactive({
        qq <- input$question
        Data <- dplyr::mutate(Data, question = qq)

        groupwiseMean(question ~ Date,
                      data   = Data,
                      conf   = 0.95,
                      digits = 3)
    })
    
    output$summary <- renderTable(
        print(ci()) # will show the data in the console for easier debugging
        ci()
    )

I did this and I think it’s so close to working. I don’t get errors anymore but the mean and CI is not being calculated. There are NA’s in the data frame. I made sure that all my values are in fact numeric so what else could be causing this ?

Without your data its hard for me to say. Try getting the groupwiseMean() function to work on your data outside of the shiny app. Once working, then try to re-factoring the code to work inside the app. Sounds like the shiny parts are working correctly based on your feedback. You just need to make sure the calculations are doing what you expect.

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.