UiOutput-question

Dear community,
I have begun writing Shiny apps with the code below (my first project). I have the following problem: If I use "ggplot(pcr, aes(x=Gene, y=input$Y_ax))", the boxplot does not appear correctly, while when I explicitly use ggplot(pcr, aes(x=Gene, y=Ct)), then the box plot is generated as expected. The observe-Function indeed tells me that input$Y_ax is "Ct".
Does anyone have a hint what I am doing wrong here?
thanks a lot
Cyrus

ui <- fluidPage(
titlePanel("Boxplot"),
sidebarLayout(
sidebarPanel(
uiOutput("Y_achse")),
mainPanel(
plotOutput("box"))
))

server <- function(input, output, session) {
output$Y_achse <- renderUI({
selectInput("Y_ax","Y-axis",choices= names(pcr), selected = "Ct")
})

observe({ print(input$Y_ax) })

output$box <- renderPlot({

ggplot(pcr, aes(x=Gene, y=input$Y_ax)) + 
  geom_boxplot(notch = FALSE)
})}

PS: The test data looks like this:

str(pcr)
'data.frame': 601 obs. of 6 variables:
Gene : Factor w/ 22 levels "14-3-3e","Act5C",..: 1 1 1 1 1 1 1 1 1 1 ... Sample: Factor w/ 4 levels "A05","C05","Std",..: 1 1 1 1 1 1 2 2 2 2 ...
Rep : int 1 1 2 2 3 3 1 1 2 2 ... Type : Factor w/ 2 levels "Std","Unkn": 2 2 2 2 2 2 2 2 2 2 ...
Serial: int NA NA NA NA NA NA NA NA NA NA ... Ct : num 16.2 16.2 16.4 16.5 16.6 ...
head(pcr)
Gene Sample Rep Type Serial Ct
1 14-3-3e A05 1 Unkn NA 16.24
2 14-3-3e A05 1 Unkn NA 16.24
3 14-3-3e A05 2 Unkn NA 16.42
4 14-3-3e A05 2 Unkn NA 16.46
5 14-3-3e A05 3 Unkn NA 16.64
6 14-3-3e A05 3 Unkn NA 16.76

When you call input$Y_ax you get a character string (i.e. "Ct") , so, basically you are passing to ggplot a character string for Y axis rather than a variable name, the correct way is to use tidy evaluation !!as.name(input$Y_ax)

2 Likes

Yes, works!! Thanks so much for your help!!!!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.