繁体   English   中英

在R中使用Shiny进行绘图

[英]Making a plot with Shiny in R

我正在学习Shiny,并试图从虹膜数据集中绘制定量数据。 我在ui.R中的selectizeinput似乎可以正常工作,但无法绘制它。 有什么建议吗? 下面的代码

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

aes_string添加到您的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)

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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