2016-05-23 3 views
1

Ich bin mir nicht sicher über den Code, den ich in den mainPanel-Teil setzen muss, um das mainPanel-Diagramm basierend auf der Benutzereingabe zu ändern.Unvollständiger Code Shiny App, mtcars-Datensatz, wie vier Benutzereingaben in vier verschiedenen Diagrammen gemacht werden

Ich habe vier Arbeits alle Diagramme zum mtcars-Datensatz gebunden:

ersten Graphen, lässt Anruf "zählt":

counts <- table(mtcars$gear) 
      barplot(counts, main="Car Distribution", 
      xlab="Number of Gears") 

zweiten Graphen lässt Anruf "Horizontal Counts":

Horizontalcounts <- table(mtcars$gear) 
        barplot(counts, main="Car Distribution", horiz=TRUE, 
        names.arg=c("3 Gears", "4 Gears", "5 Gears")) 

dritte Grafik kann "stacked Balkendiagramm" aufrufen:

stackedbarchart <- table(mtcars$vs, mtcars$gear) 
        barplot(counts, main="Car Distribution by Gears and VS", 
        xlab="Number of Gears", col=c("darkblue","red"), 
        legend = rownames(counts)) 

und der vierte Graph „gruppiert Balkendiagramm“ genannt:

groupedbarchart <- table(mtcars$vs, mtcars$gear) 
        barplot(counts, main="Car Distribution by Gears and VS", 
        xlab="Number of Gears", col=c("darkblue","red"), 
        legend = rownames(counts), beside=TRUE) 

Ich möchte folgendes mit Shiny tun, hat der Benutzer vier Optionen (die vier Diagramme siehe oben), wenn man gewählt ist, der Graph wird dann zeig. Also eine einfache glänzende App, Benutzer wählt eine Grafik und die Grafik zeigt sich auf der Hauptkonsole.

Ich habe den folgenden Code, und es läuft, aber die Diagramme zeigen nicht bis:

library(shiny) 
library(ggplot2) 


setwd("wd") 
myData <- mtcars 

ui <- fluidPage(
    titlePanel("cars graphs"), 
    sidebarLayout(
      sidebarPanel(selectInput("carsInput", " car graph Choice", 
         choices = c("counts", "Horizontal counts", "stacked bar chart", "grouped bar chart"), 
          selected = "counts")), 
     mainPanel(plotOutput("unsure?"), 
        br(), br(), 
        tableOutput("results") 
    ) 
) 
) 

server <- function(input, output) { 

     output$plotOutput <- reactive({ 
      if (input$carsInput == "counts") 
     { 


    counts <- table(mtcars$gear) 
    barplot(counts, main="Car Distribution", 
      xlab="Number of Gears") 

    } 
if (input$carsInput == "horizontal counts") 
    { 

    counts <- table(mtcars$gear) 
    barplot(counts, main="Car Distribution", horiz=TRUE, 
      names.arg=c("3 Gears", "4 Gears", "5 Gears")) 

} 

if (input$carsInput == "stacked bar chart") 
{ 


    counts <- table(mtcars$vs, mtcars$gear) 
    barplot(counts, main="Car Distribution by Gears and VS", 
      xlab="Number of Gears", col=c("darkblue","red"), 
      legend = rownames(counts)) 

} 
if (input$carsInput == "grouped bar chart") 
{ 


    counts <- table(mtcars$vs, mtcars$gear) 
    barplot(counts, main="Car Distribution by Gears and VS", 
      xlab="Number of Gears", col=c("darkblue","red"), 
      legend = rownames(counts), beside=TRUE) 

} 
} 

}) 


} 
shinyApp(ui = ui, server = server) 

Ich bin nicht sicher, der Code, den ich im Mainpanel Teil setzen müssen das Mainpanel Graph Veränderung haben basierend auf der Benutzereingabe. Hast du irgendeine Idee?

Antwort

1

Ich glaube, Sie brauchen renderPlot zu verwenden, wenn Sie Grundstücke in UI wie folgt

library(shiny) 
    library(ggplot2) 



    myData <- mtcars 

    ui <- fluidPage(
     titlePanel("cars graphs"), 
     sidebarLayout(
      sidebarPanel(selectInput("carsInput", " car graph Choice", 
            choices = c("counts", "Horizontal counts", "stacked bar chart", "grouped bar chart"), 
            selected = "counts")), 
      mainPanel(plotOutput("plotOutput"), 
         br(), br(), 
         tableOutput("results") 
      ) 
     ) 
    ) 

    server <- function(input, output) { 

     output$plotOutput <- renderPlot({ 
      if (input$carsInput == "counts") 
      { 


       counts <- table(mtcars$gear) 
       barplot(counts, main="Car Distribution", 
         xlab="Number of Gears") 

      } 
      if (input$carsInput == "Horizontal counts") 
      { 

       counts <- table(mtcars$gear) 
       barplot(counts, main="Car Distribution", horiz=TRUE, 
         names.arg=c("3 Gears", "4 Gears", "5 Gears")) 

      } 

      if (input$carsInput == "stacked bar chart") 
      { 


       counts <- table(mtcars$vs, mtcars$gear) 
       barplot(counts, main="Car Distribution by Gears and VS", 
         xlab="Number of Gears", col=c("darkblue","red"), 
         legend = rownames(counts)) 

      } 
      if (input$carsInput == "grouped bar chart") 
      { 


       counts <- table(mtcars$vs, mtcars$gear) 
       barplot(counts, main="Car Distribution by Gears and VS", 
         xlab="Number of Gears", col=c("darkblue","red"), 
         legend = rownames(counts), beside=TRUE) 

      } 
     }) 

    } 



    shinyApp(ui = ui, server = server) 
sehen wollen