Home > Enterprise >  How to develop a shiny app that produces a scatterplot based on the 1st and 2nd column names of a sp
How to develop a shiny app that produces a scatterplot based on the 1st and 2nd column names of a sp

Time:10-10

I want to create a shiny app that takes a dataset name and produces a scatterplot where the x axis is the 1st column and y axis is the second column in the dataset.

So far I have tried this:

library(shiny)
library(ggplot2)

ui = fluidPage(
        textInput("dataset", "Please enter dataset name"),
        plotOutput("plot")
)

server = function(input, output, session) {
        output$plot = renderPlot({
                req(input$dataset)
                data = get(input$dataset, "package:datasets")
                ggplot(data, aes(names(data)[1], names(data)[2]))   geom_point()
        })
}

shinyApp(ui, server)

However, it does not do the job. For example, when I type the dataset name iris, I get this:

enter image description here

CodePudding user response:

The aes part can be

library(shiny)
library(ggplot2)

ui = fluidPage(
        textInput("dataset", "Please enter dataset name"),
        plotOutput("plot")
)

server = function(input, output, session) {
        output$plot = renderPlot({
                req(input$dataset)
                data = get(input$dataset, "package:datasets")
                ggplot(data, aes(.data[[names(data)[1]]], .data[[names(data)[2]]]))   geom_point()
        })
}

shinyApp(ui, server)

-output

enter image description here

  • Related