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:
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