2017-02-23 141 views
1

我在制作反应式应用程序以显示给定状态的地图时遇到问题。我希望应用只在单击“显示地图”时作出反应,但出于某种原因,使用下面的代码,在第一次点击“显示地图”后,输出会随着输入(状态)的改变而改变,无论我是否再次点击该按钮。observeEvent(和eventReactive)未按预期执行

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(这是注释掉的代码;也不行)应该延迟反应,直到我点击了动作按钮,但它没有这样做。有人能帮我弄清楚这里有什么问题吗?谢谢!

回答

2

这是因为您在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")}) 
    }) 
+0

就是这样!非常感谢。 –