[R] "for" loop does not work with my plot_ly command
Jim Lemon
drj|m|emon @end|ng |rom gm@||@com
Tue Mar 30 04:36:55 CEST 2021
Hi Rachida,
My guess is that you create a vector of filenames:
filenames<-list.files(path="FicConfig",pattern="*.txt")
then use the filenames in the loop:
for(filename in filenames) {
nextfile<- read.table(filename, header = TRUE, sep = "\t" , dec =
",", skip = 0)
# do whatever you want with the resulting data frame here
# or perhaps save it into a list for processing later
}
Jim
On Tue, Mar 30, 2021 at 2:15 AM Rachida El Ouaraini
<elouaraini using gmail.com> wrote:
>
> Hi everyone,
> I am new to R programming, and I am having difficulties modifying an R
> script to introduce the "for" loop in the code.
> While searching I found that this issue has already been raised, but I did
> not know what to do to fix mine !
>
> *The code that works (without for command):*
>
> CPM <- read.table("FicConfig/CPM.txt" , header = TRUE, sep = "\t" , dec =
> ",", skip = 0)
> # The CPM.txt file containe 1 line
> ......................................................................
> ......................................................................
> 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
> ))
> *The code that does not work (with for command):*
> CPM <- read.table("FicConfig/CPM.txt" , header = TRUE, sep = "\t" , dec =
> ",", skip = 0)
> # The CPM.txt file containe several lines
>
> *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 Tmoy.png",
> paste("C:/MONOGRAPHIE/Resultats/Evolution Tmoy_",CPM[i,1],".png",sep="")*
> *}*
>
>
> Thank you very much in advance for any help.
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list