1594578060

# Only R Matters: mathematical epidemiology and infection rate

What does the history of mathematical epidemiology have to do with a modern pandemic? Everything really. Not much has changed except how we graphically represent it — namely with Graph — and its capacity to handle larger, world-reaching datasets. Hopefully, this piece will give you a primer to help you begin to understand the struggle we’re in today as we try to solve a mathematical problem like COVID-19.A brief history of mathematical epidemiologyMathematical modelling of epidemics dates back as far as the 18th-century Swiss mathematician Daniel Bernoulli, who, apparently not content with fundamental work in mechanics, statistics and economics, also created the first known mathematical model of the spread of an epidemic. His model gave a life expectancy in terms of the infection rate and the mortality rate, applied to the outbreaks of smallpox that were then prevalent throughout Europe.It was another disease which inspired what many consider the foundation of the science of epidemiology, namely cholera. Cholera outbreaks were a quite frequent phenomena in the 1800s in London, due to inadequate infrastructure, overcrowding, and a lack of an accurate medical model of germs and infection. It seems remarkable now, but there were two competing theories at the time: germ theory, and miasma theory. Miasma theory held that cholera, and other diseases, was due to decaying organic matter transmitted in the air — perhaps not as odd a theory as it might seem today, given the pollution then prevalent in central London, and the lack of a sewage system throughout many parts of the capital.John Snow was, in many ways, the first data scientist. He created a map of infections of an outbreak in Soho, around Broad Street, in 1854. He rejected the prevailing miasma theory, and concentrated instead on tracing the spread of cholera through the water system. He traced infections using what would now be called a Voronoi diagram. Eventually, he posited a particular water pump as the likely source of the cholera outbreak. His work provided empirical evidence for the germ theory, and established water supplies as the primary transmission agent for cholera. His work also led to major revisions in public health legislation concerning the provision of clean water and safe waste disposal. There is a pub in Soho, close to the water pump which was ground zero for the Broad Street infection, which bears his name.Many epidemiological models have been developed since this seminal work, including systems of differential equations, stochastic processes, and those based on graphs. We will look at how graph theory can be applied as a model of the spread of disease.What is a graph?Graph theory as an object of mathematical inquiry dates back to a problem known as the Seven Bridges of Königsberg. It is a problem which sounds quite simple to state, but its solution led Leonhard Euler to develop a wholly new mathematical theory in order to arrive at a definitive answer. While solving this problem, Euler invented graph theory; this is the sort of thing Euler did rather a lot of, making fundamental contributions to physics, astronomy, geography, logistics, topology, number theory, calculus, and an eye-watering number of other fields.Two rivers ran through the city of Königsberg, and seven bridges crossed them. The problem which preoccupied Euler, and led to the discovery of graph theory, was to create a walk which crossed the each of the bridges once, but only once. A map of Königsberg is given below, with the rivers given in blue and the bridges in red.

#data-science #epidemiology #health #graph-theory #mathematics #data analysis

1649209980

## C# REPL

A cross-platform command line REPL for the rapid experimentation and exploration of C#. It supports intellisense, installing NuGet packages, and referencing local .NET projects and assemblies.

(click to view animation)

C# REPL provides the following features:

• Syntax highlighting via ANSI escape sequences
• Intellisense with fly-out documentation
• Nuget package installation
• Reference local assemblies, solutions, and projects
• Navigate to source via Source Link
• IL disassembly (both Debug and Release mode)
• Fast and flicker-free rendering. A "diff" algorithm is used to only render what's changed.

## Installation

C# REPL is a .NET 6 global tool, and runs on Windows 10, Mac OS, and Linux. It can be installed via:

``````dotnet tool install -g csharprepl
``````

If you're running on Mac OS Catalina (10.15) or later, make sure you follow any additional directions printed to the screen. You may need to update your PATH variable in order to use .NET global tools.

After installation is complete, run `csharprepl` to begin. C# REPL can be updated via `dotnet tool update -g csharprepl`.

## Usage:

Run `csharprepl` from the command line to begin an interactive session. The default colorscheme uses the color palette defined by your terminal, but these colors can be changed using a `theme.json` file provided as a command line argument.

### Evaluating Code

Type some C# into the prompt and press Enter to run it. The result, if any, will be printed:

``````> Console.WriteLine("Hello World")
Hello World

[6/7/2021 5:13:00 PM]
``````

