【问题标题】:How to display plots on the same figure using radiobutton in R shiny?如何使用 R Shiny 中的单选按钮在同一图形上显示图?
【发布时间】:2023-03-16 15:43:01
【问题描述】:

在这个闪亮的应用程序中,如果我们选择第二个单选按钮“在同一图上显示图”,我希望在同一图上显示两个图 提前感谢您的帮助

library(shiny)
library(plotly)

ui <-fluidPage(
  titlePanel("title panel"),
  sidebarLayout(position = "left",
                sidebarPanel(
                  radioButtons(inputId = "plot", label=' ', choices=c("display plots on separate figures", "display plots on the same figure"))
                ),
                mainPanel(
                  plotlyOutput(outputId = "fbPlot1"),
                  plotlyOutput(outputId = "fbPlot2")
                )
                ))

server <- function(input, output) {
  output$fbPlot1 <- renderPlotly(
    fig <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
    
  )
  output$fbPlot2 <- renderPlotly(
    fig2 <- plot_ly(data = iris, x = ~Petal.Length, y = ~Sepal.Length,color ='blue')
    
  )
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    通过使用 shinyjs 包,您可以根据事件隐藏/显示 UI 元素。

    library(shiny)
    library(plotly)
    library(shinyjs)
    
    ui <-fluidPage(
      titlePanel("title panel"),
      useShinyjs(),
      sidebarLayout(position = "left",
                    sidebarPanel(
                      radioButtons(inputId = "plot", label=' ', choices=c("display plots on separate figures", "display plots on the same figure"))
                    ),
                    mainPanel(
                      div(id = "plot1", plotlyOutput(outputId = "fbPlot1")),
                      div(id = "plot2", plotlyOutput(outputId = "fbPlot2")),
                      div(id = "plot3", plotlyOutput(outputId = "fbPlot3"))
                    )
      ))
    
    server <- function(input, output) {
      
      rv <- reactiveValues()
      
      observe({
        rv$fig1 <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length)
        rv$fig2 <- plot_ly(data = iris, x = ~Petal.Length, y = ~Sepal.Length,color ='blue')
        rv$fig3 <- subplot( rv$fig1, rv$fig2)
      })
      
      output$fbPlot1 <- renderPlotly({
        rv$fig1
      })
      output$fbPlot2 <- renderPlotly({
        rv$fig2 
      })
      output$fbPlot3 <- renderPlotly({
        rv$fig3 
      })
      
      observeEvent(input$plot, {
        req(input$plot)
        
        print(input$plot)
        if(input$plot == "display plots on separate figures"){
          shinyjs::show("plot1")
          shinyjs::show("plot2")
          shinyjs::hide("plot3")
        }
        if(input$plot == "display plots on the same figure"){
          shinyjs::hide("plot1")
          shinyjs::hide("plot2")
          shinyjs::show("plot3")
        }
        
      }, ignoreNULL = FALSE)
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 感谢您的回答您的脚本不起作用,它打印:找不到错误对象'fig1'和找不到错误对象'fig2'
    • 你说得对,我忘了定义这些变量。我编辑了答案
    猜你喜欢
    • 2021-05-24
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2014-06-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 2021-08-01
    相关资源
    最近更新 更多