Html Rmarkdown-代码在脚本上运行正常,但在knit给出错误时

Html Rmarkdown-代码在脚本上运行正常,但在knit给出错误时,html,r,r-markdown,Html,R,R Markdown,我有一份Rmarkdown文件,是给我的,和给我的人相处得很好。 但是当我处理数据时,我不知道发生了什么,当我运行脚本文档的代码时,效果很好,但是当我尝试编织到html时,给我一个错误 bhv_df <- plyr::ddply(bhv_df, ~segmentid, function(d){ d= bhv_df[bhv_df$segmentid == bhv_df$segmentid[1], # predictions are made based on the mid tim

我有一份Rmarkdown文件,是给我的,和给我的人相处得很好。 但是当我处理数据时,我不知道发生了什么,当我运行脚本文档的代码时,效果很好,但是当我尝试编织到html时,给我一个错误

bhv_df <- plyr::ddply(bhv_df, ~segmentid, function(d){
  d= bhv_df[bhv_df$segmentid == bhv_df$segmentid[1], 
  # predictions are made based on the mid time between start and end of the message
  predObj <- crawl::crwPredict(object.crwFit = crawl_models_list[[d$segmentid[1]]], predTime = d$MidTime, speedEst=TRUE, flat=TRUE)
  predObj_dives <- predObj[predObj$locType == "p",]
  # reproject into lat/long, because the crawl models have been built in a mercator pacific centered CRS
  coord_points <- predObj_dives
  coordinates(coord_points) =~ mu.x + mu.y
  proj4string(coord_points) <- CRS("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
  predObj_dives[c("lon", "lat")] <- coordinates(spTransform(coord_points, CRS("+proj=longlat +datum=WGS84")))
  # calculate the lon360
  return(cbind(d, predObj_dives[c("mu.x", "mu.y", "lon", "lat")]))
})


#load("./Outputs/crawl_argos_df.RData")

######### MERGE DIVE DATASET with CRAWL-derived MOUVEMENT DATA
bhv_df <- plyr::ddply(bhv_df, ~segmentid, function(d){ # for each tag dive data
  cr <- crawl_argos_df[crawl_argos_df$segmentid == d$segmentid[1], ] # select the corresponding crawl track
  fl <- filtered_argos_df_crawl[filtered_argos_df_crawl$segmentid == d$segmentid[1], ] # select the corresponding filtered track
  d[c("lq_closest_filt","dt_closest_filt", "dist_closest_filt", "rel.angle","speed")] <- NA # add empty columns
  for (i in 1:nrow(d)){ # for each dive...
    fl$dist_diff <- as.vector(spDists(x = as.matrix(d[i,c("lon", "lat")]), y = as.matrix(fl[c("lon", "lat")]), longlat = T)) # distance calculated in km
    fl$dt_diff <- as.numeric(difftime(d$MidTime[i], fl$time, units = "hours"))
    d[i, "dt_closest_filt"] <- min(abs(fl$dt_diff)) # select the filtered position closest in time to the dive, time in hours
    d[i, "dist_closest_filt"] <- fl[which(abs(fl$dt_diff) == min(abs(fl$dt_diff))), "dist_diff"][1] # retrieve the distance from this filtered position to the dive location (predicted by crawl)
    d[i, "lq_closest_filt"] <- fl[which(abs(fl$dt_diff) == min(abs(fl$dt_diff))), "lq"][1] # argos quality of closest filtered argos position
    cr$diff <- as.numeric(difftime(d$MidTime[i], cr$time, units = "hours")) # look at the time diff between this dive and all positions recorded in crawl for that same tag
    d[i, c("rel.angle","speed")] <- cr[abs(cr$diff) == min(abs(cr$diff)), c("rel.angle", "speed")] # select closest position recorded when dive occurred
  }
  return(d)
})

bhv_df$depth_bin <- cut(bhv_df$DepthMean, seq(0, 700, 50))

ggplot(bhv_df[bhv_df$What == "Dive" & bhv_df$depth_range == "deep" & bhv_df$DepthMean < 1000,], aes(x = lon, y = lat)) +
  stat_contour(data = bathyNOAA_df, aes(x, y, z=z), binwidth = 500, color = "grey60", size = 0.2) +
  geom_tile(data = bathyNOAA_df_shallow[bathyNOAA_df_shallow$z >= 0, ], aes(x, y), fill = "grey10") +
  geom_jitter(aes(fill = -DepthMean, size = DepthMean), col="black", alpha=0.8, pch = 21, width=0.1) +
  scale_fill_viridis(option = "magma", name = "Dive Depth (m)", direction = 1, begin = 0.2) +
  xlab("Longitude") +
  ylab("Latitude") +
  coord_fixed(xlim = c(-50, -26), ylim = c(-55, -15), expand = F) 


bhv_df经过多次尝试,我删除了代码的一部分,一切正常

# I removed: d= bhv_df[bhv_df$segmentid == bhv_df$segmentid[1], 

bhv_df <- plyr::ddply(bhv_df, ~segmentid, function(d){
  # predictions are made based on the mid time between start and end of the message
  predObj <- crawl::crwPredict(object.crwFit = crawl_models_list[[d$segmentid[1]]], predTime = d$MidTime, speedEst=TRUE, flat=TRUE)
  predObj_dives <- predObj[predObj$locType == "p",]
  # reproject into lat/long, because the crawl models have been built in a mercator pacific centered CRS
  coord_points <- predObj_dives
  coordinates(coord_points) =~ mu.x + mu.y
  proj4string(coord_points) <- CRS("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
  predObj_dives[c("lon", "lat")] <- coordinates(spTransform(coord_points, CRS("+proj=longlat +datum=WGS84")))
  # calculate the lon360
  return(cbind(d, predObj_dives[c("mu.x", "mu.y", "lon", "lat")]))
})


######### MERGE DIVE DATASET with CRAWL-derived MOUVEMENT DATA
bhv_df <- plyr::ddply(bhv_df, ~segmentid, function(d){ # for each tag dive data
  cr <- crawl_argos_df[crawl_argos_df$segmentid == d$segmentid[1], ] # select the corresponding crawl track
  fl <- filtered_argos_df_crawl[filtered_argos_df_crawl$segmentid == d$segmentid[1], ] # select the corresponding filtered track
  d[c("lq_closest_filt","dt_closest_filt", "dist_closest_filt", "rel.angle","speed")] <- NA # add empty columns
  for (i in 1:nrow(d)){ # for each dive...
    fl$dist_diff <- as.vector(spDists(x = as.matrix(d[i,c("lon", "lat")]), y = as.matrix(fl[c("lon", "lat")]), longlat = T)) # distance calculated in km
    fl$dt_diff <- as.numeric(difftime(d$MidTime[i], fl$time, units = "hours"))
    d[i, "dt_closest_filt"] <- min(abs(fl$dt_diff)) # select the filtered position closest in time to the dive, time in hours
    d[i, "dist_closest_filt"] <- fl[which(abs(fl$dt_diff) == min(abs(fl$dt_diff))), "dist_diff"][1] # retrieve the distance from this filtered position to the dive location (predicted by crawl)
    d[i, "lq_closest_filt"] <- fl[which(abs(fl$dt_diff) == min(abs(fl$dt_diff))), "lq"][1] # argos quality of closest filtered argos position
    cr$diff <- as.numeric(difftime(d$MidTime[i], cr$time, units = "hours")) # look at the time diff between this dive and all positions recorded in crawl for that same tag
    d[i, c("rel.angle","speed")] <- cr[abs(cr$diff) == min(abs(cr$diff)), c("rel.angle", "speed")] # select closest position recorded when dive occurred
  }
  return(d)
}) #warnings probably due to RT being equal to NA at beginning and end of the track

#我删除了:d=bhv_-df[bhv_-df$segmentid==bhv_-df$segmentid[1],

bhv_df这行似乎没有完成:
d=bhv_df[bhv_df$segmentid==bhv_df$segmentid[1],
。我建议您在运行此代码并清除所有缓存之前,先清除您的环境并卸载所有库。有时,当knitr未生成同名的环境变量时,knitr会继承这些环境变量,而缓存也可能保留旧的结果。这将有助于您重现相同的环境并发现错误。@StéphaneLaurent,代码是这样的,在脚本中工作得很好document@user2332849,我已经这样做了,并创建了一个新的Rmarkdown文档,直到重新启动我的计算机…我同意该行尚未完成,
d=bhv_-df[bhv_-df$segmentid==bhv_-df$segmentid[1]
,可能只需要一个
]
最后,可能是以前的所有者删除了它。没有匹配的括号。不可能以这种方式工作。
# I removed: d= bhv_df[bhv_df$segmentid == bhv_df$segmentid[1], 

bhv_df <- plyr::ddply(bhv_df, ~segmentid, function(d){
  # predictions are made based on the mid time between start and end of the message
  predObj <- crawl::crwPredict(object.crwFit = crawl_models_list[[d$segmentid[1]]], predTime = d$MidTime, speedEst=TRUE, flat=TRUE)
  predObj_dives <- predObj[predObj$locType == "p",]
  # reproject into lat/long, because the crawl models have been built in a mercator pacific centered CRS
  coord_points <- predObj_dives
  coordinates(coord_points) =~ mu.x + mu.y
  proj4string(coord_points) <- CRS("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")
  predObj_dives[c("lon", "lat")] <- coordinates(spTransform(coord_points, CRS("+proj=longlat +datum=WGS84")))
  # calculate the lon360
  return(cbind(d, predObj_dives[c("mu.x", "mu.y", "lon", "lat")]))
})


######### MERGE DIVE DATASET with CRAWL-derived MOUVEMENT DATA
bhv_df <- plyr::ddply(bhv_df, ~segmentid, function(d){ # for each tag dive data
  cr <- crawl_argos_df[crawl_argos_df$segmentid == d$segmentid[1], ] # select the corresponding crawl track
  fl <- filtered_argos_df_crawl[filtered_argos_df_crawl$segmentid == d$segmentid[1], ] # select the corresponding filtered track
  d[c("lq_closest_filt","dt_closest_filt", "dist_closest_filt", "rel.angle","speed")] <- NA # add empty columns
  for (i in 1:nrow(d)){ # for each dive...
    fl$dist_diff <- as.vector(spDists(x = as.matrix(d[i,c("lon", "lat")]), y = as.matrix(fl[c("lon", "lat")]), longlat = T)) # distance calculated in km
    fl$dt_diff <- as.numeric(difftime(d$MidTime[i], fl$time, units = "hours"))
    d[i, "dt_closest_filt"] <- min(abs(fl$dt_diff)) # select the filtered position closest in time to the dive, time in hours
    d[i, "dist_closest_filt"] <- fl[which(abs(fl$dt_diff) == min(abs(fl$dt_diff))), "dist_diff"][1] # retrieve the distance from this filtered position to the dive location (predicted by crawl)
    d[i, "lq_closest_filt"] <- fl[which(abs(fl$dt_diff) == min(abs(fl$dt_diff))), "lq"][1] # argos quality of closest filtered argos position
    cr$diff <- as.numeric(difftime(d$MidTime[i], cr$time, units = "hours")) # look at the time diff between this dive and all positions recorded in crawl for that same tag
    d[i, c("rel.angle","speed")] <- cr[abs(cr$diff) == min(abs(cr$diff)), c("rel.angle", "speed")] # select closest position recorded when dive occurred
  }
  return(d)
}) #warnings probably due to RT being equal to NA at beginning and end of the track