To evaluate multiple lines of code, use Shift+Enter to insert a newline:

``````> var x = 5;
var y = 8;
x * y
40
``````

Additionally, if the statement is not a "complete statement" a newline will automatically be inserted when Enter is pressed. For example, in the below code, the first line is not a syntactically complete statement, so when we press enter we'll go down to a new line:

``````> if (x == 5)
| // caret position, after we press Enter on Line 1
``````

Finally, pressing Ctrl+Enter will show a "detailed view" of the result. For example, for the `DateTime.Now` expression below, on the first line we pressed Enter, and on the second line we pressed Ctrl+Enter to view more detailed output:

``````> DateTime.Now // Pressing Enter shows a reasonable representation
[5/30/2021 5:13:00 PM]

> DateTime.Now // Pressing Ctrl+Enter shows a detailed representation
[5/30/2021 5:13:00 PM] {
Date: [5/30/2021 12:00:00 AM],
Day: 30,
DayOfWeek: Sunday,
DayOfYear: 150,
Hour: 17,
InternalKind: 9223372036854775808,
InternalTicks: 637579915804530992,
Kind: Local,
Millisecond: 453,
Minute: 13,
Month: 5,
Second: 0,
Ticks: 637579915804530992,
TimeOfDay: [17:13:00.4530992],
Year: 2021,
_dateData: 9860951952659306800
}
``````

A note on semicolons: C# expressions do not require semicolons, but statements do. If a statement is missing a required semicolon, a newline will be added instead of trying to run the syntatically incomplete statement; simply type the semicolon to complete the statement.

``````> var now = DateTime.Now; // assignment statement, semicolon required

> DateTime.Now.AddDays(8) // expression, we don't need a semicolon
[6/7/2021 5:03:05 PM]
``````

### Keyboard Shortcuts

• Basic Usage
• Ctrl+C - Cancel current line
• Ctrl+L - Clear screen
• Enter - Evaluate the current line if it's a syntactically complete statement; otherwise add a newline
• Control+Enter - Evaluate the current line, and return a more detailed representation of the result
• Shift+Enter - Insert a new line (this does not currently work on Linux or Mac OS; Hopefully this will work in .NET 7)
• Ctrl+Shift+C - Copy current line to clipboard
• Ctrl+V, Shift+Insert, and Ctrl+Shift+V - Paste text to prompt. Automatically trims leading indent
• Code Actions
• F1 - Opens the MSDN documentation for the class/method under the caret (example)
• F9 - Shows the IL (intermediate language) for the current statement in Debug mode.
• Ctrl+F9 - Shows the IL for the current statement with Release mode optimizations.
• F12 - Opens the source code in the browser for the class/method under the caret, if the assembly supports Source Link.
• Autocompletion
• Ctrl+Space - Open autocomplete menu. If there's a single option, pressing Ctrl+Space again will select the option
• Enter, Right Arrow, Tab - Select active autocompletion option
• Escape - closes autocomplete menu
• Home and End - Navigate to beginning of a single line and end of a single line, respectively
• Ctrl+Home and Ctrl+End - Navigate to beginning of line and end across multiple lines in a multiline prompt, respectively
• Arrows - Navigate characters within text
• Ctrl+Arrows - Navigate words within text
• Ctrl+Backspace - Delete previous word
• Ctrl+Delete - Delete next word

Use the `#r` command to add assembly or nuget references.

• For assembly references, run `#r "AssemblyName"` or `#r "path/to/assembly.dll"`
• For project references, run `#r "path/to/project.csproj"`. Solution files (.sln) can also be referenced.
• For nuget references, run `#r "nuget: PackageName"` to install the latest version of a package, or `#r "nuget: PackageName, 13.0.5"` to install a specific version (13.0.5 in this case).

To run ASP.NET applications inside the REPL, start the `csharprepl `application with the `--framework` parameter, specifying the `Microsoft.AspNetCore.App` shared framework. Then, use the above `#r` command to reference the application DLL. See the Command Line Configuration section below for more details.

``````csharprepl --framework  Microsoft.AspNetCore.App
``````

## Command Line Configuration

The C# REPL supports multiple configuration flags to control startup, behavior, and appearance:

``````csharprepl [OPTIONS] [response-file.rsp] [script-file.csx] [-- <additional-arguments>]
``````

Supported options are:

