首页 > 解决方案 > 在带有按钮的闪亮应用程序中显示预定义的图

问题描述

我正在尝试制作一个带有单选按钮的小型 Shiny 应用程序,用于选择一个图(在代码中预定义)并显示它。这就是我所拥有的:

# The plots called "Plot1" etc. are predefined in the environment.

Interface <- {
  fluidPage(
    sidebarPanel(
      radioButtons(inputId = "Question",
                   label = "Plots",
                   choices = c("A" = "Plot1", 
                               "B" = "Plot2", 
                               "C" = "Plot3"),
                   selected = "Plot1")),
    mainPanel(uiOutput('ui_plot'))
  )
}

Server <- function(input, output){
   output$barplot <- renderPlotly({   
    if (input == "A") return(Plot1)
    else if (input == "B") return(Plot2)
    else if (input == "C") return(Plot3)  
    })
}

 shinyApp(ui = Interface, server = Server)

但这不起作用。我试过替换return()renderPlot()但它没有改变任何东西。

很抱歉,这可能是一个非常愚蠢的问题/错误,但这是我第一次使用 Shiny!感谢大家!

标签: rshiny

解决方案


您的代码中有几个错误。

在界面中:替换uiOutput('ui_plot')plotlyOutput("barplot")

在服务器中:

  • 替换inputinput$Questions

  • 替换"A""Plot1"

那是:

output$barplot <- renderPlotly({   
    if (input$Questions == "Plot1") return(Plot1)
    else if (input$Questions == "Plot2") return(Plot2)
    else if (input$Questions == "Plot3") return(Plot3)
})

推荐阅读