1 Shell and System Commands

Go to the RMD, R, PDF, or HTML version of this file. Go back to fan’s REconTools Package, R Code Examples Repository (bookdown site), or Intro Stats with R Repository (bookdown site).

1.1 Basic Shell Commands

Run basic shell commands in windows:

# detect current path
print(toString(shell(paste0("echo %cd%"), intern=TRUE)))
## [1] "C:\\Users\\fan\\R4Econ\\development\\system"
# Show directory
print(toString(shell(paste0("dir"), intern=TRUE)))
## [1] " Volume in drive C has no label.,  Volume Serial Number is 024A-FEC0, ,  Directory of C:\\Users\\fan\\R4Econ\\development\\system, , 11/11/2020  11:46 AM    <DIR>          ., 11/11/2020  11:46 AM    <DIR>          .., 11/11/2020  11:45 AM             1,674 fs_system_shell.Rmd, 11/11/2020  11:45 AM             1,798 fs_system_shell_mod.Rmd, 11/11/2020  11:46 AM    <DIR>          htmlpdfr, 11/11/2020  10:27 AM                17 main.Rmd,                3 File(s)          3,489 bytes,                3 Dir(s)  58,421,170,176 bytes free"

1.2 Run Python Inside a Conda Environment

Use shell rather than system to activate a conda environment, check python version:

# activate conda env
print(toString(shell(paste0("activate base & python --version"), intern=TRUE)))
## [1] "Python 3.7.9"

Activate conda env and run a line:

spg_runpython <- paste0("activate base &",
                        "python --version &",
                        "python -c ",
                        "\"st_var='this is string var';",
                        "print(f'{st_var}');",
                        "\"")
print(toString(shell(spg_runpython, intern=TRUE)))
## [1] "Python 3.7.9, this is string var"