Minor error occurring in annotate function in ggplot

Hi all,

I am trying to include annotate function to my plot. So basically, as per the filtering under First variable and Second Variable, I have to calculate mean(First Variable)/mean(Second Variable). So i have included them in my code annotate("text",x=10,y=10,label = round(mean(plot_data$input$b)/mean(plot_data$input$a),2)) but I am sure there is minor mistake. But I am not able to solve this. Please guide.

Sample code below

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: scroll
    runtime: shiny
    theme: cosmo
---
```{r setup, include=FALSE}
library(shiny)
library(flexdashboard)
library(tidyverse)
```
```{r}
Copy_of_mill_para <- structure(list(Date = structure(c(1505779200, 1505779500, 1505779800, 
1505780100, 1505780400, 1505780700, 1505781000, 1505781300, 1505781600
), class = c("POSIXct", "POSIXt"), tzone = "UTC"), A = c(42, 
40, 41, 45, 25, 39, 44, 25, 39), B = c(27, 36, 40, 31, 44, 34, 
39, 44, 41), C = c(39, 42, 33, 26, 29, 42, 24, 34, 35)), row.names = c(NA, 
-9L), class = "data.frame")
Copy_of_mill_para1 <- Copy_of_mill_para %>% 
    gather(variable, value, -Date)
```
Summary
=================
Inputs {.sidebar}
-----------------------------------------------------------------------
```{r}
selectInput("c", "Filter1", choices = c("","Correlation"))
output$filter_2 <- renderUI({
    if (input$c == "") {
      return()
    } else {
      label = "First Variable"
      selectInput("b",
                label,
                choices = c(levels(factor(Copy_of_mill_para1$variable))))
    }
  })
output$filter_3 <- renderUI({
    if (input$c == "") {
      return()
    } else {
      selectInput("a",
                "Second Variable",
                choices = c(levels(factor(Copy_of_mill_para1$variable))))
    }
  })
uiOutput("filter_2")
uiOutput("filter_3")
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart A
```{r}
output$g1 <- renderPlot({
    req(input$c)
    if (input$c == "Correlation"){
        plot_data <- Copy_of_mill_para
    }
        if (input$c == "Correlation") {
        req(input$a)
        req(input$b)
        ggplot(plot_data, aes_string(x = input$b, y = input$a)) +
            geom_point() + annotate("text",x=10,y=10,label = round(mean(plot_data$input$b)/mean(plot_data$input$a),2))
    }
})
plotOutput("g1")
```

You are using the wrong syntax, this is basic data frame subsetting, see this example

input <- NULL
input$a <- "Sepal.Width"
mean(iris[[input$a]])
#> [1] 3.057333

I think you need to work a little bit in your basic R skills before jumping into shiny, I recommend you to read this free ebook

1 Like

Thanks for the suggestion. Will have a look at it

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