简体   繁体   中英

develop reactive scatter plot using shiny in R

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 eg 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.

I updated the server code to what seems like what you are intending.

pt1 should require what you need to filter your data to prevent warnings.

input$a does not return "a" but instead TRUE - so to filter on a, you could include something like a vector with your checkbox choices a, b, c, and d and subset based on inputs (see below). It also checks to make sure only 2 checkboxes are selected (sum is 2). Depending on your needs this can all be simplified. You might want to include a checkboxGroupInput instead which will return a character vector of selections instead.

As you can have a combination of multiple checkboxes for a through d, use %in% instead of == .

output$scatter will get the filtered data by calling the reactive function pt1 . It will determine which 2 variables are present in subsetted data, and plot 1 versus the other.

Hope this is helpful.

s <- function(input, output) {
  pt1 <- reactive({
    req(input$yearRef)
    if (sum(input$a, input$b, input$c, input$d) == 2) {
      dplyr::filter(dat_l, variable %in% c("a"[input$a], "b"[input$b], "c"[input$c], "d"[input$d]) & 
                    year == input$yearRef)
    } else {
      return(NULL)
    }
  })

  output$scatter = renderPlot({
    pdata <- pt1()
    pvars <- unique(pdata$variable)
    if (!is.null(pdata)) {
      plot(pdata[pdata$variable == pvars[1], ]$value,
           pdata[pdata$variable == pvars[2], ]$value,
           xlab = pvars[1], ylab = pvars[2])
    }
  })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM