【问题标题】:Plot columns from two data frams in Shiny在 Shiny 中绘制两个数据框中的列
【发布时间】:2020-07-06 20:22:05
【问题描述】:

我想绘制 df1.t 中的列与 df2.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)
  
}

【问题讨论】:

    标签: ggplot2 shiny


    【解决方案1】:

    这应该可行:

    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)
    

    【讨论】:

    • 我收到了这个错误:输出$plot 中的错误
    • U 应该加载library(ggplot2),其中library(shiny) 在顶部,而不是每次启动会话时
    • 嗯。我不明白。你能在例子中展示它吗?
    • library(shiny); library(ggplot2)
    • 同样的错误。我想我必须将 output$plot 放入一个函数并放入shinyApp?您能否尝试使答案可重现?
    猜你喜欢
    • 2021-04-29
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多