Shiny not displaying ggplot data

China☆狼群 提交于 2021-01-30 09:10:20

问题


I am new to working with shiny package. I am trying to use it to display a ggplot2 graph. I get no errors with my code, however data points are not appearing on the graph. When I select the variables from ui, the axes labels changes accordingly but the data is not added to the plot.

Thank you,

Code:

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:


The issue is that you have to tell ggplot that your inputs are names of variables in your dataset. This could be achieved e.g. by making use of the .data pronoun, i.e. instead of using input$x which is simply a string use .data[[input$x]] which tells ggplot that by input$x you mean the variable with that name in your data:

As you provided no data I could not check but this should give you the desired result:

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)


来源:https://stackoverflow.com/questions/64620046/shiny-not-displaying-ggplot-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!