Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

develop reactive scatter plot using shiny in R

$
0
0

I have 10 years of data of four variables. Here's how data looks like

dat <- data.frame(year = rep(2001:2010, each = 50),
                  a = rnorm(50), b = rnorm(50), c = rnorm(50), d = rnorm(50))

dat_l <- tidyr::gather(dat, variable, value, a:d)

I am trying to learn shiny app. I want my app to plot one variable against the other for a given year For e.g. if user selects a and b and year 2001, plot a against b for a year 2001. If b and c are selected for year 2003, plot b vs c for 2003 and so on. Also do not do anything if a single variable or more than 2 variable are selected for a given year.

library(shiny)

u <- fluidPage(
     titlePanel('My analytics'),
     sidebarLayout(position = 'left',
                   sidebarPanel(
                   checkboxInput('a', 'aRef', value = F),
                    checkboxInput('b', 'bRef', value = F),
                    checkboxInput('c', 'cRef', value = F),
                    checkboxInput('d', 'dRef', value = F),
                    sliderInput('yearRef','Select Year',min=2001,max=2010,value=1)
                  ),

                  mainPanel(
                    tabsetPanel(
                      tabPanel('Scatter', plotOutput(outputId = 'scatter'))
                    )
                  )
    )
  )


  s <- shinyServer(function(input, output) 
  {
    pt1 <- reactive({
      if (!input$a) return(NULL)
      pdata <- dplyr::filter(dat_l, variable == input$a and year == input$yearRef)
      plot(pdata$value, pdata$value)
    })
    output$scatter = renderPlot({pt1})
  })


shinyApp(u,s)

This code is incomplete and I hope I could get some help on how to develop the server part of the code.


Viewing all articles
Browse latest Browse all 201839

Trending Articles