闪亮不显示ggplot数据

时间:2020-10-31 09:01:41

标签: r ggplot2 shiny

我刚接触过闪亮的包装。我正在尝试使用它来显示ggplot2图。我的代码没有错误,但是数据点未出现在图形上。当我从ui中选择变量时,轴标签会相应更改,但数据不会添加到绘图中。

谢谢

代码:

ui <- fluidPage(
     sidebarLayout(
         sidebarPanel(
             selectInput(inputId = "y", 
                         label = "Y-axis:", 
                         choices = c("P-value", "P-adjust"),
                         selected = "P-adjust"),
             selectInput(inputId = "x" , 
                         label = "X-axis:", 
                         choices = c("FC", "Mean_Count_PD"),
                         selected = "FC")
                 ),
         mainPanel(plotOutput(outputId = "scatterplot"))
         ))

server <- function(input, output) 
     {
     output$scatterplot <- renderPlot({
     ggplot(data = mir, aes(input$x,input$y)) + geom_point()
     })
 }

1 个答案:

答案 0 :(得分:1)

问题在于,您必须告诉ggplot input是数据集中变量的名称。例如,这可以实现。通过使用.data代词,即不是使用简单的字符串input$x而是使用.data[[input$x]]告诉ggplot,input$x的意思是带有该名称的变量您的数据:

由于您没有提供任何数据,所以我无法检查,但这应该会为您提供所需的结果:

library(shiny)
library(ggplot2)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "y", 
                  label = "Y-axis:", 
                  choices = c("P-value", "P-adjust"),
                  selected = "P-adjust"),
      selectInput(inputId = "x" , 
                  label = "X-axis:", 
                  choices = c("FC", "Mean_Count_PD"),
                  selected = "FC")
    ),
    mainPanel(plotOutput(outputId = "scatterplot"))
  ))

server <- function(input, output) {
  output$scatterplot <- renderPlot({
    ggplot(data = mir, aes(.data[[input$x]], .data[[input$y]])) + geom_point()
  })
}

shinyApp(ui, server)