• OPTIONS:
• `-r <dll>` or `--reference <dll>`: Reference an assembly, project file, or nuget package. Can be specified multiple times. Uses the same syntax as `#r` statements inside the REPL. For example, `csharprepl -r "nuget:Newtonsoft.Json" "path/to/myproj.csproj"`
• When an assembly or project is referenced, assemblies in the containing directory will be added to the assembly search path. This means that you don't need to manually add references to all of your assembly's dependencies (e.g. other references and nuget packages). Referencing the main entry assembly is enough.
• `-u <namespace>` or `--using <namespace>`: Add a using statement. Can be specified multiple times.
• `-f <framework>` or `--framework <framework>`: Reference a shared framework. The available shared frameworks depends on the local .NET installation, and can be useful when running an ASP.NET application from the REPL. Example frameworks are:
• Microsoft.NETCore.App (default)
• Microsoft.AspNetCore.All
• Microsoft.AspNetCore.App
• Microsoft.WindowsDesktop.App
• `-t <theme.json>` or `--theme <theme.json>`: Read a theme file for syntax highlighting. This theme file associates C# syntax classifications with colors. The color values can be full RGB, or ANSI color names (defined in your terminal's theme). The NO_COLOR standard is supported.
• `--trace`: Produce a trace file in the current directory that logs CSharpRepl internals. Useful for CSharpRepl bug reports.
• `-v` or `--version`: Show version number and exit.
• `-h` or `--help`: Show help and exit.
• `response-file.rsp`: A filepath of an .rsp file, containing any of the above command line options.
• `script-file.csx`: A filepath of a .csx file, containing lines of C# to evaluate before starting the REPL. Arguments to this script can be passed as `<additional-arguments>`, after a double hyphen (`--`), and will be available in a global `args` variable.

If you have `dotnet-suggest` enabled, all options can be tab-completed, including values provided to `--framework` and .NET namespaces provided to `--using`.

## Integrating with other software

C# REPL is a standalone software application, but it can be useful to integrate it with other developer tools:

### Windows Terminal

To add C# REPL as a menu entry in Windows Terminal, add the following profile to Windows Terminal's `settings.json` configuration file (under the JSON property `profiles.list`):

``````{
"name": "C# REPL",
"commandline": "csharprepl"
},
``````

To get the exact colors shown in the screenshots in this README, install the Windows Terminal Dracula theme.

### Visual Studio Code

To use the C# REPL with Visual Studio Code, simply run the `csharprepl` command in the Visual Studio Code terminal. To send commands to the REPL, use the built-in `Terminal: Run Selected Text In Active Terminal` command from the Command Palette (`workbench.action.terminal.runSelectedText`).

### Windows OS

To add the C# REPL to the Windows Start Menu for quick access, you can run the following PowerShell command, which will start C# REPL in Windows Terminal:

``````\$shell = New-Object -ComObject WScript.Shell
\$shortcut = \$shell.CreateShortcut("\$env:appdata\Microsoft\Windows\Start Menu\Programs\csharprepl.lnk")
\$shortcut.TargetPath = "wt.exe"
\$shortcut.Arguments = "-w 0 nt csharprepl.exe"
\$shortcut.Save()
``````

You may also wish to add a shorter alias for C# REPL, which can be done by creating a `.cmd` file somewhere on your path. For example, put the following contents in `C:\Users\username\.dotnet\tools\csr.cmd`:

``````wt -w 0 nt csharprepl
``````

This will allow you to launch C# REPL by running `csr` from anywhere that accepts Windows commands, like the Window Run dialog.

## Comparison with other REPLs

This project is far from being the first REPL for C#. Here are some other projects; if this project doesn't suit you, another one might!

Visual Studio's C# Interactive pane is full-featured (it has syntax highlighting and intellisense) and is part of Visual Studio. This deep integration with Visual Studio is both a benefit from a workflow perspective, and a drawback as it's not cross-platform. As far as I know, the C# Interactive pane does not support NuGet packages or navigating to documentation/source code. Subjectively, it does not follow typical command line keybindings, so can feel a bit foreign.

csi.exe ships with C# and is a command line REPL. It's great because it's a cross platform REPL that comes out of the box, but it doesn't support syntax highlighting or autocompletion.

dotnet script allows you to run C# scripts from the command line. It has a REPL built-in, but the predominant focus seems to be as a script runner. It's a great tool, though, and has a strong community following.

