使用按钮在闪亮的应用程序中显示预定义的图

时间:2018-08-03 15:16:56

标签: r shiny

我正在尝试制作一个带有单选按钮的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!谢谢大家!

1 个答案:

答案 0 :(得分:1)

您的代码中有几个错误。

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

在服务器中:

  • input替换为input$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)
})
相关问题