未完成的代码Shiny App,mtcars数据集,如何对四个不同的图表进行四个用户输入

时间:2016-05-23 02:21:32

标签: r ggplot2 shiny

我不确定我需要在mainPanel部分放置的代码,以便根据用户输入更改mainPanel图形。

我有四个工作图都与mtcars数据集绑定:

第一张图,让我们调用“计数”:

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

第二个图表让我们调用“水平计数”:

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

第三张图表可以调用“堆积条形图”:

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

和第四个图形称为“分组条形图”:

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)

我想用Shiny进行以下操作,当用户选择一个时,用户有四个选项(上面列出的四个图表),然后会显示图表。因此,一个简单的闪亮应用程序,用户选择一个图形,图形显示在主面板上。

我有以下代码并运行,但图表没有显示:

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)

我不确定我需要在mainPanel部分放置的代码,以便根据用户输入更改mainPanel图形。你有什么主意吗?

1 个答案:

答案 0 :(得分:1)

如果你想在UI中看到这样的情节

,我认为你需要使用renderPlot
 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)