Shiny and ggplot problem

Hi,
I have a problem with creating ggplot in Shiny. I would like to build a plot, in which I could select x and y variable, so I use input$xcol and input$ycol, however I doesn't work properly. Do you know how could I resolve this problem?

Thanks for your help!

ui <- fluidPage(
pageWithSidebar(
headerPanel('Data'),
sidebarPanel(
selectInput('xcol', 'Choose x', names(rdatabase)),
selectInput('ycol', 'Choose y', names(rdatabase),
selected=names(rdatabase)[[2]]),
numericInput('clusters', 'Number of clusters', 3,
min = 1, max = 9)
    ),
    
mainPanel(
plotOutput('plot1'),
plotOutput('plot2'),
plotOutput('plot3'),
plotOutput('plot4')
    )
  )
)

server <- function(input, output, session) {
  
selectedData <- reactive({rdatabase[, c(input$xcol, input$ycol)]})
  
clusters <- reactive({kmeans(selectedData(), input$clusters)})

#plot4
output$plot4 <- renderPlot({
ggplot(selectedData(), aes(input$xcol, input$ycol)) + 
  geom_point(color='blue') +
  geom_smooth(method = "lm", se = FALSE)
})
  
}
shinyApp(ui=ui, server=server)

image

You have to use tidy evaluation e.g.

output$plot4 <- renderPlot({
    ggplot(selectedData(), aes(!!as.name(input$xcol), !!as.name(input$ycol))) +
    geom_point(color='blue') +
    geom_smooth(method = "lm", se = FALSE)
}
3 Likes

Thanks a lot!!! :slight_smile:

1 Like

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