本文介绍了根据单选按钮选择创建图R Shiny的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个Shiny应用.用户界面UI.R看起来很好,但是server.R出现问题.基本上,我希望根据用户选择哪个radio选项获得不同的绘图输出.

I'm trying to create a Shiny App. The user interface UI.R looks just fine but I'm having issues with server.R. Basically I want a different plot output depending on which radio option the user selects.

用户可以选择选项ABC.如果用户选择选项AB的条形图和选项C的饼图,我想绘制直方图,但是我不知道如何编码条件?像if-else语句吗?我已经奋斗了几个小时!这是我的代码示例:

The user may choose option A, B, or C. I want to draw a histogram if user selects option A, bar graph for B, and a pie chart for option C but I don't know how to code the condition? Is it like an if-else statement? I've been struggling for hours! Here's my code sample:

output$plots <- renderPlot({
   if selection == 'A'
      # plot histogram
   if selection == 'B'
      # plot bar chart
   if selection == 'C'
      # plot pie chart
})

谢谢!

推荐答案

您可以使用switch根据选择确定行为:

You can use switch to determine the behaviour based on the selection:

library(shiny)
myData <- runif(100)
plotType <- function(x, type) {
  switch(type,
         A = hist(x),
         B = barplot(x),
         C = pie(x))
}
runApp(list(
  ui = bootstrapPage(
    radioButtons("pType", "Choose plot type:",
                 list("A", "B", "C")),
    plotOutput('plot')
  ),
  server = function(input, output) {
    output$plot <- renderPlot({
       plotType(myData, input$pType)
    })
  }
))

这篇关于根据单选按钮选择创建图R Shiny的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 01:38