Search code examples
rshinyr-corrplot

Bring out correlation with a double-ended range slider


i would like to bring out correlation between several biomarkers using a double-ended ranger slider. However, only the first ended ranger slider works. How can I fix the problem ? Thanks

library(shiny)
library(corrplot)

# Dataset
df <- data.frame(BM1 = c(30, 3, 34, 57, 100, 475),
             BM2 = c(0.04, 0.9, 2, 4, 2.23, 3),
             BM3 = c(2, 3, 4, 3, 10, 42),
             BM4 = c(1, 1.1, 2, 4, 2, 3),
             BM5 = c(3000, 30, 304, 507, 1000, 4075),
             BM6 = c( 0.043, 20.9, 27, 84, 2.273, 63),
             BM7 = c(304, 32, 34, 57, 100, 4753),
             BM8 = c( 0.004, 10.9, 20, 4, 2.23, 31),
             BM9 = c(301, 13, 314, 571, 10, 47),
             BM10 = c( 0.24, 0.93, 12, 42, 23, 3000))


ui <- fluidPage(
  titlePanel("Bring out correlation between several biomarkers"),
  sidebarLayout(
    sidebarPanel(
      sliderInput(inputId = "slide0",
                  label = "Choose the number of variables",
                  min = 2, 
                  max = 10,
                  value = c(1,10),
                  step = 1
       )
      ),
    mainPanel(
      plotOutput(outputId = "pearson")
    )
  )
 )

server <- function(input, output, session) {
  output$pearson <- renderPlot({
    corr <- cor(df[,1:input$slide0], method = "pearson", use  = "pairwise.complete.obs")
    corrplot(corr, type = "lower", method = "circle", tl.col = "black", tl.srt = 45)
  })
 }

Here, an example

EXPECTATION enter image description here

REALITY enter image description here


Solution

  • Something like this?

    library(shiny)
    library(corrplot)
    
    # Dataset
    df <- data.frame(BM1 = c(30, 3, 34, 57, 100, 475),
                     BM2 = c(0.04, 0.9, 2, 4, 2.23, 3),
                     BM3 = c(2, 3, 4, 3, 10, 42),
                     BM4 = c(1, 1.1, 2, 4, 2, 3),
                     BM5 = c(3000, 30, 304, 507, 1000, 4075),
                     BM6 = c( 0.043, 20.9, 27, 84, 2.273, 63),
                     BM7 = c(304, 32, 34, 57, 100, 4753),
                     BM8 = c( 0.004, 10.9, 20, 4, 2.23, 31),
                     BM9 = c(301, 13, 314, 571, 10, 47),
                     BM10 = c( 0.24, 0.93, 12, 42, 23, 3000))
    
    
    ui <- fluidPage(
      titlePanel("Bring out correlation between several biomarkers"),
      sidebarLayout(
        sidebarPanel(
          sliderInput(inputId = "slide0",
                      label = "Choose the number of variables",
                      min = 2, 
                      max = 10,
                      value = c(1,10),
                      step = 1
          )
        ),
        mainPanel(
          plotOutput(outputId = "pearson")
        )
      )
    )
    
    server <- function(input, output, session) {
      output$pearson <- renderPlot({
        corr <- cor(df[,input$slide0[1]:input$slide0[2]], method = "pearson", use  = "pairwise.complete.obs")
        corrplot(corr, type = "lower", method = "circle", tl.col = "black", tl.srt = 45)
      })
    }
    shinyApp(ui=ui,server=server)