首页 > 解决方案 > 使用带有 for 循环的 plot_ly 命令时出现问题

问题描述

我是 R 编程新手,在修改 R 脚本以在代码中引入“for”循环时遇到了困难。在搜索时,我发现这个问题已经被提出并使用 lapply 命令解决了,但我不知道如何解决我的问题!有效的代码(没有 for 命令):CPM.txt 文件包含 1 行

CPM <- read.table("FicConfig/CPM.txt" , header = TRUE, sep = "\t" , dec = ",", skip = 0)

options(viewer = NULL)
plot_ly() %>%
 htmlwidgets::onRender(
   "function(el, x) {
     var gd = document.getElementById(el.id);
     Plotly.downloadImage(gd, {format: 'png', width: 1000, height: 700, filename: 'AnomalieRR_EcartTmoy'});
   }"
 )%>%
 add_trace(x =TAnn[ ,3],  y = TAnn[ ,5], name = "Normale mensuelle de Tmax(en °C)",type = 'scatter', mode = 'markers',  marker = list(size = T, symbol = 'circle',  color = ~TAnn[ ,5], line = list(width= 2, color = cl))) %>%
add_annotations(text = TAnn[ ,1], x= TAnn[ ,3], y = TAnn[ ,5], font = list(color = "white", size = 14),  showarrow = FALSE)%>%
 layout(title = paste("Combinaison anomalie relative annuelle des précipitations et écart annuel à la normale de la température moyenne  à ",CPM[1,1],sep =""),
      xaxis = list(title = "Anomalie relative des précipitations annuelles(en %)", tickangle = 20 ,titlefont = list(color= "blue", size= 14 , family = 'Arial'), tickfont = list(color = "blue", size = 14)),
yaxis = list(title = "Ecart à la normale de la température moyenne annuelle(en°C)", titlefont = list(color= "red", size= 14 , family = 'Arial'),
tickfont = list(color = "red", size = 14) , showline = TRUE, linecolor = "red", linewidth = 2),
   legend = list(x = 0.1, y = -0.3, font=list(size = 14,color= "black")),margin = list(
      t = 70,
     r = 70,
      b = 70,
      l = 70
    ))

不起作用的代码(使用 for 命令): # CPM.txt 文件包含几行

CPM <- read.table("FicConfig/CPM.txt" , header = TRUE, sep = "\t" , dec = ",", skip = 0)
for (i in 1: (nrow(CPM)))   {

options(viewer = NULL)
plot_ly() %>%
 htmlwidgets::onRender(
   "function(el, x) {
     var gd = document.getElementById(el.id);
     Plotly.downloadImage(gd, {format: 'png', width: 1000, height: 700, filename: 'AnomalieRR_EcartTmoy'});
   }"
 )%>%
 add_trace(x =TAnn[ ,3],  y = TAnn[ ,5], name = "Normale mensuelle de Tmax(en °C)",type = 'scatter', mode = 'markers',  marker = list(size = T, symbol = 'circle',  color = ~TAnn[ ,5], line = list(width= 2, color = cl))) %>%
add_annotations(text = TAnn[ ,1], x= TAnn[ ,3], y = TAnn[ ,5], font = list(color = "white", size = 14),  showarrow = FALSE)%>%
  layout(title = paste("Combinaison anomalie relative annuelle des précipitations et écart annuel à la normale de la température moyenne  à ",CPM[i,1],sep =""),
      xaxis = list(title = "Anomalie relative des précipitations annuelles(en %)", tickangle = 20 ,titlefont = list(color= "blue", size= 14 , family = 'Arial'), tickfont = list(color = "blue", size = 14)),
yaxis = list(title = "Ecart à la normale de la température moyenne annuelle(en°C)", titlefont = list(color= "red", size= 14 , family = 'Arial'),
tickfont = list(color = "red", size = 14) , showline = TRUE, linecolor = "red", linewidth = 2),
  legend = list(x = 0.1, y = -0.3, font=list(size = 14,color= "black")),margin = list(
      t = 70,
     r = 70,
      b = 70,
      l = 70
    ))
file.copy("C:/Users/pc/Downloads/Evolution.png", paste("C:/MONOGRAPHIE/EvolTmoy_",CPM[i,1],".png",sep="")
}

非常感谢您的任何帮助。

标签: r

解决方案


推荐阅读