---
title: "Report con OJS"
author: "DV"
format: 
  html:
    self-contained: true
editor: visual
---

## Un esempio grafico in R

```{r}
#| message: false
library(gapminder)
library(tidyverse)
gap_last <- gapminder |> 
  filter(year == 2007, continent != "Oceania") |> 
  mutate(continent = as.character(continent))
```

```{r}
gap_last |> 
#  filter(continent == "Europe") |> 
  ggplot() +
    aes(x = gdpPercap, y = lifeExp) +
    geom_point() +
    labs(title = "Scatterplot gdpPercap vs lifeExp",
         x = "Gross Domestic Product (per capite)",
         y = "Life Expectancy (years)")
```


## Lo stesso esempio in OJS

Questo è un banale codice ottenuto sfruttando `OJS`:
```{ojs}
2 + 2
```

```{r}
ojs_define(gap_last = gap_last |> transpose())
```

```{ojs}
Plot.plot({
  title: "Scatterplot gdpPercap vs lifeExp",
  marginLeft: 56,  // per lasciare spazio alle etichette dell'asse Y
  marks: [
    Plot.dot(gap_last, {
      x: "gdpPercap",
      y: "lifeExp",
      r: 3,           // raggio dei punti
      fill: "#1f77b4" // colore
    })
  ],
  x: {label: "Gross Domestic Product (per capite)"},
  y: {label: "Life Expectancy (years)"}
})
```

```{ojs}
viewof color = Inputs.select(["red", "orange", "yellow", "green", "blue", "violet"], {value: "green"})
```

```{ojs}

// Ottieni i valori distinti dalla colonna "continent"
continents = Array.from(new Set(gap_last.map(d => d.continent)))

// Crea la casella a discesa con i valori unici
viewof continent = Inputs.select(continents, {label: "Seleziona continente", value: continents[0]})
```

```{ojs}
gap_continent = gap_last.filter(d => d.continent === continent)
```

```{ojs}
Plot.plot({
  title: "Scatterplot gdpPercap vs lifeExp",
  marginLeft: 56,  // per lasciare spazio alle etichette dell'asse Y
  marks: [
    Plot.dot(gap_continent, {
      x: "gdpPercap",
      y: "lifeExp",
      r: 3,           // raggio dei punti
      fill: "#1f77b4" // colore
    })
  ],
  x: {label: "Gross Domestic Product (per capite)"},
  y: {label: "Life Expectancy (years)"}
})
```
