Skip to content

Commit

Permalink
Updating the part about log files
Browse files Browse the repository at this point in the history
  • Loading branch information
larsvilhuber committed Oct 14, 2024
1 parent 73279ce commit d942bf5
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 347 deletions.
40 changes: 37 additions & 3 deletions 04-02-creating_log_files_automatically.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,52 @@ On Windows, follow instructions [here](https://www.stata.com/manuals/gswb.pdf#gs

::::{tab-item} R

To automatically create a log file, run R from the command line as follows:
To automatically create a log file, run R from the command line using the `BATCH` functionality, as follows:

```bash
R CMD BATCH main.R
R CMD BATCH options infile outfile
```

where

- `options` are optional _options_ from the command R

- `infile` is the required input file with the code to be executed

- `outfile` is the name of an optional output file. If no output file
is provided, the name of `infile` is taken as default, appending
the extension `.Rout` to it.


:::{warning}
On Windows, you may need to include the full path of R: `C:\Program Files\R\R-4.1.0\bin\R.exe CMD BATCH main.R`
:::


This will create a file `main.Rout` in the same directory as `main.R`. If you prefer a different name for the output file, you can specify it.

```bash
R CMD BATCH main.R main.$(date +%F-%H:%M:%S).Rout
```

which will create a second-precise date-time stamped log file. Finally, if you want to prevent R from saving or restoring its environment (by default, `R CMD BATCH` does both), you can specify the `--no-save` and `--no-restore` options.

```bash
R CMD BATCH --no-save --no-restore main.R main.$(date +%F-%H:%M:%S).Rout
```

will create a file `main.Rout` in the same directory as `main.R`.

:::{warning}
If there are other commands, such as `sink()`, active in the R code, the `main.Rout` file will not contain some output.
:::


> To see more information, check the manual documentation by typing
`?BATCH` (or `help(BATCH)`) from within an R interactive session. Or by
typing `R CMD BATCH --help` from the command line.



::::

::::{tab-item} MATLAB
Expand Down
118 changes: 102 additions & 16 deletions presentation/04-creating_log_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,14 @@ log using "`globallog'", name(global) replace text
- While some software (Stata, MATLAB) will create log files that contain **commands and output**, others (R, Python) will create log files that contain **only output**.


### Creating log files automatically
# Creating log files automatically

An alternative (or complement) to creating log files explicitly is to use native functionality of the software to create them. This usually is triggered when using the command line to run the software, and thus may be considered an **advanced topic.** The examples below are for Linux/macOS, but similar functionality exists for Windows.


:::: {.panel-tabset}


### Stata
## Stata

To automatically create a log file, run Stata from the command line with the `-b` option:

Expand All @@ -138,37 +137,121 @@ stata -b do main.do

which will create a file `main.log` in the same directory as `main.do`.

```{.notes}
For this to work, the filename cannot include spaces.
> - For this to work, the filename cannot include spaces.
> - On Windows, follow instructions [here](https://www.stata.com/manuals/gswb.pdf#gswB.5).





















## R {auto-animate=true transition="fade"}

To automatically create a log file, run R from the command line using the `BATCH` functionality, as follows:

```bash
R CMD BATCH options infile outfile
```

> On Windows, you may need to include the full path of R:
```
On Windows, follow instructions [here](https://www.stata.com/manuals/gswb.pdf#gswB.5).
C:\Program Files\R\R-4.1.0\bin\R.exe CMD BATCH main.R
```


## R {auto-animate=true transition="fade"}

To automatically create a log file, run R from the command line using the `BATCH` functionality, as follows:

```bash
R CMD BATCH options infile outfile
```

### R
This will create a file `main.Rout` in the same directory as `main.R`.

To automatically create a log file, run R from the command line as follows:
## R {auto-animate=true transition="fade"}

If you prefer a different name for the output file, you can specify it.

```bash
R CMD BATCH main.R
R CMD BATCH main.R main.$(date +%F-%H:%M:%S).Rout
```

will create a file `main.Rout` in the same directory as `main.R`.
which will create a second-precise date-time stamped log file.

```{.notes}
If there are other commands, such as `sink()`, active in the R code, the `main.Rout` file will not contain some output.
## R {auto-animate=true transition="fade"}

If you want to prevent R from saving or restoring its environment (by default, `R CMD BATCH` does both), you can specify the `--no-save` and `--no-restore` options.

```bash
R CMD BATCH --no-save --no-restore main.R main.$(date +%F-%H:%M:%S).Rout
```


## R {.smaller}

:::{warning}
If there are other commands, such as `sink()`, active in the R code, the `main.Rout` file will not contain some output.
:::

## R {.smaller}

> To see more information, check the manual documentation by typing
`?BATCH` (or `help(BATCH)`) from within an R interactive session. Or by
typing `R CMD BATCH --help` from the command line.














### MATLAB












## MATLAB {auto-animate=true}

To automatically create a log file, run MATLAB from the command line as follows:

```bash
matlab -nodisplay -r "addpath(genpath('.')); main" -logfile matlab.log
```

## MATLAB {auto-animate=true transition="fade"}

A similar command on Windows would be:

```bash
Expand All @@ -177,23 +260,26 @@ start matlab -nosplash -minimize -r "addpath(genpath('.'));main" -logfile mat



### Julia, Python
## Julia, Python {auto-animate=true transition="fade"}

In order to capture screen output in Julia and Python, on Unix-like system (Linux, macOS), the following can be run:

```bash
julia main.jl | tee main.log
```

or
which will create a log file with everything that would normally appear on the console using the `tee` command.

## Julia, Python {auto-animate=true transition="fade"}

In order to capture screen output in Julia and Python, on Unix-like system (Linux, macOS), the following can be run:

```bash
python main.py | tee main.log
```

which will create a log file with everything that would normally appear on the console using the `tee` command.

::::



Expand Down
Loading

0 comments on commit d942bf5

Please sign in to comment.