简体   繁体   中英

Making a plot with Shiny in R

I'm learning Shiny and am trying to plot the quantitative data from the iris dataset. My selectizeinput in the ui.R appears to work but I can't get it to plot. Any advice? Code below

ui.R

irisx<-read.csv("iris.csv",header=T)
library(shiny)
shinyUI(fluidPage(
  titlePanel("Assignment 11"),
      sidebarLayout(
        sidebarPanel(
          selectizeInput("x","X:",choices = c("Sepal Length"="Sepal.Length","Sepal Width"="Sepal.Width","Petal Length"="Petal.Length", "Petal Width"="Petal.Width")),
          selectizeInput("y","Y:",choices = c("Sepal Length"="Sepal.Length","Sepal Width"="Sepal.Width","Petal Length"="Petal.Length", "Petal Width"="Petal.Width"))
        ),
        mainPanel(plotOutput("irisChart"))
      )
    ))

server.R

 irisx<-read.csv("iris.csv",header=T)

 library(shiny)
 library(ggplot)
 shinyServer(function(input,output){
 output$irisChart<-renderPlot({  
irx<-as.numeric(input$x)
iry<-as.numeric(input$y)
p1<-ggplot(irisx,aes(input$x,input$y)) + geom_point()
print(p1)
  })
 })

Add aes_string to your ggplot

rm(list = ls())
library(shiny)
library(ggplot2)

irisx <- iris
ui <- fluidPage(
  titlePanel("Assignment 11"),
  sidebarLayout(
    sidebarPanel(
      selectizeInput("x","X:",choices = c("Sepal Length"="Sepal.Length","Sepal Width"="Sepal.Width","Petal Length"="Petal.Length", "Petal Width"="Petal.Width")),
      selectizeInput("y","Y:",choices = c("Sepal Length"="Sepal.Length","Sepal Width"="Sepal.Width","Petal Length"="Petal.Length", "Petal Width"="Petal.Width"))
    ),
    mainPanel(plotOutput("irisChart"))
  )
)

server <- shinyServer(function(input,output){
  output$irisChart <- renderPlot({  
    irx <- input$x
    iry <- input$y
    p1 <- ggplot(data = irisx,aes_string(irx,iry)) + geom_point()
    p1
  })
})

shinyApp(ui, server)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM