---
title: ""
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
#setwd("~/Documents/my_papers/number/subitizing_info_theory/nature_human_behavior/shiny_tutorial/ex2")
library(shiny)
library(shinythemes)
library(ggplot2)
knitr::opts_chunk$set(echo = TRUE)
```


```{r, echo=FALSE}
#library(ggExtra)

paper_theme <- theme_light() + theme( axis.title.x = element_text(size=18),
  axis.text.x=element_text(colour="black", 
                           size = 14), 
  axis.title.y = element_text(size = 18, vjust = 1),
  axis.text.y  = element_text(size = 14),
  strip.text=element_text(size=16),
  axis.line.x = element_line(colour = "black"), 
  axis.line.y = element_line(colour = "black"),
  legend.title=element_text(size=18),
  legend.text=element_text(size=16))  

#c1 <- brewer.pal(n=10, "PuBuGn")[4:8]

df <- read.csv("o1.csv")
df <- subset(df, df$alpha == 2)

unique_info <- unique(df$info)
info_step <- (unique_info[2] - unique_info[1])



```

```{r,echo=FALSE}

ui <- fluidPage(

  titlePanel(""),
  theme = shinytheme("united"),

  fluidRow(
  
    column(3,
      wellPanel(
        sliderInput(inputId = "info",
                  label = "Bits:",
                  min = min(df$info),
                  max = max(df$info),
                  value = min(df$info),
                  step = info_step)
      )       
    ),

    column(8,
      plotOutput(outputId = "distPlot",width="400px",height="250px")
    )
  )
)

```



```{r,fig.width=5,fig.height=4,echo=FALSE}
server <- function(input, output) {


  output$distPlot <- renderPlot({
    data <- subset(df, (df$info==input$info))
    
    ggplot(data=data,aes(x=k,y=Q_kn,group=n,color=n)) +
        geom_line() +
        paper_theme + xlab("Estimate") + ylab("Probability") +
        scale_color_gradientn(colors=c("blue","red","orange")) +
        coord_cartesian(xlim=c(min(df$k),max(df$k)),ylim=c(0,1)) 
      })
  
  
  
}



```


```{r,echo=FALSE}
shinyApp(ui,server)

```

