DAY 27
2
Data Technology

## 【27】當行銷人員傻眼問：呃...工程師大大，為什麼按了取經緯度按鈕後程式好像就當掉了？

``````server <- function(input, output) {
output\$plot <- renderPlot({
input\$goPlot # Re-run when button is clicked

# Create 0-row data frame which will be used to store data
dat <- data.frame(x = numeric(0), y = numeric(0))

withProgress(message = 'Making plot', value = 0, {
# Number of times we'll go through the loop
n <- 10

for (i in 1:n) {
# Each time through the loop, add another row of data. This is
# a stand-in for a long-running computation.
dat <- rbind(dat, data.frame(x = rnorm(1), y = rnorm(1)))

# Increment the progress bar, and update the detail text.
incProgress(1/n, detail = paste("Doing part", i))

# Pause for 0.1 seconds to simulate a long computation.
Sys.sleep(0.1)
}
})

plot(dat\$x, dat\$y)
})
}

ui <- shinyUI(basicPage(
plotOutput('plot', width = "300px", height = "300px"),
actionButton('goPlot', 'Go plot')
))

shinyApp(ui = ui, server = server)
``````

``````observeEvent(input\$request, {
withProgress(message = '擷取經緯度', value = 0, {
v\$addressWithLatLng <- v\$address %>%
rowwise() %>%
mutate(LatLng = getLatLng(V1) %>%
filter(LatLng!="error") %>%
separate(LatLng, c("Lat", "Lng"), sep=",") %>%
mutate(Lat=as.numeric(Lat), Lng=as.numeric(Lng))

})
})
``````

``````getLatLngWithProcress = function(address, total) {
incProgress(1/total, detail = "解析地址中")
return (getLatLng(address))
}
``````

``````observeEvent(input\$request, {
withProgress(message = '擷取經緯度', value = 0, {
v\$addressWithLatLng <- v\$address %>%
rowwise() %>%
mutate(LatLng = getLatLngWithProcress(V1, nrow(v\$address))) %>%
filter(LatLng!="error") %>%
separate(LatLng, c("Lat", "Lng"), sep=",") %>%
mutate(Lat=as.numeric(Lat), Lng=as.numeric(Lng))

})
})
``````

ref
day27程式碼