Skip to content

Generic R code for producing multi-tabbed HTML output for plotted results in a specific directory

Notifications You must be signed in to change notification settings

haddonm/makehtml

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Latest Changes

  • 2024-02-19 0.1.1 Updated the readme file so the example works with new libraries codeutils and hplot, adjusted ‘setuphtml()’ to remove the deprecated ‘cleanslate’ (see ‘cleanrundir()’ function).

makehtml

Generic R code for producing multi-tabbed HTML output for plotted and tabulating results into a specific directory. The objective is to provide a standard method for an initial presentation of what can be extensive results from an analysis. Rmarkdown and Rmd files are wonderful but sometimes one just needs to review the data and the results and so needs a convenient way of viewing tables and plots. makehtml aims to provide that method.

This code is adapted and revised from code put together by Ian Taylor inside the R package r4ss. Essentially it creates a CSS file that is used by a series of HTML files, each of which represents a user defined category of results (each generates a single tab in an internal wedsite.

All plots should be .png files, and all tables are derived from saved .csv files.

All files, the CSS, HTML, .png and .csv files are saved to a user defined directory (rundir), which is the main user input requirement. One can now provide a name for the webpages rather than use the default, which is set to ‘packagename’, so you might want to change that, and this can be done in the make_html() function by changing the value of the packagename and htmlname arguments.

Installation

You can install the development version from GitHub with:

# # If you do not have the required packages installed unhash the code in this block and run it
# if (!require(devtools)){install.packages("devtools")} 
# 
# devtools::install_github("https://github.com/haddonm/makehtml")
# 
# # you will also need codutils and hplot
# 
# devtools::install_github("https://github.com/haddonm/codeutils")
# devtools::install_github("https://github.com/haddonm/hplot")

Alternatively, you can generate a branch that you can work on by cloning the repository, which, again, can be done very simply within RStudio. Open the New Project option in the project dialog at the top right of the RStudio screen and selection Version Control, then use ‘https://github.com/haddonm/makehtml’ in the top box, identify where you want the new directory put, and press return.

It would be a good idea to read Hadley Wickham’s draft chapter on Git and GitHub at https://r-pkgs.org/index.html.

Example

This is a basic example which illustrates the use of makehtml, though I have hashed out the final call to make_html so that the RMarkdown version of the readme.md file does not generate a local website:

library(makehtml)
library(codeutils) 
library(hplot) # for plotprep and parset
#> 
#> Attaching package: 'hplot'
#> The following objects are masked from 'package:codeutils':
#> 
#>     countgtzero, getmax, getmin
starttime <- as.character(Sys.time())

# OBVIOUSLY you should define your own directory
ddir <- getDBdir()
indir <- pathtopath(ddir,"/A_codeUse/makehtmlUse/") 
rundir <- pathtopath(indir,"result") # pathtopath combines paths consistently
dirExists(rundir,verbose=TRUE)
#> c:/Users/Malcolm/DropBox/A_codeUse/makehtmlUse/result :  exists
analysis <- "Schaefer"
resfile <- setuphtml(rundir=rundir) # creates resultTable.csv in rundir

# Some data
catch <- c(60913,72294,78353,91522,78288,110417,114590,76841,41965,
           50058,64094,89194,129701,160134,200340,192458,224810,183685,
           192234,138918,138623,140581)
effort <- c(5879,6295,6771,8233,6830,10488,10801,9584,5961,5930,6397,
            9377,13958,20381,23984,23013,31856,18726,31529,36423,
            24995,17806)
schaef <- as.data.frame(cbind(year=1934:1955,effort=effort,catch=catch,
                cpue=catch/effort)) # Schaefer's 1957 yellowfin data
# First save the data; obvioulsy you should use unique filenames
filen <- "schaef.csv"  # csv files only
addtable(intable=schaef,filen=filen,rundir=rundir,category="data",
         caption="Schaefer's original 1957 Yellowfin Tuna fishery data.")
# addtable logs the filename automatically into resultTable.csv
# sort the table on catch, just to provide two tables
filen <- "sortedschaef.csv"
sortyft <- schaef[order(schaef[,"catch"]),]
addtable(intable=sortyft,filen=filen,rundir=rundir,category="data",
         caption="The Schaefer 1957 data sorted on catch.")

# plot the timeseries of cpue and catch,  only png files
filename <- filenametopath(rundir,"Schaefer_Fisherydata.png")
plotprep(width=7,height=4.5,filename=filename,cex=0.9,verbose=FALSE)
parset(plots=c(2,1))
ymax <- getmax(schaef$cpue)
plot(schaef$year,schaef$cpue,type="l",lwd=2,ylim=c(0,ymax),
     panel.first=grid(),xlab="",ylab="CPUE")
ymax <- getmax(schaef$catch)
plot(schaef$year,schaef$catch,type="l",lwd=2,ylim=c(0,ymax),
     panel.first=grid(),xlab="",ylab="Catch")
caption <- "The Schaefer Yellowfin tuna fishery data from 1957."
addplot(filen=filename,rundir=rundir,category="Fishery",caption=caption)  

# plot the catch against the effort
filename <- filenametopath(rundir,"Schaefer_CatchvEffort.png")
caption <- "The Schaefer Yellowfin tuna data Catch vs Effort."
plotprep(width=7,height=3,filename=filename,cex=0.9,verbose=FALSE)
ymax <- getmax(schaef$catch)
plot(schaef$effort,schaef$catch,type="p",pch=16,cex=1.2,ylim=c(0,ymax),
     panel.first=grid(),xlab="Effort",ylab="Catch")
addplot(filen=filename,rundir=rundir,category="Fishery",caption=caption)  

# plot the effort data; if filename="" no png file is produced or added, but a
# new plot is certainly generated. Once as you want it, then use plotfilename.
filename <- filenametopath(rundir,"Schaefer_Effortdata.png")
caption <- "The Schaefer Yellowfin tuna effort data from 1957."
plotprep(width=7,height=4.5,newdev=FALSE,filename=filename,cex=0.9,
         verbose=FALSE)
ymax <- getmax(schaef$effort)
plot(schaef$year,schaef$effort,type="l",lwd=2,ylim=c(0,ymax),
     panel.first=grid(),xlab="",ylab="Effort Class 4 clipper days '000s")
addplot(filen=filename,rundir=rundir,category="Effort",caption=caption)  
txt <- c("Here is an example of text that might want to be added to one of ",
         "the html tabs. In this case, of course, the text is merely here to ",
         "illustrate how to do this and to see if I can develop a means to ",
         "format the material seperately by including custom CSS code.")
addtext(txt=txt,rundir,filename="test2.txt",category="Effort")

endtime <- as.character(Sys.time())

reportlist <- list(  #these 2 are minimal requirements for the replist
                     # though the whole of replist can be NULL if 
  starttime=starttime,  # you are feeling lazy.
  endtime=endtime 
)
str(reportlist,max.level = 1)
#> List of 2
#>  $ starttime: chr "2024-02-19 14:31:34.036428"
#>  $ endtime  : chr "2024-02-19 14:31:34.241203"

runnotes <- "This is merely to illustrate how to use the package."
# If you unhash the make_html component it will open the local
# website generated inside rundir, if openfile=FALSE it will be
# generated but not opened.

# make_html(replist=reportlist,rundir=rundir,width=500,openfile=TRUE,
#           runnotes=runnotes,verbose=FALSE,packagename="makehtml",
#           htmlname="Schaefer_Example")

About

Generic R code for producing multi-tabbed HTML output for plotted results in a specific directory

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages