我在闪亮应用程序中的ggplot将不会显示数据表中的值,而只会显示一个点

时间:2018-11-29 12:40:59

标签: r shiny

好吧,我敢肯定,这是我错过的一些显而易见的事情,但是我对R和创建闪亮的应用程序都是陌生的,但是我正试图自学它们两个。

我有一个RNA值数据表(来自约150个供体的> 20,000个基因),我试图创建一个简单的闪亮应用程序以从列表中选取2个基因,并显示所有基因表达值的散点图150个捐助者互相反对。后来我想对数据做更多的事情,但是我正努力让这个简单的代码先运行!

以下代码是我的应用程序的当前简单状态。它仍然非常简单,但是结果图只显示每个基因的单个点,即代码下方的图像。当我要显示的是表格中相对值的散点图

下面是数据集的截短版本(仍会产生相同的错误)以及str()结果。

library(shiny)
library(ggplot2)
GeneNames <- colnames(Short_cortex5)
ui <- fluidPage(
   sidebarLayout(
      sidebarPanel(
         selectizeInput(inputId = "gene_X",
                     label = "Select gene X:",
                     choices = GeneNames),

         selectizeInput(inputId = "gene_Y",
                        label = "Select gene Y:",
                        choices = GeneNames)
      ),

      mainPanel(
         plotOutput(outputId = "corrPlot")
      )
   )
)

server <- function(input, output) {

  output$corrPlot <- renderPlot({
    ggplot(data = Short_cortex5, aes(x=input$gene_X, y=input$gene_Y)) +
      geom_point()
   })
}

shinyApp(ui = ui, server = server)

enter image description here

> str(Short_cortex5)
'data.frame':   8 obs. of  8 variables:
 $ MARC2 : num  15.3 16.1 17.3 11.4 21.8 ...
 $ MARCH1: num  2.5 4.7 2.46 3.11 6.12 ...
 $ SEPT1 : num  1.068 0.298 0.381 0.555 0.756 ...
 $ DEC1  : num  0.0261 0 0 0.0226 0 ...
 $ MARCH2: num  81.2 68.9 63.4 84.5 88.5 ...
 $ SEPT2 : num  66.1 89 65.2 49 106.2 ...
 $ MARCH3: num  1.756 1.348 1.25 0.451 2.137 ...
 $ SEPT3 : num  103 208 190 202 223 ...
> Short_cortex5
  MARC2 MARCH1  SEPT1    DEC1 MARCH2  SEPT2 MARCH3 SEPT3
1 15.30  2.501 1.0680 0.02610  81.15  66.14 1.7560 103.3
2 16.07  4.700 0.2980 0.00000  68.90  88.95 1.3480 207.7
3 17.27  2.462 0.3812 0.00000  63.41  65.20 1.2500 190.5
4 11.37  3.107 0.5550 0.02261  84.53  48.98 0.4507 201.9
5 21.76  6.123 0.7558 0.00000  88.49 106.20 2.1370 223.0
6 15.55  4.239 0.5859 0.03581  58.91  69.80 0.5515 214.1
7 16.74  4.596 0.6551 0.02627  70.38  68.50 0.8489 224.4
8 11.48  2.167 0.3567 0.01090  60.14  74.04 1.1210 164.9

3 个答案:

答案 0 :(得分:3)

您的应用当前正在做的是相互绘制基因名称,因此您只能得到1分。

您想告诉R使用与该名称相对应的变量并将其绘制。最简单的方法是如下使用Non-standard evaluation

require(rlang)

output$corrPlot <- renderPlot({
    ggplot(data = Short_cortex5, aes(x=!!sym(input$gene_X), y=!!sym(input$gene_Y))) +
      geom_point()
  })

编辑:

根据其他答复,这可能不是我所说的最简单的方法!

我想从ggplot2 V3.0.0的{​​{3}}中注意到:

  

aes()现在支持准引号,因此您可以使用!!,!!!和   :=。这将替换现在的aes_()和aes_string()   不推荐使用(但会保留很长时间)。

答案 1 :(得分:2)

嗨,您需要像这样在ggplot调用中将标准aes更改为aes_string

server <- function(input, output) {

  output$corrPlot <- renderPlot({
    ggplot(data = Short_cortex5, aes_string(x=input$gene_X, y=input$gene_Y)) +
      geom_point()
   })
}

这是因为输入的值是字符向量

希望这会有所帮助!

答案 2 :(得分:1)

您可以只使用get,它非常通用,并不特定于ggplot,并且可以用于您可能拥有的其他图表。

  output$corrPlot <- renderPlot({
    ggplot(data = mtcars, aes(x=get(input$gene_X), y=get(input$gene_Y))) +
      geom_point()
  })
相关问题