【问题标题】:How to return back to the first plot in R-Shiny?如何回到 R-Shiny 的第一个情节?
【发布时间】:2019-08-29 11:31:48
【问题描述】:

我正在开发一个 R-Shiny 应用程序。我使用以下代码(演示代码)与情节进行交互。

  ui <- shinyUI(fluidPage(
   titlePanel("Title"),
   sidebarLayout(
     sidebarPanel(
     ),
     mainPanel(
       plotOutput("graph", width = "100%", click = "plot_click"),
       verbatimTextOutput("click_info")
     )
   )
 ) 
 )

 server <- shinyServer(function(input, output, session) { 
   observe({
     output$graph <- renderPlot({
       plot(1, 1)
     })  
   })


   # interaction click in graph  
   observe({
     if(is.null(input$plot_click$x)) return(NULL)
     x <- sample(20:30,1,F)
     isolate({
       output$graph <- renderPlot({
         draw.single.venn(x)
       }) 
     })

   })
 })
 shinyApp(ui=ui,server=server)

它可以通过鼠标点击改变情节。我想使用重置按钮回到第一个情节。请帮忙。

【问题讨论】:

    标签: r shiny shinydashboard shiny-server


    【解决方案1】:

    我在您的侧边栏中添加了一个重置​​按钮。希望这会有所帮助。 This link 提供有关如何执行此类功能的更多信息。

    library(shiny)
    
    ui <- shinyUI(fluidPage(
      titlePanel("Title"),
      sidebarLayout(
        sidebarPanel(
          actionButton("Reset", label="Reset Graph")
        ),
        mainPanel(
          plotOutput("graph", width = "100%", click = "plot_click"),
          verbatimTextOutput("click_info")
        )
      )
    ) 
    )
    
    server <- shinyServer(function(input, output, session) { 
      observeEvent(input$Reset,{ output$graph <- renderPlot({ plot(1, 1) }) }, ignoreNULL = F)
    
      # interaction click in graph  
      observe({
        if(is.null(input$plot_click$x)) return(NULL)
        x <- sample(20:30,1,F)
        isolate({
          output$graph <- renderPlot({
            draw.single.venn(x)
          }) 
        })
    
      })
    
    })
    shinyApp(ui=ui,server=server)
    

    【讨论】:

    • 无需重复代码:observeEvent(input$Reset,{ output$graph &lt;- renderPlot({ plot(1, 1) }) }, ignoreNULL = F)
    • 在观察中放置渲染和其他繁重操作可能会出现内存泄漏,尝试添加更大的数据集,您会明白我的意思
    • 感谢您的解决方案。如果出现任何问题,会通知您。
    • @olorcain 还有一件事。我想做什么改变才能回到上一个情节(不是第一个情节)。
    • 我认为返回之前的用户定义的情节要比返回固定的起点更加困难。您可能需要保存可以参考和加载的绘图参数列表。我认为这应该是一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-12
    • 2021-12-03
    • 2019-11-25
    • 2014-10-11
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多