Skip to content

Commit 672f5a7

Browse files
committed
20250520 - try statement documentation
1 parent add4745 commit 672f5a7

File tree

1 file changed

+94
-53
lines changed

1 file changed

+94
-53
lines changed

R.Rmd

Lines changed: 94 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -311,59 +311,6 @@ However, do make sure to save your `R` scripts before exiting Rstudio.
311311
- [Regression](regression.html)
312312
- [Structural Equation Modeling](sem.html)
313313

314-
# Troubleshooting {#troubleshooting}
315-
316-
To troubleshoot `R` issues, be resourceful.
317-
Try googling the error message or issue you are experiencing.
318-
See [here](#questions) for a list of places you can pose `R`-related questions for help.
319-
In addition, particular errors/warnings/issues are included below:
320-
321-
## General Troubleshooting Tips
322-
323-
1. **Run code line-by-line to identify the source of the error**
324-
- If you are using RStudio, a red line will often appear on the lefthand side of the screen to indicate the particular line(s) of code that result in an error
325-
- It may be useful to clear your working environment in R before trying to diagnose an error to be sure that you are not experiencing any interference from existing objects in the environment
326-
- Running code line-by-line is particularly useful because it allows you to inspect the data at each step of its processing
327-
- Data issues are commonly the culprit of code errors (see below)
328-
1. **Check for typos**
329-
- Are all brackets/parentheses/etc. closed?
330-
- Does the object/variable that you are referring to exist? (*If the object does not exisit, you will likely get an error message indicating `[object name] not found`*)
331-
- Has it been defined somewhere prior in the code?
332-
- Does it depend on something else to be created?
333-
- Is its name spelled correctly?
334-
- Highlighting the object or variable in question and using `CTRL-F` to search for it is a good way to check these things
335-
- Did you spell the function you want to use correctly?
336-
- Do you have commas separating items within a list/function/etc.?
337-
1. **Does your working environment have what it needs?**
338-
- Are all necessary packages/libraries installed?
339-
Do any require an update?
340-
- Are all expected objects (dataframes, lists, environment variables, etc) present?
341-
Are they correct?
342-
1. **Investigate the structure of your data**
343-
- Errors such as `non-numeric argument to binary operator` indicate that the function you are trying to use is expecting to recieve numeric data as an input but one or more of the inputs are not numeric
344-
- The function `class(data$variable)` (replace "data" and "variable" with whatever you are trying to invesitgate) is useful for determining how R is interpreting your data.
345-
Often, data that look like numbers can end up being stored as a character (text) variable.
346-
This can be due to import/export processes or due to an issue with data entry.
347-
- For example, when `00` is entered instead of `0` in a numeric field R is likely to interpret the variable as a character instead of a number when importing the data
348-
- Check to make sure that all expected rows/columns are present and that they look the way you expect them to
349-
- If your dataset has been created by joining/merging multiple dataframes, any duplicate columns not accounted for when joining may have a suffix appended to distinguish them (i.e., `variable.x` or `variable.y`)
350-
- To resolve this, either call the appended variable in subsequent manipulations (`variable.x` instead of `variable`) or deal with the duplicated columns before/during the joining process (e.g., include duplicate columns in the `by` argument of joining function or remove the redundant column from one of the datasets to be joined)
351-
- If you have been creating/computing variables in your dataset, ensure that they are being computed as expected
352-
- Check for `NA` (missing) or `NaN` (not a number) values.
353-
Such values may not be an issue or error in all cases, but if you are not expecting that as a result of your computations, this might suggest an issue with the code and/or data structure
354-
- Check whether the computed values are reasonable
355-
356-
## Warning: `PACKAGENAME` package in `FILEPATH` library will not be updated
357-
358-
This warning might indicate that it cannot update the package because it is part of the base `R` installation.
359-
To fix this:
360-
361-
- find and delete the package that is part of the base `R` installation in the `library` directory of the `R` installation (e.g., `C:\R\R-4.3.1\library\PACKAGENAME\`)
362-
- make sure the package is also not in any other package installation locations (e.g., `C:\R\Packages\PACKAGENAME\`); if it is, delete it from there as well
363-
- then, after deleting the package from these locations, restart `R` and run `install.packages("PACKAGENAME")` to reinstall the package
364-
- close `R`
365-
- if the package was part of the base `R` installation, move the installed package folder `C:\R\Packages\PACKAGENAME\` to the `library` directory of the `R` installation (e.g., `C:\R\R-4.3.1\library\PACKAGENAME\`)
366-
367314
# Running Scripts Automatically with Windows
368315

369316
https://www.spsanderson.com/steveondata/posts/2023-06-29/index.html (archived at https://perma.cc/9EXK-W99Y)
@@ -503,6 +450,37 @@ The instructions to download and configure `git` software can be found [here](gi
503450
push(repo, credentials = cred_user_pass("insertUsername", myPassword))
504451
```
505452
453+
# Using `try()` Statements {#try}
454+
455+
[`try()` statements](https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/try) can be very useful in cases where you would like code to continue running even if a certain line errors out.
456+
If the code inside of the statement results in an error, the `try()` statement will allow R to simply jump to the next line of code, rather than halting the entire process.
457+
458+
It is best practice to ensure that the smallest possible "unit" of code is wrapped in the `try()` statement rather than a large section.
459+
This is because the `try()` statement will treat everything within it as a "single" operation.
460+
Thus, an error ocurring early in the sequence within a `try()` statement will cause the remainder of the sequence to be skipped, and R will skip to the next line of code outside of the present `try()` statement.
461+
462+
For example:
463+
464+
The following code does NOT have `try()` statements around the smallest possible unit.
465+
If `file_b.R` were to error out, `file_c.R` would not run.
466+
In other words, `file_c.R` would be skipped because it occurs after `file_b.R` in the **same** `try()` statement.
467+
```
468+
try({
469+
source(path/to/file/file_a.R)
470+
source(path/to/file/file_b.R)
471+
source(path/to/file_c.R)
472+
})
473+
```
474+
475+
On the other hand, the revised code below IS using `try()` statements around the smallest possible unit.
476+
If `file_b.R` were to error out, `file_c.R` would then begin to run.
477+
This is because each individual file being "sourced" is wrapped in its **own** `try()` statement.
478+
```
479+
try({source(path/to/file/file_a.R)})
480+
try({source(path/to/file/file_b.R)})
481+
try({source(path/to/file_c.R)})
482+
```
483+
506484
# Reading Password Protected Excel Databases
507485
508486
A helpful post can be found here: <br>
@@ -796,3 +774,66 @@ Unofficial documentation:
796774
### For Licensing
797775

798776
- https://usethis.r-lib.org/reference/licenses.html
777+
778+
779+
# Troubleshooting {#troubleshooting}
780+
781+
To troubleshoot `R` issues, be resourceful.
782+
Try googling the error message or issue you are experiencing.
783+
See [here](#questions) for a list of places you can pose `R`-related questions for help.
784+
In addition, particular errors/warnings/issues are included below:
785+
786+
## General Troubleshooting Tips
787+
788+
1. **Run code line-by-line to identify the source of the error**
789+
- If you are using RStudio, a red line will often appear on the lefthand side of the screen to indicate the particular line(s) of code that result in an error
790+
- It may be useful to clear your working environment in R before trying to diagnose an error to be sure that you are not experiencing any interference from existing objects in the environment
791+
- Running code line-by-line is particularly useful because it allows you to inspect the data at each step of its processing
792+
- Data issues are commonly the culprit of code errors (see below)
793+
1. **Check for typos**
794+
- Are all brackets/parentheses/etc. closed?
795+
- Does the object/variable that you are referring to exist? (*If the object does not exisit, you will likely get an error message indicating `[object name] not found`*)
796+
- Has it been defined somewhere prior in the code?
797+
- Does it depend on something else to be created?
798+
- Is its name spelled correctly?
799+
- Highlighting the object or variable in question and using `CTRL-F` to search for it is a good way to check these things
800+
- Did you spell the function you want to use correctly?
801+
- Do you have commas separating items within a list/function/etc.?
802+
1. **Does your working environment have what it needs?**
803+
- Are all necessary packages/libraries installed?
804+
Do any require an update?
805+
- Are all expected objects (dataframes, lists, environment variables, etc) present?
806+
Are they correct?
807+
1. **Investigate the structure of your data**
808+
- Errors such as `non-numeric argument to binary operator` indicate that the function you are trying to use is expecting to recieve numeric data as an input but one or more of the inputs are not numeric
809+
- The function `class(data$variable)` (replace "data" and "variable" with whatever you are trying to invesitgate) is useful for determining how R is interpreting your data.
810+
Often, data that look like numbers can end up being stored as a character (text) variable.
811+
This can be due to import/export processes or due to an issue with data entry.
812+
- For example, when `00` is entered instead of `0` in a numeric field R is likely to interpret the variable as a character instead of a number when importing the data
813+
- Check to make sure that all expected rows/columns are present and that they look the way you expect them to
814+
- If your dataset has been created by joining/merging multiple dataframes, any duplicate columns not accounted for when joining may have a suffix appended to distinguish them (i.e., `variable.x` or `variable.y`)
815+
- To resolve this, either call the appended variable in subsequent manipulations (`variable.x` instead of `variable`) or deal with the duplicated columns before/during the joining process (e.g., include duplicate columns in the `by` argument of joining function or remove the redundant column from one of the datasets to be joined)
816+
- If you have been creating/computing variables in your dataset, ensure that they are being computed as expected
817+
- Check for `NA` (missing) or `NaN` (not a number) values.
818+
Such values may not be an issue or error in all cases, but if you are not expecting that as a result of your computations, this might suggest an issue with the code and/or data structure
819+
- Check whether the computed values are reasonable
820+
821+
## A single error preventing a Wiki site from rendering
822+
823+
Code can be wrapped in `try()` statements so that if an error occurs, the script will simply "skip" to the next line of code.
824+
These `try()` statements are particularly useful when rendering a site; if they are not used, a single error will halt the entire process. `try()` statements allow the code to continue running in spite of a line or two erroring out.
825+
826+
If you have already incorporated `try()` statements and are still experiencing fatal errors in code, check to make sure that the smallest possible "unit" of code is wrapped in `try()` statements, rather than large sections of code.
827+
See [here](#try) for more information.
828+
829+
830+
## Warning: `PACKAGENAME` package in `FILEPATH` library will not be updated
831+
832+
This warning might indicate that it cannot update the package because it is part of the base `R` installation.
833+
To fix this:
834+
835+
- find and delete the package that is part of the base `R` installation in the `library` directory of the `R` installation (e.g., `C:\R\R-4.3.1\library\PACKAGENAME\`)
836+
- make sure the package is also not in any other package installation locations (e.g., `C:\R\Packages\PACKAGENAME\`); if it is, delete it from there as well
837+
- then, after deleting the package from these locations, restart `R` and run `install.packages("PACKAGENAME")` to reinstall the package
838+
- close `R`
839+
- if the package was part of the base `R` installation, move the installed package folder `C:\R\Packages\PACKAGENAME\` to the `library` directory of the `R` installation (e.g., `C:\R\R-4.3.1\library\PACKAGENAME\`)

0 commit comments

Comments
 (0)