Exporting html file in batch mode

Hi, I am about to render all of the graphs in HTML (interactive version) using saveWidget command.

Here is my data description: I got multiple pigs, each has its own ID. They have a set of data referenced back to the pig ID. I want to plot this data and save each graph of each pig to a html file.

SO far, I can only do one pig at once and I want to do it for all pig ID. Here is my codebase:

#Set working directory  

setwd("C:/Users/Kevin Le/PycharmProjects/R_project")

#load data after data treatment  

load("JRPData.Rdata")
#----------------------------------------------------
# Attribute new easier name to each column
#----------------------------------------------------

#Order number of Animal_ID
ID <- unique(JRP_NA$ANIMAL_ID) 

#Pig ID  
Pig_ID <- JRP_NA$ANIMAL_ID

#Age (days)
Age <- JRP_NA$AGE

#Daily Feed Intake (kg)
DFI <- JRP_NA$FEED_INTAKE

for(idc in seq_along(ID)){


  # extract data for animal i 

  i <- ID[idc] # 90
  
  #extract dataset for animal i
  Data <- JRP_NA.0[JRP_NA.0$ANIMAL_ID == i,]
  Data
  #Age vector associated with 
  Age.plot <- Age[Pig_ID == i]
  
  #DFI vector associated with animal i
  DFI.plot <- DFI[Pig_ID == i]
  
  # Plot DFI graph
  
  # tiff(file = paste0("C:/Users/Kevin Le/PycharmProjects/R_project/Graphs/Step0_graphs/", idc, ".", Data$ANIMAL_ID, ".", "DFI", ".png"), width = 6000, height = 3500, units = "px", res=600)
  D <- plot_ly(Data, x = Age.plot, y = DFI.plot , type = 'scatter', mode = 'markers')%>%
  layout(title = paste("Daily feed intake measured from an auto-feeder","\nPig ID:", ID[idc]), plot_bgcolor = "#e5ecf6", xaxis = list(title = 'Age, d'),
         yaxis = list(title = 'Daily Feed Intake, kg/ d'))



  saveWidget(D, file="myFile.html")
}

As you can see, I can only export it into a file name:myFile.html. Now, I want to export the htl graph file for all pig ID , each have their own file.

Hi @17021279_Le_Cao_Tung and welcome to RStudio Community.

What you are trying to achieve is definitely doable; however, you need to provide a bit more information. Especially, about the data you are looking for. It may be helpful for you to take a look at this article, which explains how to efficiently ask an R question:

I have update my question

The same way you paste together a chart title , you can paste together an appropriate file name .

Something like this:

saveWidget(D, file=paste("Daily feed intake measured from an auto-feeder","\nPig ID:", ID[idc],".html")

You should use paste0() instead of paste() so that there will not be any space in your file name. Also the "\n" character (line break) is not a good idea in a file name. I don't claim to understand your context very well but why not just use a simple file name such as:

saveWidget(D, file=paste0("Pig_ID_", ID[idc], ".html")

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.