Silvio Ortiz
Silvio Ortiz

Reputation: 58

I can't make my x axis reactive using ggplot in shiny

For simplicity I will only include the parts of the code that I am having issues with. This code pulls a large data set from a data base and creates tabs with different analyses. I am having issues with making my x variable reactive for ggplot graphs. If I define the x variable inside aes() the plot shows up fine but once I create a reactive input that substitutes my variable then ggplot has troubles defining the x variable. The variables I want to be able to switch are: "assay_plate_label" and "assay_read_on" from my data set.

ui.r

library(shiny)


shinyUI(fluidPage(
# Header or Title Panel 
titlePanel(title = h4("HTS QC analysis", align="center")),
sidebarLayout(
# Sidebar panel
sidebarPanel(

  numericInput("pg_number", "Plate Group", 4552),
  actionButton('process', 'Process Plate Group Data')

),


# Main Panel
mainPanel(
  tabsetPanel(type="tab", 
tabPanel("Box Plot All", 
                       selectInput('x_axis', "Choose the X axis", choices= c("Assay Plate Label"= "assay_plate_label", "Assay Date"= "assay_read_on")),
                       plotOutput("deviation"))


    )

   )

  )
) 
)

server.r

 shinyServer(function(input, output){



  x_label <- reactive({
  switch(input$x_axis,
         "assay_plate_label" = assay_plate_label,
         "assay_read_on"=  assay_read_on)
 })



  read.csv("table_na")
 [Here is a sample of the data with the variables I use in this plot ][1]




 output$deviation <- renderPlot({


  table_na$assay_plate_label <- as.character(table_na$assay_plate_label)
  ggplot(table_na, aes(x_label(), y=raw_assay_value, group=as.character(x_label()))) + 
    geom_boxplot() +
    theme(axis.text.x = element_text(angle = 90, hjust = 3, vjust= 0)) +
    xlab("Plate Label")+
    ylab(table_na$plate_type_name)+
    geom_boxplot(outlier.colour = "red", outlier.size= 1.5)+
    ggtitle("All Plates Box Plot")


})
 })



 }
 )

I had to cut down on most of the unnecessary code to make it understandable, but the code that relates to the problem it self is all here.

Upvotes: 0

Views: 722

Answers (1)

Andrew Chisholm
Andrew Chisholm

Reputation: 6567

Difficult to know without being able to run the code, but try aes_string instead of aes.

Upvotes: 2

Related Questions