利用双端范围滑块带来相关性

时间:2018-09-20 08:05:59

标签: r shiny r-corrplot

我想使用双头游标滑块来显示多个生物标记之间的相关性。但是,仅第一个结束的游侠滑块起作用。我该如何解决该问题?谢谢

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)
  })
 }

这里是一个示例

期望 enter image description here

真实性 enter image description here

1 个答案:

答案 0 :(得分:3)

像这样吗?

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)