为什么Boxplot出现在RStudio中而不是我的Shiny应用程序中?

时间:2019-06-30 10:41:58

标签: r plot shiny

我有以下代码。我的Shiny应用程序中正确显示了三个图(第一,第二,第四),而缺少第三个图(即“箱线图”)。但是,它会出现在RStudio的“绘图”窗口中。我在做什么错了?

library(shiny)

ui <- fluidPage(

    fluidRow( 
        verticalLayout( 
            splitLayout(cellWidths = c("50%", "50%"), 
                        plotOutput("pt1"), 
                        plotOutput("pt2")), 
            splitLayout(cellWidths = c("50%", "50%"), 
                        plotOutput("pt3"), 
                        plotOutput("pt4"))
        )
    )
)

server <- function(input, output) {

    set.seed(1234)

    pt1 <- qplot(rnorm(600),fill=I("blue"),binwidth=0.2)
    pt2 <- qplot(rnorm(200),fill=I("blue"),binwidth=0.2)
    pt3 <- boxplot(mpg~cyl,data=mtcars, main="Cars", xlab="cyl", ylab="mpg") 
    pt4 <- qplot(rnorm(900),fill=I("blue"),binwidth=0.2)

    output$pt1 = renderPlot({pt1})
    output$pt2 = renderPlot({pt2})
    output$pt3 = renderPlot({pt3})
    output$pt4 = renderPlot({pt4})    

}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:2)

您将在这里看到:How to save boxplot to as to a variable?,我们需要一些技巧来将箱形图保存为变量。

library(shiny)
library(ggplot2)
ui <- fluidPage(

  fluidRow( 
    verticalLayout( 
      splitLayout(cellWidths = c("50%", "50%"), 
                  plotOutput("pt1"), 
                  plotOutput("pt2")), 
      splitLayout(cellWidths = c("50%", "50%"), 
                  plotOutput("pt3"), 
                  plotOutput("pt4"))
    )
  )
)

server <- function(input, output) {

  set.seed(1234)

  pt1 <- qplot(rnorm(600),fill=I("blue"),binwidth=0.2)
  pt2 <- qplot(rnorm(200),fill=I("blue"),binwidth=0.2)
  boxplot(mpg~cyl,data=mtcars, main="Cars", xlab="cyl", ylab="mpg")
  pt3 = recordPlot()
  dev.off()

  pt4 <- qplot(rnorm(900),fill=I("blue"),binwidth=0.2)

  output$pt1 = renderPlot({pt1})
  output$pt2 = renderPlot({pt2})
  output$pt3 = renderPlot({pt3 })
  output$pt4 = renderPlot({pt4})    

}

shinyApp(ui = ui, server = server)