dotnet interactive is a tool from Microsoft that creates a Jupyter notebook for C#, runnable through Visual Studio Code. It also provides a general framework useful for running REPLs.

Author: waf
Source Code: https://github.com/waf/CSharpRepl

1594578060

## Only R Matters: mathematical epidemiology and infection rate

What does the history of mathematical epidemiology have to do with a modern pandemic? Everything really. Not much has changed except how we graphically represent it — namely with Graph — and its capacity to handle larger, world-reaching datasets. Hopefully, this piece will give you a primer to help you begin to understand the struggle we’re in today as we try to solve a mathematical problem like COVID-19.A brief history of mathematical epidemiologyMathematical modelling of epidemics dates back as far as the 18th-century Swiss mathematician Daniel Bernoulli, who, apparently not content with fundamental work in mechanics, statistics and economics, also created the first known mathematical model of the spread of an epidemic. His model gave a life expectancy in terms of the infection rate and the mortality rate, applied to the outbreaks of smallpox that were then prevalent throughout Europe.It was another disease which inspired what many consider the foundation of the science of epidemiology, namely cholera. Cholera outbreaks were a quite frequent phenomena in the 1800s in London, due to inadequate infrastructure, overcrowding, and a lack of an accurate medical model of germs and infection. It seems remarkable now, but there were two competing theories at the time: germ theory, and miasma theory. Miasma theory held that cholera, and other diseases, was due to decaying organic matter transmitted in the air — perhaps not as odd a theory as it might seem today, given the pollution then prevalent in central London, and the lack of a sewage system throughout many parts of the capital.John Snow was, in many ways, the first data scientist. He created a map of infections of an outbreak in Soho, around Broad Street, in 1854. He rejected the prevailing miasma theory, and concentrated instead on tracing the spread of cholera through the water system. He traced infections using what would now be called a Voronoi diagram. Eventually, he posited a particular water pump as the likely source of the cholera outbreak. His work provided empirical evidence for the germ theory, and established water supplies as the primary transmission agent for cholera. His work also led to major revisions in public health legislation concerning the provision of clean water and safe waste disposal. There is a pub in Soho, close to the water pump which was ground zero for the Broad Street infection, which bears his name.Many epidemiological models have been developed since this seminal work, including systems of differential equations, stochastic processes, and those based on graphs. We will look at how graph theory can be applied as a model of the spread of disease.What is a graph?Graph theory as an object of mathematical inquiry dates back to a problem known as the Seven Bridges of Königsberg. It is a problem which sounds quite simple to state, but its solution led Leonhard Euler to develop a wholly new mathematical theory in order to arrive at a definitive answer. While solving this problem, Euler invented graph theory; this is the sort of thing Euler did rather a lot of, making fundamental contributions to physics, astronomy, geography, logistics, topology, number theory, calculus, and an eye-watering number of other fields.Two rivers ran through the city of Königsberg, and seven bridges crossed them. The problem which preoccupied Euler, and led to the discovery of graph theory, was to create a walk which crossed the each of the bridges once, but only once. A map of Königsberg is given below, with the rivers given in blue and the bridges in red.

#data-science #epidemiology #health #graph-theory #mathematics #data analysis

1647064260

## dotnet script

Run C# scripts from the .NET CLI, define NuGet packages inline and edit/debug them in VS Code - all of that with full language services support from OmniSharp.

## Installing

### Prerequisites

The only thing we need to install is .NET Core 3.1 or .NET 5.0 SDK.

### .NET Core Global Tool

.NET Core 2.1 introduced the concept of global tools meaning that you can install `dotnet-script` using nothing but the .NET CLI.

``````dotnet tool install -g dotnet-script

You can invoke the tool using the following command: dotnet-script
Tool 'dotnet-script' (version '0.22.0') was successfully installed.
``````

The advantage of this approach is that you can use the same command for installation across all platforms. .NET Core SDK also supports viewing a list of installed tools and their uninstallation.

``````dotnet tool list -g

Package Id         Version      Commands
---------------------------------------------
dotnet-script      0.22.0       dotnet-script
``````
``````dotnet tool uninstall dotnet-script -g

Tool 'dotnet-script' (version '0.22.0') was successfully uninstalled.
``````

### Windows

``````choco install dotnet.script
``````

We also provide a PowerShell script for installation.

``````(new-object Net.WebClient).DownloadString("https://raw.githubusercontent.com/filipw/dotnet-script/master/install/install.ps1") | iex
``````

