Can You Use the HEC-RAS Controller with R?

Many people who use the HEC-RAS Controller to automate HEC-RAS write their code in the Microsoft Excel VBA environment. This is probably because Breaking the HEC-RAS Code, which is the book that outlines how to use the HEC-RAS Controller was written for VBA. However, there are many people who are seeking to use other programming languages such as R to access the HEC-RAS Controller. I am familiar with the R programming language, so I tried to figure out how to use the HEC-RAS Controller in R.

In order to access the HEC-RAS Controller with R, I attempted to use the RDCOMClient library. However, I ran into a couple of issues including R’s limitations on numerical data types and issues opening an HEC-RAS project. Overall, I would recommend using Microsoft Excel VBA rather than R to access the HEC-RAS Controller.

In this article, I will talk about some of the issues I had accessing the HEC-RAS Controller with R. Hopefully, this will save other people some time.

If anyone has any recommendations for how to fix the problems described in this article, please let me know (engineerpaige17@gmail.com)!

About the HEC-RAS Controller

If you are reading this article, I am guessing you are interested in using the HEC-RAS Controller. The HEC-RAS application programming interface (API) is a library that contains three classes. Each class has its own set of procedures. The HEC-RAS Controller is one of these classes. HEC-RAS is compiled as a Component Object Module (COM), and the HEC-RAS Controller can be called by any program that can read a COM data definition language (DDL).

The HEC-RAS Controller contains many procedures that allow the user to do things such as define input data and retrieve output data externally. This allows you to automate certain tasks in HEC-RAS. For example, it would be useful for exercise that requires many iterations such as Monte Carlo simulations. The Monte Carlo method is a statistical method that involves random sampling and requires the model to be run thousands of times.

About the R Programming Language

Before discussing some of the issues I had using R to access the HEC-RAS Controller, it would be useful to know a bit about it. R is an open-source programming language. It runs on a variety of operating systems and was created primarily for statistical computing. The R Foundation, which is located in Vienna, Austria, is a non-profit organization responsible for overseeing the development of R. Personally, I like R because it is easy to use and produces attractive graphics very easily. The download/setup process is also very user-friendly.

Click here to download the most current version of R. Note that the R programming language is updated frequently, and there are times that some functions/libraries will not work with the newest versions.

R Studio

After installing R, I highly recommend downloading RStudio. RStudio is the integrated development environment (IDE) for R. It contains a console and tools for debugging, viewing your run history, and managing your workspace. Although R Studio and R go together, RStudio has no formal connection with the R Foundation. It was created by RStudio, Inc., which is a commercial business founded by JJ Allaire.

R Studio is available in two formats: RStudio Desktop and RStudio Server. RStudio Desktop allows you to run the program locally on your desktop, and RStudio Server allows you to access RStudio using a web browser. I have only used RStudio Desktop, and it works well.

R Studio

Issues I Had Accessing the HEC-RAS Controller with R

After doing some research online, the only source I could find for accessing the HEC-RAS Controller in R is this forum post. It appears that others were having issues. Nevertheless, I attempted to build upon the code discussed in the aforementioned post. The code I was trying to run is shown below. I used version 3.6 of R.

library(RDCOMClient)
library(gmp)
library(bit64)

HRC <- COMCreate("RAS507.HECRASController")
strRASProject = "C:/Users/myname/Documents/HEC Data/Example Projects/1D Unsteady Flow Hydraulics/Balde Eagle Creek/BaldEagle.prj"
HRC$Project_Open(strRASProject)

HRC$CurrentGeomFile()
HRC$CurrentPlanFile()
HRC$CurrentSteadyFile()
HRC$CurrentUnsteadyFile()
HRC$CurrentProjectTitle()

lngMessages = as.bigq(as.numeric())
strMessages = ""
HRC$Compute_ShowComputationWindow()
HRC$Compute_CurrentPlan(lngMessages, strMessages)

I was not able to use R to run HEC-RAS. The list below summarizes some of the issues I encountered.

Data Type Issues

You have to explicitly specify long number data types in R. By default, numbers are stored as a double data type rather than long in R. This is an issue when running the Compute_CurrentPlan function because the variable lngMessages must be stored as a number of the long data type.

I was able to get around this by using the as.bigq function in the gmp package.

Opening a Project

In addition, I had trouble actually opening a project. I tried using one slash as well as two slashes. I also tried several projects, but I was never able to successfully open a project. When running the Project_Open command, R only returned “NULL.” This means that I was not able to retrieve a current geometry file, flow file, plan file, etc.

When I removed the file name, R opened HEC-RAS.

Opening a project is an essential first step in automating HEC-RAS. Unfortunately, using R to access the HEC-RAS Controller is infeasible if this function will not work.

Running Compute_CurrentPlan

Finally, I kept getting an error when I ran the Compute_CurrentPlan command. After correcting the errors related to data types, this error kept appearing.

Honestly, I did not find a way to resolve this issue. I also noticed that the person who posted their R code in the forum had problems with this function as well.

Final Thoughts

Although learning Excel VBA will take some time, I believe it will take less time than messing with R. Because HEC-RAS is written in Visual Basic, trying to use the HEC-RAS Controller in R is like fitting a square peg in a round hole. In addition, using Microsoft Excel VBA makes your code more accessible to others in the engineering community. Almost everyone has Excel on their work computer meaning that most people can open a spreadsheet containing a macro. In contrast, most engineers do not have R or Python installed on their machines making them unable to run your R or Python scripts without going through a lengthy setup process.

That being said, I hope somebody else figures out how to access the HEC-RAS Controller in R because I would like to present HEC-RAS data using the graphing capabilities available in R.

Related Questions

Can you use the HEC-RAS Controller in Python?

It appears that others have successfully used Python to automate HEC-RAS. I have tested this Python code posted on GitHub, and it appears that it works.

Can you use the HEC-RAS Controller in MATLAB?

If you are a MATLAB user, check out this journal article written by Arturo S. Leon and Chris Goodell. The paper provides MATLAB scripts to write input files, read output files, and automate functions in HEC-RAS.

Leave a Comment