Making a plot with Shiny in R

余生颓废 提交于 2020-01-06 03:26:08

问题


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

回答1:


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)



来源:https://stackoverflow.com/questions/36627808/making-a-plot-with-shiny-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!