Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wagnerds committed May 1, 2024
2 parents ee9b3ea + e6934d2 commit fe782e2
Show file tree
Hide file tree
Showing 17 changed files with 9,061 additions and 8,708 deletions.
11 changes: 6 additions & 5 deletions fvsOL/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
Package: fvsOL
Title: Forest Vegetation Simulator
Version: 2023.07.28
Version: 2024.04.01
Authors@R: c(person("Nicholas", "Crookston", email = "[email protected]",
role = c("aut")),
person("FVS", "Staff", email = "[email protected]",
role = c("aut", "cre")))
Description: An R-Shiny interface to the Forest Vegetation Simulator which can be
run as an "Online" or "Onlocal" configuration.
Depends: R (>= 4.0.0), rFVS (>= 2021.03.15), shiny (>= 1.6.0), Cairo (>= 1.5.11),
rhandsontable (>= 0.3.7), ggplot2 (>= 3.3.3), parallel (>= 4.0.0),
RSQLite (>= 2.2.4), plyr (>= 1.8.6), dplyr (>= 1.0.4), colourpicker (>= 1.1.0),
rgl (>= 0.105.0), leaflet (>= 2.0.4.1), zip (>= 2.1.1), openxlsx (>= 4.2.3)
Depends:
R (>= 4.0.0), rFVS (>= 2021.03.15), shiny (>= 1.6.0), Cairo (>= 1.5.11), rhandsontable (>= 0.3.7),
ggplot2 (>= 3.3.3), parallel (>= 4.0.0), RSQLite (>= 2.2.4), plyr (>= 1.8.6), dplyr (>= 1.0.4),
colourpicker (>= 1.1.0), rgl (>= 0.105.0), leaflet (>= 2.0.4.1), zip (>= 2.1.1), openxlsx (>= 4.2.3),
shinyFiles (>= 0.9.3)
Suggests: rgdal (>= 1.5-23), nlme (>= 3.1-140)
License: MIT
Roxygen: list(markdown = TRUE)
Expand Down
96 changes: 96 additions & 0 deletions fvsOL/R/change_project_dir.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
####################################################################################
# change_project_dir - r.20230711
#
# Accepts path to directory
#
# Opens new interface instance with last modified project in chosen directory
#
# makes heavy usage of code from server.R for input input$PrjOpen
#
# Function does not verify user access to chosen directory,
# must be handled prior to function call
####################################################################################


