根据 2 个输入使用动作按钮绘图

Plot with action button depending on 2 inputs

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

到目前为止,我已经使用了 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)
        

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)

这应该有效

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)
    }
})