【问题标题】:observeEvent (and eventReactive) not performing as expected观察事件(和事件反应)未按预期执行
【发布时间】:2017-07-14 14:10:35
【问题描述】:

我在制作响应式应用程序以显示给定状态的地图时遇到问题。我希望应用程序仅在我单击“显示地图”时做出反应,但出于某种原因,使用以下代码,在第一次单击“显示地图”后,无论我是否更改输入(状态),输出都会更改再次点击按钮。

library(shiny)

ui <- fluidPage(
    titlePanel("Show map of a given state"),
    sidebarLayout(
        sidebarPanel(
        textInput("state", label = "State", value = "CA", placeholder = "California or CA"),
        actionButton("showU","Show map")
        ),
        mainPanel(
            conditionalPanel(
                condition = "input.showU > 0",
                h3(textOutput("state")),
                uiOutput("url")
            )
        )
    )
)

server <- function(input, output){
    observeEvent(input$showU,{
        output$state <- renderText(paste("Map of", input$state, ":"))
        output$url <-renderUI({a(href=paste("https://www.google.com/maps/place/", input$state, sep=""),"Show in Google Map",target="_blank")})
    })
    #output$state <- eventReactive(input$showU, renderText(paste("Map of", input$state, ":")))
    #output$url <- eventReactive(input$showU, renderUI({a(href=paste("https://www.google.com/maps/place/", input$state, sep=""),"Show in Google Map",target="_blank")}))
}

shinyApp(ui,server)

我认为 observeEvent 或 eventReactive(被注释掉的代码;也不起作用)应该延迟反应,直到我单击操作按钮,但它没有这样做。谁能帮我弄清楚这里出了什么问题?谢谢!

【问题讨论】:

    标签: user-interface shiny


    【解决方案1】:

    这是因为您在renderYYY 中调用了input$xxx。你可以使用isolate():

    observeEvent(input$showU,{
            output$state <- renderText(paste("Map of", isolate(input$state), ":"))
            output$url <-renderUI({a(href=paste("https://www.google.com/maps/place/", isolate(input$state), sep=""),"Show in Google Map",target="_blank")})
        })
    

    【讨论】:

    • 就是这样!非常感谢。
    猜你喜欢
    • 2018-10-16
    • 2019-03-31
    • 2018-11-28
    • 1970-01-01
    • 2017-04-24
    • 2019-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多