change_project_dir <- function(new_proj_dir) {
cat(paste0("User Specified Dir: ", new_proj_dir))

if (dir.exists(new_proj_dir)) {
if (isLocal()) {
if (exists("RscriptLocation")) {
rscript= RscriptLocation
} else {
exe_file = normalizePath(commandArgs(trailingOnly = FALSE)[1])

if (.Platform$OS.type == "windows") {
bin = regexpr("\\\\bin\\\\", exe_file)
} else {
bin = regexpr("/bin/", exe_file)
}

bin = substr(exe_file, 1, bin + attr(bin, "match.length") - 2)

if (.Platform$OS.type == "windows") {
file.path(bin,"Rscript.exe")
} else {
file.path(bin,"Rscript")
}
}

rscript = gsub("\\\\", "/", rscript)
defs = paste0("RscriptLocation='", rscript, "';")

if (exists("mdbToolsDir")) {
defs = paste0(defs, "mdbToolsDir='", mdbToolsDir, "';")
}

if (exists("sqlite3exe")) {
defs = paste0(defs, "sqlite3exe='", sqlite3exe, "';")
}

cat(".libPaths=", unlist(.libPaths()), "\n")

if (exists("RscriptLocation")) {
Rlib2Use <-
paste0(dirname(dirname(dirname(RscriptLocation))), "/library")
defs = paste0(defs, ".libPaths('", Rlib2Use, "');")
}

# Get list of projects in supplied Directory
prjs = list()
dirs = dir(new_proj_dir)
for (dir in dirs) {
if (file.exists(paste0(new_proj_dir, "/", dir, "/projectId.txt"))){
prjs = append(prjs, paste0(new_proj_dir, "/", dir))
prjs <- as.character((prjs))
}
}

if (!length(prjs)) {
if (file.exists(paste0(new_proj_dir, "/Project_1/projectId.txt"))) {
#Display notice of locked project
} else {
#Create new project_1 directory and launch
dir.create(paste0(new_proj_dir, "/Project_1"))
write(file = paste0(new_proj_dir, "/Project_1/projectId.txt"),
"title= Project_1")
prjs = append(prjs, paste0(new_proj_dir, "/Project_1"))
}
}

ord = sort(unlist(lapply(prjs, function(x) as.integer(file.mtime(x)))),
decreasing = TRUE, index.return = TRUE)$ix

cmd = paste0("$",rscript,"$ --vanilla -e $", defs, "require(fvsOL)",
";fvsOL(prjDir='", prjs[ord[1]], "',fvsBin='", fvsBin, "');
quit()$")
cmd = gsub('$', '\"', cmd, fixed=TRUE)

if (.Platform$OS.type == "unix") {
cmd = paste0("nohup ", cmd, " >> /dev/null")
}
rtn=try(system(cmd, wait=FALSE))
cat("cmd for launch project=", cmd, "\nrtn=", rtn, "\n")
}
}
}
142 changes: 142 additions & 0 deletions fvsOL/R/checkMinColumnDefs.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
checkMinColumnDefs <- function(dbo, progress = NULL, pn = 0) {
cat("In checkMinColumnDefs\n")
# Set up list of column names that are automatic database rejection
reject_if_missing <- c("Variant", "Stand_ID", "Inv_Year")

# Set up data frame to dub in required columns if missing
StdInitColumnReq <- data.frame(
ColumnName = c("Groups", "FVSKeywords", "Sam_Wt"),
DataType = c("text", "text", "real"),
Default = c("'All_Stands'", NA, NA)
)

FVS_StandInit = FALSE
valid_table_found = FALSE
FIA_dataset = FALSE
FIA_Tables = c("FVS_StandInit_Plot", "FVS_PlotInit_Plot", "FVS_StandInit_Cond")
for (table in FIA_Tables) {
tryCatch(
# Try to read table from db connection
{
stdInit <- dbReadTable(dbo, table)
FIA_dataset = TRUE
},

# if an error . . .
error = function(e) {
cat(paste0("Table: ", table, " not found in database\n"))
}
)
}
if (FIA_dataset) return(NULL)

for (initnm in c("FVS_StandInit", "FVS_PlotInit")) {
initnm_exists = FALSE
tryCatch(
# Try to read table from db connection
{
stdInit <- dbReadTable(dbo, initnm)
initnm_exists = TRUE
valid_table_found = TRUE
},

# if an error . . .
error = function(e) {
cat(paste0("Table: ", initnm, " not found in database\n"))
}
)
if (initnm_exists) {
if (initnm == "FVS_StandInit") FVS_StandInit = TRUE

# get list of column names in table
fields <- tolower(names(stdInit))
reject = reject_if_missing

if (initnm == 'FVS_PlotInit' || initnm == 'FVS_PlotInit_Plot') {
plotInit = c("StandPlot_ID")
reject = c(reject_if_missing, plotInit)
}


# Check for missing required columns that would reject database
for (e in reject) {
if (!is.null(progress)) {
pn = pn+1
progress$set(message = paste0("Checking ", initnm), value = pn,
detail = e)
}

if (!(tolower(e) %in% fields)) {
return(paste0("<h4>Input database invalid.<br>",
initnm, " table Missing column: '", e, "'</h4>"))
}

# Add logic to check for blank entries
# Note: Sqlite extensions 'math', 'regexp', 'series', 'csv' not enabled by default
RSQLite::initExtension(dbo, extension = c('regexp'))
q <- paste0("SELECT COUNT(*) FROM ", initnm, " WHERE ", e,
" NOT REGEXP '[A-Za-z0-9_]' OR ", e, " IS NULL")

if (tolower(e) == 'inv_year'){
q <- paste0("SELECT COUNT (*) FROM ", initnm, " WHERE CAST(INV_YEAR AS INT) <= 0 OR INV_YEAR IS NULL")
}
tryCatch(
{
result <- dbGetQuery(dbo, q)
if (result > 0) {
return(paste0("<h4>Input database invalid.<br>",
initnm, " Column ", e, " contains a blank or missing value</h4>"))
}
},
error = function(e) {
return(paste0("Attempt to read column: ", e, "Failed."))
}
)
}

for (reqColumn in StdInitColumnReq$ColumnName) {
type = StdInitColumnReq$DataType[
StdInitColumnReq$ColumnName == reqColumn]

default = StdInitColumnReq$Default[
StdInitColumnReq$ColumnName == reqColumn]

if (reqColumn == "Groups" &&
(initnm == "FVS_PlotInit" || initnm == "FVS_PlotInit_Plot")) {
default = "'All_Plots'"
}

if (!is.null(progress)) {
pn = pn + 1
progress$set(message = paste0("Checking ", initnm),
value = pn, detail = reqColumn)
}

if (!(tolower(reqColumn) %in% fields)) {

tryCatch(
{
if(!is.na(default)){
dbExecute(dbo, paste0("alter table ", initnm, " add column ",
reqColumn, " ", type, " default ", default, ";"))
}
else{
dbExecute(dbo, paste0("alter table ", initnm, " add column ",
reqColumn, " ", type, ";"))
}

},
error = function(e) {
return(paste0("Attempt to add column: ", reqColumn, "Failed."))
}
)
}
}
}
}
if (!valid_table_found) {
return(paste0("<h4>Input database invalid.<br>",
"No valid StandInit or PlotInit tables found</h4>"))
}
return(NULL)
}
118 changes: 0 additions & 118 deletions fvsOL/R/editDataUtilities.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,124 +27,6 @@ cat(" qry=",qry,"\n")
dbGlb$tbl$Delete = FALSE
}

