【问题标题】:Plot with action button depending on 2 inputs根据 2 个输入使用操作按钮进行绘图
【发布时间】:2021-06-02 13:13:38
【问题描述】:

我正在尝试制作具有以下特征的东西:

  • 开始时不显示任何内容(无绘图输出),并且
  • 仅在单击按钮后更新(在更改第一个或第二个 var 时不会更新)

到目前为止,我已经使用了 observeEvent 和 eventReactive 但两者都还没有工作。对于这个问题,我以 iris 为例。在过去的几个小时里,我一直在绞尽脑汁,我使用了许多不同的技术,例如 observeEvent、eventReactive、isolate() 等。

代码:

library(shiny)
library(datasets)
library(ggplot2)

data(iris)
ui <- shinyUI(pageWithSidebar(
        headerPanel("plot example"),
        
        sidebarPanel(

                selectInput("var1", "First var",
                            list("Sepal length" = "Sepal.Length",
                                 "Sepal width"  = "Sepal.Width",
                                 "Petal length" = "Petal.Length",
                                 "Petal width"  = "Petal.Width")),
                
                selectInput("var2", "Second var",
                            list("Petal length" = "Petal.Length",
                                 "Petal width"  = "Petal.Width",
                                 "Sepal length" = "Sepal.Length",
                                 "Sepal width"  = "Sepal.Width")),
                actionButton("gobutton", "Go")
        ),
        
        mainPanel(
                                 plotlyOutput("plot"))
))

server <- shinyServer(function(input, output, session){
        
        # I want the plot only to update if I press "Go"
        # observe event only does this the first time
        #  observeEvent(eventExpr = {
        #          input$gobutton
        #  },
        #          handlerExpr = {
        # output$plot <- renderPlotly({
        #         iris_plot <- ggplot(iris, aes_string(x=input$var1, 
        #                                      y=input$var2, 
        #                                      colour="Species")) + geom_point()
        #         ggplotly(iris_plot)
        # })
        #          })
        
        # this gives the error: Unknown input: reactive.event
        inputVar <- eventReactive(input$gobutton, {
                runif(input$var1, input$var2)
        })
        
        output$plot <- renderPlotly({
                iris_plot <- ggplot(iris, aes_string(x=inputVar, 
                                                     y=inputVar,
                                                     colour="Species")) + geom_point()
                ggplotly(iris_plot)
        })
        
        # this below is the regular code
        # output$plot <- renderPlotly({
        #                  iris_plot <- ggplot(iris, aes_string(x=input$var1,
        #                                               y=input$var2,
        #                                               colour="Species")) + geom_point()
        #                  ggplotly(iris_plot)
        # })
})

shinyApp(ui = ui, server = server)
        

【问题讨论】:

    标签: r shiny ggplotly


    【解决方案1】:

    这应该可行

    rv <- reactiveValues(var1=NULL,var2=NULL)
    observeEvent(input$gobutton,{
       rv$var1 <- input$var1
       rv$var2 <- input$var2
    })
    output$plot <- renderPlotly({
        if (!is.null(rv$var1) & !is.null(rv$var2)){
                    iris_plot <- ggplot(iris, aes_string(x=rv$var1, 
                                                         y=rv$var2,
                                                         colour="Species")) + geom_point()
                    ggplotly(iris_plot)
        }
    })
    
    
    

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    req(input$gobutton) 应该可以解决问题

    output$plot <- renderPlotly({
        req(input$gobutton)
    
        iris_plot <- ggplot(iris, aes_string(x=input$var1,
                                             y=input$var2,
                                             colour="Species")) + geom_point()
    
        ggplotly(iris_plot)
    

    【讨论】:

    • 感谢您的回复。但是第一次按下按钮后(有效),它会立即更新而无需再次按下按钮?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 2013-10-16
    • 2014-06-24
    • 1970-01-01
    • 2012-12-20
    相关资源
    最近更新 更多