반응식 dataframe
에 변형을 시도하고 있는데 shiny
입니다. 아래의 코드에서 euc.dist
함수를 반응식 데이터 프레임 bathy_new()
에 사용하고 싶습니다. 여기빛나는 반응성 데이터 프레임 내부에서 eucledian 거리 계산
library(shiny)
ui <- fluidRow(
numericInput(inputId = "n", "Group ", value = 1),
plotOutput(outputId = "plot")
)
server <- function(input, output){
bathy <- structure(list(`Corrected Time` = structure(c(
1512040500, 1512040500,
1512040501, 1512040502, 1512040502, 1512040503
), class = c(
"POSIXct",
"POSIXt"
), tzone = "UTC"), Longitude = c(
-87.169858, -87.169858,
-87.1698618, -87.1698652, -87.1698652, -87.16986785
), Latitude = c(
33.7578743,
33.7578743, 33.75788237, 33.75789018, 33.75789018, 33.75789717
), `Depth (m)` = c(
3.95096, 3.82296, 3.63096, 3.57096, 3.48096,
3.32096
), easting = c(
484269.60819222, 484269.60819222, 484269.257751374,
484268.944306767, 484268.944306767, 484268.700169299
), northing = c(
3735323.04565401,
3735323.04565401, 3735323.94098565, 3735324.80742908, 3735324.80742908,
3735325.58284154
), diff = c(0, 0, 0, 0, 0, 0), group = c(
1, 1,
1, 2, 2, 2
)), .Names = c(
"Corrected Time", "Longitude", "Latitude",
"Depth (m)", "easting", "northing", "diff", "group"
), row.names = c(
NA,
-6L
), class = c("tbl_df", "tbl", "data.frame"))
euc.dist <- function(x1, y1, x2, y2){
distance <- sqrt((x2-x1)^2 + (y2-y1)^2)
return(distance)
}
#
bathy_new <- reactive({
bathy %>% dplyr::filter(group == input$n)
})
test <- bathy_new()
dist <- NULL
for (i in 1:nrow(test)){
dist <- euc.dist(x1 = test[i, "easting"] %>% .$easting,
y1 = test[i, "northing"] %>% .$northing,
x2 = test[i+1, 'easting'] %>% .$easting,
y2 = test[i+1, 'northing'] %>% .$northing)
}
test$dist <- dist
output$plot <- renderPlot(
qplot(cumsum(test$dist), bathy_new()$`Depth (m)`)
)
}
shinyApp(ui, server)
데이터는 내 원래 세트에 비해 매우 작은 데이터입니다 :
다음은 재현 예입니다. 그러나 목표는 각 그룹에서 포인트 사이의 거리를 찾는 것입니다. 이 작은 데이터 세트에는 2 개의 그룹이 있습니다. 1, 2
것은 내가 반짝의 외부에서이 코드를 실행할 수있는 다음과 같은 오류를
Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
가 계속 잘하지만 반응 데이터를 처리하는 방법을 잘.
test <- bathy_new()
dist <- NULL
for (i in 1:nrow(test)){
dist <- euc.dist(x1 = test[i, "easting"] %>% .$easting,
y1 = test[i, "northing"] %>% .$northing,
x2 = test[i+1, 'easting'] %>% .$easting,
y2 = test[i+1, 'northing'] %>% .$northing)
}
test$dist <- dist
는 결국, 내가 누적 거리 cum(dist)
과 깊이 Depth (m)
을 플롯 할 :
이
오류가있는 코드의 덩어리입니다.
안녕 @GyD 클래스 reaactive 처리하는 방법을 설명 주셔서 너무 감사합니다.renderPlot 함수 안에 모든 것을 넣어야한다는 것을 깨닫지 못했습니다. 또한 for 루프 문제에 대해 지적 해 주셔서 감사합니다. 나는 과거에는 반짝 반짝 빛 났었지만 시간이 좀 걸렸다. 코드가 완벽하게 작동합니다. –