Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 计算网络中反应数据帧内的欧氏距离_R_Shiny - Fatal编程技术网

R 计算网络中反应数据帧内的欧氏距离

R 计算网络中反应数据帧内的欧氏距离,r,shiny,R,Shiny,我正在尝试对shinny中的反应式dataframe进行一些转换。我想使用函数euc.dist对下面代码中的反应数据帧bathy_new() 以下是可复制的示例: library(shiny) ui <- fluidRow( numericInput(inputId = "n", "Group ", value = 1), plotOutput(outputId = "plot") ) server <- function(input, output){ bathy

我正在尝试对
shinny
中的反应式
dataframe
进行一些转换。我想使用函数
euc.dist
对下面代码中的反应数据帧
bathy_new()

以下是可复制的示例:

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)

最后,我想绘制累积距离
cum(dist)
和深度
depth(m)
得到该错误的原因是您实际上试图将
反应性
分配给变量
test
。这只能从反应式表达式或观察者内部完成

因此,您需要做的是将代码放在反应式表达式中,例如
renderPlot

  output$plot <- renderPlot({
    test <- bathy_new() 

    dist <- NULL
    for (i in 1:(nrow(test) - 1)){
      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
    qplot(cumsum(test$dist), bathy_new()$`Depth (m)`)
  })

出现该错误的原因是,您实际上试图将
反应性
分配给变量
测试
。这只能从反应式表达式或观察者内部完成

因此,您需要做的是将代码放在反应式表达式中,例如
renderPlot

  output$plot <- renderPlot({
    test <- bathy_new() 

    dist <- NULL
    for (i in 1:(nrow(test) - 1)){
      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
    qplot(cumsum(test$dist), bathy_new()$`Depth (m)`)
  })

您好@GyD非常感谢您解释如何处理课堂活动。我没有意识到我必须把所有的东西都放在renderPlot函数中。还感谢您指出for循环的问题。我过去用过闪亮,但已经有一段时间了。代码运行得非常完美。您好@GyD非常感谢您解释如何处理类reaactive。我没有意识到我必须把所有的东西都放在renderPlot函数中。还感谢您指出for循环的问题。我过去用过闪亮,但已经有一段时间了。代码工作得很完美。
library(shiny)
library(magrittr)
library(ggplot2)

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

ui <- fluidRow(
  numericInput(inputId = "n", "Group ", value = 1), 
  plotOutput(outputId = "plot")
)

server <- function(input, output){
  bathy_new <- reactive({
    bathy %>% dplyr::filter(group == input$n)
  })

  output$plot <- renderPlot({
    test <- bathy_new() 

    dist <- NULL
    for (i in 1:(nrow(test) - 1)){
      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
    qplot(cumsum(test$dist), bathy_new()$`Depth (m)`)
  })
}

shinyApp(ui, server)