### Linux and Mac

``````curl -s https://raw.githubusercontent.com/filipw/dotnet-script/master/install/install.sh | bash
``````

If permission is denied we can try with `sudo`

``````curl -s https://raw.githubusercontent.com/filipw/dotnet-script/master/install/install.sh | sudo bash
``````

### Docker

A Dockerfile for running dotnet-script in a Linux container is available. Build:

``````cd build
docker build -t dotnet-script -f Dockerfile ..
``````

And run:

``````docker run -it dotnet-script --version
``````

### Github

You can manually download all the releases in `zip` format from the GitHub releases page.

## Usage

Our typical `helloworld.csx` might look like this:

``````Console.WriteLine("Hello world!");
``````

That is all it takes and we can execute the script. Args are accessible via the global Args array.

``````dotnet script helloworld.csx
``````

### Scaffolding

Simply create a folder somewhere on your system and issue the following command.

``````dotnet script init
``````

This will create `main.csx` along with the launch configuration needed to debug the script in VS Code.

``````.
├── .vscode
│   └── launch.json
├── main.csx
└── omnisharp.json
``````

We can also initialize a folder using a custom filename.

``````dotnet script init custom.csx
``````

Instead of `main.csx` which is the default, we now have a file named `custom.csx`.

``````.
├── .vscode
│   └── launch.json
├── custom.csx
└── omnisharp.json
``````

Note: Executing `dotnet script init` inside a folder that already contains one or more script files will not create the `main.csx` file.

### Running scripts

Scripts can be executed directly from the shell as if they were executables.

``````foo.csx arg1 arg2 arg3
``````

OSX/Linux

Just like all scripts, on OSX/Linux you need to have a `#!` and mark the file as executable via chmod +x foo.csx. If you use dotnet script init to create your csx it will automatically have the `#!` directive and be marked as executable.

The OSX/Linux shebang directive should be #!/usr/bin/env dotnet-script

``````#!/usr/bin/env dotnet-script
Console.WriteLine("Hello world");
``````

You can execute your script using dotnet script or dotnet-script, which allows you to pass arguments to control your script execution more.

``````foo.csx arg1 arg2 arg3
dotnet script foo.csx -- arg1 arg2 arg3
dotnet-script foo.csx -- arg1 arg2 arg3
``````

#### Passing arguments to scripts

All arguments after `--` are passed to the script in the following way:

``````dotnet script foo.csx -- arg1 arg2 arg3
``````

Then you can access the arguments in the script context using the global `Args` collection:

``````foreach (var arg in Args)
{
Console.WriteLine(arg);
}
``````

All arguments before `--` are processed by `dotnet script`. For example, the following command-line

``````dotnet script -d foo.csx -- -d
``````

will pass the `-d` before `--` to `dotnet script` and enable the debug mode whereas the `-d` after `--` is passed to script for its own interpretation of the argument.

### NuGet Packages

`dotnet script` has built-in support for referencing NuGet packages directly from within the script.

``````#r "nuget: AutoMapper, 6.1.0"
``````

Note: Omnisharp needs to be restarted after adding a new package reference

#### Package Sources

We can define package sources using a `NuGet.Config` file in the script root folder. In addition to being used during execution of the script, it will also be used by `OmniSharp` that provides language services for packages resolved from these package sources.

As an alternative to maintaining a local `NuGet.Config` file we can define these package sources globally either at the user level or at the computer level as described in Configuring NuGet Behaviour

It is also possible to specify packages sources when executing the script.

``````dotnet script foo.csx -s https://SomePackageSource
``````

Multiple packages sources can be specified like this:

``````dotnet script foo.csx -s https://SomePackageSource -s https://AnotherPackageSource
``````

### Creating DLLs or Exes from a CSX file

Dotnet-Script can create a standalone executable or DLL for your script.

The executable you can run directly independent of dotnet install, while the DLL can be run using the dotnet CLI like this:

``````dotnet script exec {path_to_dll} -- arg1 arg2
``````

### Caching

We provide two types of caching, the `dependency cache` and the `execution cache` which is explained in detail below. In order for any of these caches to be enabled, it is required that all NuGet package references are specified using an exact version number. The reason for this constraint is that we need to make sure that we don't execute a script with a stale dependency graph.

#### Dependency Cache