checkMinColumnDefs <- function(dbo,progress=NULL,pn=0)
{
cat ("in checkMinColumnDefs\n")
for (initnm in c("FVS_StandInit","FVS_PlotInit","FVS_StandInit_Cond"))
{
stdInit = getTableName(dbo,initnm)
if (!is.null(stdInit)) break
}
cat ("stdInit=",stdInit,"\n")
if (is.null(stdInit)) return("No standinit table was found.")
fields = try(dbListFields(dbo,stdInit))
# if this is an error, then FVS_StandInit does not exist and this is an error
# where the standard fixup in this case is to try recovery of the database.
if (class(fields) == "try-error") return("Geting column names from StandInit table failed.")
sID = FALSE
sCN = FALSE
grp = FALSE
# make sure groups are defined, if missing set one to "All_Stands"
if (!is.null(progress)) progress$set(message = paste0("Checking ",stdInit),
value = pn+1, detail = "Groups")
if (length(grep("Groups",fields,ignore.case=TRUE)) == 0)
{
qt = try(dbExecute(dbo,paste0("alter table '",stdInit,
"' add column Groups text default 'All All_Stands'")))
if (class(qt)=="try-error") return ("Adding group 'All All_Stands' to StandInit failed.")
grp = TRUE
}
# make sure Stand_ID is defined
if (!is.null(progress)) progress$set(message = paste0("Checking ",stdInit),
value = pn+2, detail = "Stand_ID")
if (length(grep("Stand_ID",fields,ignore.case=TRUE)) == 0)
{
qt = try(dbExecute(dbo,paste0("alter table '",stdInit,
"' add column Stand_ID text")))
if (class(qt)=="try-error") return ("Adding 'Stand_ID' to StandInit failed.")
sID = TRUE
}
# make sure Stand_CN is defined
if (!is.null(progress)) progress$set(message = paste0("Checking ",stdInit),
value = pn+3, detail = "Stand_CN")
if (length(grep("Stand_CN",fields,ignore.case=TRUE)) == 0)
{
qt = try(dbExecute(dbo,paste0("alter table '",stdInit,
"' add column Stand_CN text")))
if (class(qt)=="try-error") return ("Adding 'Stand_CN' to StandInit failed.")
sCN = TRUE
}
# make sure Inv_Year is defined
if (!is.null(progress)) progress$set(message = paste0("Checking ",stdInit),
value = pn+4, detail = "Inv_Year ")
if (length(grep("Inv_Year",fields,ignore.case=TRUE)) == 0)
{
year=substring(as.character(Sys.time()),1,4)
qt=try((dbExecute(dbo,paste0(paste0("alter table '",stdInit,
"' add column Inv_Year integer default ",year)))))
if (class(qt)=="try-error") return ("Adding 'Inv_Year' to StandInit failed.")
}
# make sure FVSKeywords is defined
if (!is.null(progress)) progress$set(message = paste0("Checking ",stdInit),
value = pn+5, detail = "FVSKeywords ")
if (length(grep("FVSKeywords",fields,ignore.case=TRUE)) == 0)
{
year=substring(as.character(Sys.time()),1,4)
qt=try(dbExecute(dbo,paste0("alter table '",stdInit,
"' add column FVSKeywords text")))
if (class(qt)=="try-error") return ("Adding 'FVSKeywords' to StandInit failed.")
}
# make sure Sam_Wt is defined
if (!is.null(progress)) progress$set(message = paste0("Checking ",stdInit),
value = pn+6, detail = "Sam_Wt")
if (length(grep("Sam_Wt",fields,ignore.case=TRUE)) == 0)
{
qt=try(dbExecute(dbo,paste0("alter table ",stdInit,
" add column Sam_Wt real")))
if (class(qt)=="try-error") return ("Adding 'Sam_Wt' to StandInit failed.")
}
cat ("in checkMinColumnDefs sID=",sID," sCN=",sCN,"\n")
if (sID || sCN)
{
fvsInit = try(dbReadTable(dbo,stdInit))
if (class(fvsInit)=="try-error") return ("Can not read StandInit.")
if (!is.null(progress)) progress$set(message = paste0("Checking ",stdInit),
value = pn+7, detail = "Stand_ID and Stand_CN consistent")
if (nrow(fvsInit))
{
isCN = grep("Stand_CN",names(fvsInit),ignore.case=TRUE)
if (sID) fvsInit$Stand_ID =
if (sCN) paste0("Stand",1:nrow(fvsInit)) else fvsInit[,isCN]
isID = grep("Stand_ID",names(fvsInit),ignore.case=TRUE)
if (sCN)
{
isCN = grep("Stand_CN",names(fvsInit),ignore.case=TRUE)
fvsInit[,isCN] = fvsInit[,isID]
isID = grep("Stand_ID",names(fvsInit),ignore.case=TRUE)
fvsInit[,isCN] = fvsInit[,isID]
}
dbWriteTable(dbo,stdInit,fvsInit,overwrite=TRUE)
}
}
# check groups
if (!grp)
{
if (!is.null(progress)) progress$set(message = paste0("Checking ",stdInit),
value = pn+8, detail = "Groups content")
grps = try(dbGetQuery(dbo,paste0("select Groups from '",stdInit,"'")))
if (class(grps)=="try-error") return ("Can not read Groups from StandInit.")
names(grps) = toupper(names(grps))
if (is.null(grps$GROUPS) || any(is.na(grps$GROUPS)) || any(grps$GROUPS == ""))
{
qt =try(dbExecute(dbo,paste0("update '",stdInit,
" set Groups = 'All_Stands' where Groups = ''")))
if (class(qt)=="try-error") return ("Failure updating 'Groups' in StandInit.")
}
}
return(NULL)
}


fixFVSKeywords <- function(dbo)
{
tbs <- dbGetQuery(dbo,"select name from sqlite_master where type='table';")[,1]
Expand Down
Loading

0 comments on commit fe782e2

Please sign in to comment.