2016-04-14 124 views
0

我在學習Shiny,並試圖繪製虹膜數據集中的定量數據。我在ui.R中的選擇輸入似乎工作,但我無法得到它的陰謀。有什麼建議? 代碼如下在R中使用Shiny製作陰謀

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) 
    }) 
}) 

回答

1

添加aes_stringggplot

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) 

enter image description here

+1

謝謝豬排! – David