In order to resolve the dependencies for a script, a `dotnet restore` is executed under the hood to produce a `project.assets.json` file from which we can figure out all the dependencies we need to add to the compilation. This is an out-of-process operation and represents a significant overhead to the script execution. So this cache works by looking at all the dependencies specified in the script(s) either in the form of NuGet package references or assembly file references. If these dependencies matches the dependencies from the last script execution, we skip the restore and read the dependencies from the already generated `project.assets.json` file. If any of the dependencies has changed, we must restore again to obtain the new dependency graph.

#### Execution cache

In order to execute a script it needs to be compiled first and since that is a CPU and time consuming operation, we make sure that we only compile when the source code has changed. This works by creating a SHA256 hash from all the script files involved in the execution. This hash is written to a temporary location along with the DLL that represents the result of the script compilation. When a script is executed the hash is computed and compared with the hash from the previous compilation. If they match there is no need to recompile and we run from the already compiled DLL. If the hashes don't match, the cache is invalidated and we recompile.

You can override this automatic caching by passing --no-cache flag, which will bypass both caches and cause dependency resolution and script compilation to happen every time we execute the script.

#### Cache Location

The temporary location used for caches is a sub-directory named `dotnet-script` under (in order of priority):

1. The path specified for the value of the environment variable named `DOTNET_SCRIPT_CACHE_LOCATION`, if defined and value is not empty.
2. Linux distributions only: `\$XDG_CACHE_HOME` if defined otherwise `\$HOME/.cache`
3. macOS only: `~/Library/Caches`
4. The value returned by `Path.GetTempPath` for the platform.

### Debugging

The days of debugging scripts using `Console.WriteLine` are over. One major feature of `dotnet script` is the ability to debug scripts directly in VS Code. Just set a breakpoint anywhere in your script file(s) and hit F5(start debugging)

### Script Packages

Script packages are a way of organizing reusable scripts into NuGet packages that can be consumed by other scripts. This means that we now can leverage scripting infrastructure without the need for any kind of bootstrapping.

#### Creating a script package

A script package is just a regular NuGet package that contains script files inside the `content` or `contentFiles` folder.

The following example shows how the scripts are laid out inside the NuGet package according to the standard convention .

``````└── contentFiles
└── csx
└── netstandard2.0
└── main.csx
``````

This example contains just the `main.csx` file in the root folder, but packages may have multiple script files either in the root folder or in subfolders below the root folder.

When loading a script package we will look for an entry point script to be loaded. This entry point script is identified by one of the following.

• A script called `main.csx` in the root folder
• A single script file in the root folder

If the entry point script cannot be determined, we will simply load all the scripts files in the package.

The advantage with using an entry point script is that we can control loading other scripts from the package.

#### Consuming a script package

To consume a script package all we need to do specify the NuGet package in the `#load`directive.

The following example loads the simple-targets package that contains script files to be included in our script.

``````#load "nuget:simple-targets-csx, 6.0.0"

using static SimpleTargets;
var targets = new TargetDictionary();

targets.Add("default", () => Console.WriteLine("Hello, world!"));

Run(Args, targets);
``````

Note: Debugging also works for script packages so that we can easily step into the scripts that are brought in using the `#load` directive.

### Remote Scripts

Scripts don't actually have to exist locally on the machine. We can also execute scripts that are made available on an `http(s)` endpoint.

This means that we can create a Gist on Github and execute it just by providing the URL to the Gist.

This Gist contains a script that prints out "Hello World"

We can execute the script like this

``````dotnet script https://gist.githubusercontent.com/seesharper/5d6859509ea8364a1fdf66bbf5b7923d/raw/0a32bac2c3ea807f9379a38e251d93e39c8131cb/HelloWorld.csx
``````

That is a pretty long URL, so why don't make it a TinyURL like this:

``````dotnet script https://tinyurl.com/y8cda9zt
``````

### Script Location

A pretty common scenario is that we have logic that is relative to the script path. We don't want to require the user to be in a certain directory for these paths to resolve correctly so here is how to provide the script path and the script folder regardless of the current working directory.

``````public static string GetScriptPath([CallerFilePath] string path = null) => path;
public static string GetScriptFolder([CallerFilePath] string path = null) => Path.GetDirectoryName(path);
``````

Tip: Put these methods as top level methods in a separate script file and `#load` that file wherever access to the script path and/or folder is needed.

## REPL

