首页 > 解决方案 > 在 Shiny 中绘制两个数据框中的列

问题描述

我想针对 df2.t 中的列绘制 df1.t 中的列。以前我只有一个数据框,但现在我将数据拆分为两个数据框。我不确定如何server正确完成从不同数据框中选择列。请看我下面的例子

> dput(df1.t[1:5,][,1:5])
structure(list(`hsa-let-7a-3p` = c(5.58182112427671, 5.21705272399953, 
5.42864356356758, -1.1383057411356, 5.06203248358181), `hsa-let-7a-5p` = c(17.0260439263402, 
15.2857710138151, 17.1420214989373, 15.1034766165351, 14.5449390552056
), `hsa-let-7b-3p` = c(4.28580929310353, 2.46805733598209, 5.15298557165018, 
4.63298501632773, -0.398732335974934), `hsa-let-7b-5p` = c(13.0477955183433, 
10.880357260433, 12.2652935281359, 11.1312184397251, 7.45844929748327
), `hsa-let-7c-5p` = c(12.5551466619424, 9.6650262124332, 12.1037832874061, 
9.557036296907, 9.92698639082262)), class = "data.frame", row.names = c("86", 
"175", "217", "394", "444"))


> dput(df2.t[1:5,][,1:5])
structure(list(TSPAN6 = c(-0.828323126096606, -3.10305950914023, 
1.29283167997387, 1.45789964523008, 2.5865078028694), TNMD = c(-3.10305950914023, 
-2.24464338564074, -3.10305950914023, -2.40005739936056, -3.10305950914023
), DPM1 = c(5.28259829784066, 4.78188654848771, 4.64737618644607, 
5.30924702614244, 5.31267531367151), SCYL3 = c(2.87362293573059, 
4.28995424523396, 1.90557669028164, 3.40137165784651, 2.31237762728826
), C1orf112 = c(1.06700120906004, 4.32783509690622, 0.330332820167606, 
0.442181000111075, 2.50079103019751)), class = "data.frame", row.names = c("86", 
"175", "217", "394", "444"))



ui <- fluidPage(
  
  mainPanel(
    plotOutput("plot")
  ),
  
  selectInput(inputId ="data1",
              label = "Choose miRNA",
              choices = names(df1.t),
              selected = NULL
              
  ),
  selectInput(inputId ="data2",
              label = "choose Gene",
              choices = names(df2.t),
              selected = NULL
              
  ),
  textOutput("result"))


server <- function(input,output){
  library(ggplot2)
  output$plot <- renderPlot({
    data <- plot[, c(input$data1, input$data2)] #Needs edit
    colnames(data) <- c("col1", "col2") #Needs edit
    ggplot(data,aes(x=col1,y=col2)) + 
      geom_point(colour='black') +
      labs(x = input$data1, y = input$data2) +
      theme_classic(base_size = 8) +
      geom_smooth(method="lm",se = F) +
      stat_cor()
      
  }, height = 400, width = 600)
  
}

标签: ggplot2shiny

解决方案


这应该有效:

library(shiny)
library(ggplot2)

df1.t <- structure(list(`hsa-let-7a-3p` = c(5.58182112427671, 5.21705272399953, 
                                   5.42864356356758, -1.1383057411356, 5.06203248358181), `hsa-let-7a-5p` = c(17.0260439263402, 
                                                                                                              15.2857710138151, 17.1420214989373, 15.1034766165351, 14.5449390552056
                                   ), `hsa-let-7b-3p` = c(4.28580929310353, 2.46805733598209, 5.15298557165018, 
                                                          4.63298501632773, -0.398732335974934), `hsa-let-7b-5p` = c(13.0477955183433, 
                                                                                                                     10.880357260433, 12.2652935281359, 11.1312184397251, 7.45844929748327
                                                          ), `hsa-let-7c-5p` = c(12.5551466619424, 9.6650262124332, 12.1037832874061, 
                                                                                 9.557036296907, 9.92698639082262)), class = "data.frame", row.names = c("86", 
                                                                                                                                                         "175", "217", "394", "444"))


df2.t <- structure(list(TSPAN6 = c(-0.828323126096606, -3.10305950914023, 
                          1.29283167997387, 1.45789964523008, 2.5865078028694), TNMD = c(-3.10305950914023, 
                                                                                         -2.24464338564074, -3.10305950914023, -2.40005739936056, -3.10305950914023
                          ), DPM1 = c(5.28259829784066, 4.78188654848771, 4.64737618644607, 
                                      5.30924702614244, 5.31267531367151), SCYL3 = c(2.87362293573059, 
                                                                                     4.28995424523396, 1.90557669028164, 3.40137165784651, 2.31237762728826
                                      ), C1orf112 = c(1.06700120906004, 4.32783509690622, 0.330332820167606, 
                                                      0.442181000111075, 2.50079103019751)), class = "data.frame", row.names = c("86", 
                                                                                                                                 "175", "217", "394", "444"))



ui <- fluidPage(
    
    mainPanel(
        plotOutput("plot")
    ),
    
    selectInput(inputId ="data1",
                label = "Choose miRNA",
                choices = names(df1.t),
                selected = NULL
                
    ),
    selectInput(inputId ="data2",
                label = "choose Gene",
                choices = names(df2.t),
                selected = NULL
                
    ),
    textOutput("result"))


server <- function(input,output){
    
    data <- eventReactive(c(input$data1,input$data2),{
        data <- data.frame(df1.t[[input$data1]], df2.t[[input$data2]])
        colnames(data) <- c("col1", "col2")
        data
    })
    
    output$plot <- renderPlot({
        ggplot(data(),aes(x=col1,y=col2)) +
            geom_point(colour='black') +
            labs(x = input$data1, y = input$data2) +
            theme_classic(base_size = 8) +
            geom_smooth(method="lm",se = F)
        
    }, height = 400, width = 600)
    
}

shinyApp(ui, server)

在此处输入图像描述


推荐阅读