Updating R output width after resizing the terminal and at startup

By default, R wraps or truncates its output at 80 columns, making it difficult to read data frames with many columns or with long text values. With a modern computer it’s quite likely your terminal emulator or console is significantly wider than 80 columns.

One apparent solution is:

options(setWidthOnResize = TRUE)

Unfortunately, this only takes effect after resizing the terminal, and not at startup. The R documentation suggests setting the initial width from the COLUMNS environment variable:

cols <- Sys.getenv("COLUMNS")
if(nzchar(cols)) options(width = as.integer(cols))

However, at the time ~/.Rprofile is evaluated, COLUMNS is surprisingly empty for me! My guess is that GNU readline initializes it sometime after startup. To work around this, on POSIX platforms we can use the stty command to query the terminal dimensions:

% /bin/stty size
68 128

So the eventual snippet in my ~/.Rprofile is:

# Set output width from terminal width.
if (interactive() && nzchar(Sys.getenv("TERM"))) local({
  # Needs modern readline and R to work.
  options(setWidthOnResize = TRUE)
  columns <- Sys.getenv("COLUMNS")
  if (nzchar(columns)) {
    options(width = as.integer(columns))
  } else {
    # weirdly, COLUMNS is usually empty at startup, so run a child process.
      size <- system2("/bin/stty", "size", stdout = TRUE)
      width <- as.integer(strsplit(size, " ")[[1]])[2]
      options(width = width)
    }, error = function(e) e )

Resetting USB / Ethernet Adapters from Linux

I bought a cheap UGREEN USB 3.0 Gigabit Ethernet Adapter, which is based on the ASIX AX88179 chipset. I found that it would periodically (usually while under load) stop transmitting/receiving packets, and shortly after that, its LEDs would turn off. Apparently AX88179 adapters are often flakey like this.

Cycling the Linux network interface running on top of the adapter using ip link set dev eth0 down and ip link set dev eth0 up didn’t get it working again. This is unfortunate since I run PPP over this ethernet link to connect to the internet. It seemed like the only fix was to reboot the machine.

However, I did find a solution to soft-reset the adapter by using the USBDEVFS_RESET ioctl system call.

Continue reading “Resetting USB / Ethernet Adapters from Linux”

Circuit for temperature-controlled dual-fan Raspberry Pi case

Several Raspberry Pi retailers are selling a CNC’d aluminium heatsink case for the Raspberry Pi, also known as the “Armour” case. While I actually wanted the passively cooled case (i.e. just a big heatsink), somehow I wound up with the dual-fan case instead.

The fans terminate in a combined dual-0.1″ plug, which they recommend connecting across the 5V and ground pins at positions 4 and 6 on the Pi’s GPIO header. This works fine, but the fans are on all the time, which I didn’t want (they’re not perfectly silent, and they’re also drawing power despite the Pi being relatively cool).

circuit with flyback diode added
Aluminium dual-fan case with the fans in the manufacturer’s recommended “always-on” configuration

It would be better to switch the fans on when the Pi gets above a particular temperature – and this post explains how. Thanks to Jim for supplying hardware, knowledge and soldering skills to make a solution possible.


From the software side, this is easily achieved. Some other sites recommend writing a userspace daemon in Python, but we can simply add a device-tree overlay to have the kernel do the monitoring and switching:

# in /boot/config.txt

By default, this sets GPIO12 high when the Pi hits 55C, and turns it off once the Pi gets below 45C. Theoretically, this also supports hardware PWM to control the fan speed, although the speed isn’t configurable through the overlay.


So, how do we wire things up so that when GPIO12 goes high, we can supply the fans with the power they need? GPIO12 only supplies 3.3V and at most 16mA, so connecting the fans directly will damage the Pi. The hard-to-find gpio-fan overlay source code suggests using a 2N7002 N-MOSFET like so:

circuit with 2n7002 transistor's Gate connected to GPIO12, and fan between 5V and the 2n7002's Drain; the transistor's Source is connected to ground

However, this doesn’t work for the dual-fans. And while it might work for single fans, there are still some additional improvements that can help protect the Pi.

Continue reading “Circuit for temperature-controlled dual-fan Raspberry Pi case”