This release contains a C# REPL (Read-Evaluate-Print-Loop). The REPL mode ("interactive mode") is started by executing `dotnet-script` without any arguments.

The interactive mode allows you to supply individual C# code blocks and have them executed as soon as you press Enter. The REPL is configured with the same default set of assembly references and using statements as regular CSX script execution.

### Basic usage

Once `dotnet-script` starts you will see a prompt for input. You can start typing C# code there.

``````~\$ dotnet script
> var x = 1;
> x+x
2
``````

If you submit an unterminated expression into the REPL (no `;` at the end), it will be evaluated and the result will be serialized using a formatter and printed in the output. This is a bit more interesting than just calling `ToString()` on the object, because it attempts to capture the actual structure of the object. For example:

``````~\$ dotnet script
> var x = new List<string>();
> x
List<string>(1) { "foo" }
> x
List<string>(2) { "foo", "bar" }
>
``````

### Inline Nuget packages

REPL also supports inline Nuget packages - meaning the Nuget packages can be installed into the REPL from within the REPL. This is done via our `#r` and `#load` from Nuget support and uses identical syntax.

``````~\$ dotnet script
> #r "nuget: Automapper, 6.1.1"
> using AutoMapper;
> typeof(MapperConfiguration)
[AutoMapper.MapperConfiguration]
> #load "nuget: simple-targets-csx, 6.0.0";
> using static SimpleTargets;
> typeof(TargetDictionary)
[Submission#0+SimpleTargets+TargetDictionary]
``````

### Multiline mode

Using Roslyn syntax parsing, we also support multiline REPL mode. This means that if you have an uncompleted code block and press Enter, we will automatically enter the multiline mode. The mode is indicated by the `*` character. This is particularly useful for declaring classes and other more complex constructs.

``````~\$ dotnet script
> class Foo {
* public string Bar {get; set;}
* }
> var foo = new Foo();
``````

### REPL commands

Aside from the regular C# script code, you can invoke the following commands (directives) from within the REPL:

### Seeding REPL with a script

You can execute a CSX script and, at the end of it, drop yourself into the context of the REPL. This way, the REPL becomes "seeded" with your code - all the classes, methods or variables are available in the REPL context. This is achieved by running a script with an `-i` flag.

For example, given the following CSX script:

``````var msg = "Hello World";
Console.WriteLine(msg);
``````

When you run this with the `-i` flag, `Hello World` is printed, REPL starts and `msg` variable is available in the REPL context.

``````~\$ dotnet script foo.csx -i
Hello World
>
``````

You can also seed the REPL from inside the REPL - at any point - by invoking a `#load` directive pointed at a specific file. For example:

``````~\$ dotnet script
Hello World
>
``````

## Piping

The following example shows how we can pipe data in and out of a script.

The `UpperCase.csx` script simply converts the standard input to upper case and writes it back out to standard output.

``````using (var streamReader = new StreamReader(Console.OpenStandardInput()))
{
}
``````

We can now simply pipe the output from one command into our script like this.

``````echo "This is some text" | dotnet script UpperCase.csx
THIS IS SOME TEXT
``````

### Debugging

The first thing we need to do add the following to the `launch.config` file that allows VS Code to debug a running process.

``````{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach",
"processId": "\${command:pickProcess}"
}
``````

To debug this script we need a way to attach the debugger in VS Code and the simplest thing we can do here is to wait for the debugger to attach by adding this method somewhere.

``````public static void WaitForDebugger()
{
Console.WriteLine("Attach Debugger (VS Code)");
while(!Debugger.IsAttached)
{
}
}
``````

To debug the script when executing it from the command line we can do something like

``````WaitForDebugger();
{
}
``````

Now when we run the script from the command line we will get

``````\$ echo "This is some text" | dotnet script UpperCase.csx
Attach Debugger (VS Code)
``````

This now gives us a chance to attach the debugger before stepping into the script and from VS Code, select the `.NET Core Attach` debugger and pick the process that represents the executing script.

Once that is done we should see our breakpoint being hit.

## Configuration(Debug/Release)

By default, scripts will be compiled using the `debug` configuration. This is to ensure that we can debug a script in VS Code as well as attaching a debugger for long running scripts.

There are however situations where we might need to execute a script that is compiled with the `release` configuration. For instance, running benchmarks using BenchmarkDotNet is not possible unless the script is compiled with the `release` configuration.

We can specify this when executing the script.

``````dotnet script foo.csx -c release
``````

## Nullable reference types

Starting from version 0.50.0, `dotnet-script` supports .Net Core 3.0 and all the C# 8 features. The way we deal with nullable references types in `dotnet-script` is that we turn every warning related to nullable reference types into compiler errors. This means every warning between `CS8600` and `CS8655` are treated as an error when compiling the script.

Nullable references types are turned off by default and the way we enable it is using the `#nullable enable` compiler directive. This means that existing scripts will continue to work, but we can now opt-in on this new feature.

``````#!/usr/bin/env dotnet-script

#nullable enable

string name = null;
``````

Trying to execute the script will result in the following error

``````main.csx(5,15): error CS8625: Cannot convert null literal to non-nullable reference type.
``````

We will also see this when working with scripts in VS Code under the problems panel.

Author: filipw
Source Code: https://github.com/filipw/dotnet-script

1594399440

## Getting Started with R Markdown — Guide and Cheatsheet

In this blog post, we’ll look at how to use R Markdown. By the end, you’ll have the skills you need to produce a document or presentation using R Mardown, from scratch!

We’ll show you how to convert the default R Markdown document into a useful reference guide of your own. We encourage you to follow along by building out your own R Markdown guide, but if you prefer to just read along, that works, too!

R Markdown is an open-source tool for producing reproducible reports in R. It enables you to keep all of your code, results, plots, and writing in one place. R Markdown is particularly useful when you are producing a document for an audience that is interested in the results from your analysis, but not your code.

R Markdown is powerful because it can be used for data analysis and data science, collaborating with others, and communicating results to decision makers. With R Markdown, you have the option to export your work to numerous formats including PDF, Microsoft Word, a slideshow, or an HTML document for use in a website.

Turn your data analysis into pretty documents with R Markdown.

We’ll use the RStudio integrated development environment (IDE) to produce our R Markdown reference guide. If you’d like to learn more about RStudio, check out our list of 23 awesome RStudio tips and tricks!

Here at Dataquest, we love using R Markdown for coding in R and authoring content. In fact, we wrote this blog post in R Markdown! Also, learners on the Dataquest platform use R Markdown for completing their R projects.

We included fully-reproducible code examples in this blog post. When you’ve mastered the content in this post, check out our other blog post on R Markdown tips, tricks, and shortcuts.

Okay, let’s get started with building our very own R Markdown reference document!

## 1. Install R Markdown

R Markdown is a free, open source tool that is installed like any other R package. Use the following command to install R Markdown:

``````install.packages("rmarkdown")
``````

Now that R Markdown is installed, open a new R Markdown file in RStudio by navigating to `File > New File > R Markdown…`. R Markdown files have the file extension “.Rmd”.

## 2. Default Output Format

When you open a new R Markdown file in RStudio, a pop-up window appears that prompts you to select output format to use for the document.

The default output format is HTML. With HTML, you can easily view it in a web browser.

We recommend selecting the default HTML setting for now — it can save you time! Why? Because compiling an HTML document is generally faster than generating a PDF or other format. When you near a finished product, you change the output to the format of your choosing and then make the final touches.

One final thing to note is that the title you give your document in the pop-up above is not the file name! Navigate to `File > Save As..` to name, and save, the document.

#data science tutorials #beginner #r #r markdown #r tutorial #r tutorials #rstats #rstudio #tutorial #tutorials

1624422360

## R vs Python: What Should Beginners Learn?

### Let go of any doubts or confusion, make the right choice and then focus and thrive as a data scientist.

I currently lead a research group with data scientists who use both R and Python. I have been in this field for over 14 years. I have witnessed the growth of both languages over the years and there is now a thriving community behind both.

I did not have a straightforward journey and learned many things the hard way. However, you can avoid making the mistakes I made and lead a more focussed, more rewarding journey and reach your goals quicker than others.

Before I dive in, let’s get something out of the way. R and Python are just tools to do the same thing. Data Science. Neither of the tools is inherently better than the other. Both the tools have been evolving over years (and will likely continue to do so).

Therefore, the short answer on whether you should learn Python or R is: it depends.

The longer answer, if you can spare a few minutes, will help you focus on what really matters and avoid the most common mistakes most enthusiastic beginners aspiring to become expert data scientists make.

#r-programming #python #perspective #r vs python: what should beginners learn? #r vs python #r