首页 文章

基于R Shiny仪表板中的单选按钮进行绘图

提问于
浏览
1

这是我的代码:

ui <- fluidPage(
  radioButtons("dist", "Distribution type:",
               c("Sepal.Length" = "Length",
                 "Sepal.Width" = "Width",
                 "Petal.Length" = "Length")),
  plotOutput("distPlot")
)

server <- function(input, output) {
  output$distPlot <- renderPlot({
    dist <- switch(input$dist,
                   "Length" = plot(iris$Sepal.Length),
                   "Width" = plot(iris$Sepal.Width),
                   "Length" = plot(iris$Sepal.Length))

    plot(dist)
  })
}

shinyApp(ui, server)
}

我究竟做错了什么?

另外,我希望每个按钮都有一个 par(mfrow=c(1,2)) 图 . 我怎样才能做到这一点?

有帮助吗?

谢谢

1 回答

  • 2

    您不需要绘制已分配的函数,因为 switch it0s已经这样做了 .

    server <- function(input, output) {
      output$distPlot <- renderPlot({
      switch(input$dist,
                   "Length" = plot(iris$Sepal.Length),
                   "Width" = plot(iris$Sepal.Width),
                   "Length" = plot(iris$Sepal.Length))
               })
    }
    

    编辑:关于使用par (mfrow=c(1,2)) ,尽管我对其他选择的评论,这是我提出的替代方案:

    server <- function(input, output) {
      output$distPlot <- renderPlot({
      par(mfrow=c(1,2))
      switch(input$dist,
                   "Length" = plot(iris$Sepal.Length),
                   "Width" = plot(iris$Sepal.Width),
                   "Length" = plot(iris$Sepal.Length))
      switch(input$dist,
                   "Length" = plot(iris$Sepal.Length),
                   "Width" = plot(iris$Sepal.Width),
                   "Length" = plot(iris$Sepal.Length))
           })
    

相关问题