RStudio uses the "cmd-shift-m" keyboard shortcut to insert the tidyverse pipe operator (%>%
). If you are using VSCode to write R code, it's nice to have this there too. Here's what you need to add to keybindings.json
to get this into place:
{
"key": "shift+cmd+m",
"command": "-workbench.actions.view.problems"
},
{
"key": "shift+cmd+m",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"snippet": "%>% "
}
},
{
"key": "shift+cmd+m",
"command": "workbench.action.terminal.sendSequence",
"when": "terminalFocus",
"args": {
"text": "%>% "
}
}
]]>
I often find myself wanting to grab the contents of a tweet in Markdown format to post on my blog, so I’ve created an iOS shortcut that takes the URL to a tweet, grabs the contents via the public Twitter API, expands t.co URLs, and copies the content to the clipboard.
Here’s an example tweet:
It’s surprisingly hard to find a tweet with multiple URLs in it for API response testing purposes.
— Max Masnick (@masnick) December 25, 2019
So here are my two favorite non-Google search engines:
1. https://t.co/jFqkR0eo8x
2. https://t.co/UZD9mmtzEH
👍
And here’s what the output of the Shortcut looks like:
]]>I have a data frame in R where dates are in two different formats: 2019-01-01
and 01/01/2019
. These are stored as characters by default because R can't convert them to dates.
There's a pretty easy way to clean this up, however:
]]>It's often convenient to store a password in the macOS system keychain and retrieve it inside a shell script.
Here's the bash command for setting a password:
security add-generic-password -s "Keychain item name here" -a "username here" -p
Then enter your password when prompted.
After you run this once, you can retrieve the password in a script like this:
PASSWORD=`security find-generic-password -s "Keychain item name here" -a "username here" -w`
You can then use $PASSWORD
whenever you need the password in your script.
window.location.href = 'https://threadreaderapp.com/search?q='+encodeURI(window.location.href)
You can use this page to convert the above JavaScript to a bookmarklet.
]]>Once the menu is active, you can use the arrow keys to navigate up and down, and return to select a given speaker. This allows you to tell iTunes to control a Homepod or other AirPlay 2 speaker from the keyboard.
Unfortunately there is no way I can find to automatically select a specific speaker – you have to manually do it with the arrow keys.
]]>R Markdown is very nice way to write literate R code. Recently, reticulate
became available, which bridges R and Python in R Markdown documents.
If you use Pipenv for Python dependency management, here's how you get reticulate
to use the proper Python executable:
library('reticulate')
venv <- system("pipenv --venv", inter = TRUE)
use_virtualenv(venv, required = TRUE)
py_config()
The py_config()
command should print out the path to the Python executable in the virtualenv
created by Pipenv.
A very handy tip from Victor Afanasev:
]]>Did you know that you can use Bundler inside single Ruby script (without Gemfile) and automatically install required dependencies for it?
# example.rb require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'rest-client' gem 'nokogiri' end ### body = RestClient.get("https://www.reddit.com/r/ruby/").body puts "Posts from r/ruby front page:" Nokogiri::HTML(body).xpath("//div[contains(@class, 'scrollerItem')]//h2").each do |h2| puts h2.text.strip end
By default, the Mac will display a giant padlock image when "Curtain Mode" is engaged with the Screens VNC app or macOS's built-in screen sharing capability:
I wanted to remove the padlock to avoid burn-in during long screen sharing sessions. I found this Apple Discussions post with the following instructions for overriding the default image:
- Create a picture using a graphics program, such as AppleWorks.
- Save the picture in PICT, TIFF, GIF, JPEG, or any other QuickTime-compatible static image format.
- QuickTime-compatible movies or QuickTime VR objects cannot be used.
- Name the picture "Lock Screen Picture".
- Copy the "Lock Screen Picture" file to /Library/Preferences/ on the client computer.
I was very dubious that it would work, but incredibly it does! If you save a plain black .tiff like this one as /Library/Preferences/Lock Screen Picture.tiff
, the padlock image will disappear.
There is a relatively easy fix for a message from git lfs like:
Encountered n file(s) that should have been pointers, but weren't
If the files in question are already on a remote
and you don't want to rewrite history, run:
git lfs migrate import --no-rewrite path/to/file.ext
This will create a new commit moving path/to/file.ext
to LFS.
If you do need to rewrite history, see the documentation for migrating data to LFS.
To check whether this worked, run:
git lfs ls-files | grep path/to/file.ext
You should see an entry for path/to/file.ext
in the resulting output.
I've found that Pocket's bookmarklet is not reliable in Safari and Safari Technology Preview due to browser security measures against 3rd party cookies. This results in their official bookmarklet asking me to "Please login" every time I try to use it.
Fortunately, there is an easy work-around with a very simple bookmarklet:
window.location.href = 'https://getpocket.com/edit?url='+encodeURI(window.location.href)
You can use the Bookmarklet Maker to turn this code into an actual bookmarklet.
]]>This happened to me because I restored my boot disk from a full disk clone (thanks to a bad macOS update installation). I didn't want to restore to an unencrypted disk, so when I booted from my backup disk to do the restore, I used Disk Utility to encrypt my internal boot disk with a password.
After encrypting and then cloning the backup to my boot drive, I was greeted with a screen like this on my first boot from my boot drive:
Fine, no problem. I entered my disk password, and the normal login screen showed up. I entered my user password and everything was working...except I could not re-enable unlocking my disk with my user's password:
After searching around for how to fix failure of "Enable users" when "Some users are unable to unlock the disk", I discovered that there are a set of command line utilities for dealing with FileVault and I was able to fix the problem by decrypting the disk and then re-enabling FileVault.
Here's the command to decrypt: diskutil apfs decryptVolume /dev/disk1s1
To get "/dev/disk1s1", I simply looked at the info for my hard drive ("HD") in Disk Utility. (Normally this would be named "Macintosh HD", but I always rename mine.)
Then I simply re-enabled FileVault through System Preferences.
Warning: Messing around with disk encryption (or messing around with anything involving your boot disk) is risky. Always have a known good backup before you try anything like this, and proceed at your own risk – I am not responsible if you break your disk trying to follow these instructions.
]]>I've found a few decent ways to extract all the tweets from a "tweetstorm" for easier reading or quoting:
When using PyCharm for debugging data science code, I often want to view a Pandas DataFrame from PyCharm's interactive debugger. It took me a bit to figure out how to do this from the keyboard — here's what I do:
cmd-;
in my configuration).enter
, which gives me this:
tab
from the "Expression" text field to the "Results" area and then hit my "View as Array" shortcut (cmd-enter
). This opens the DataFrame in SciView.Here's the "View as Array" keyboard shortcut configuration:
I've found "floating mode" to work best with SciView. cmd-w
will close SciView in floating mode, but for some reason when I do this, the "Evaluate" window will lose focus and I can't figure out how to focus it again without mousing. Keyboard Meastro to the rescue here:
Source: var x = /twitter.com\/.*\/status\/([0-9]+)/.exec(window.location.href); if(x == null) {alert("Doesn't look like this is a tweet.")} else {window.location='twitterrific:///tweet?id='+x[1]};
Converted to bookmarklet: javascript:(function()%7Bvar%20x%20%3D%20%2Ftwitter.com%5C%2F.*%5C%2Fstatus%5C%2F(%5B0-9%5D%2B)%2F.exec(window.location.href)%3B%20if(x%20%3D%3D%20null)%20%7Balert(%22Doesn't%20look%20like%20this%20is%20a%20tweet.%22)%7D%20else%20%7Bwindow.location%3D'twitterrific%3A%2F%2F%2Ftweet%3Fid%3D'%2Bx%5B1%5D%7D%7D)()
This is directly from macscripter.net, but they have some encoding issues that I've fixed:
]]>I often want to view the contents of .pickle files on my computer. I like doing this in the IPython REPL, so I would manually open a new Terminal window, switch to my data science virtualenv, import pickle, and then load the .pickle file in question into a variable. Sounds like a prime target for automation, eh?
Here's a Keyboard Maestro macro that will take the selected .pickle file in Finder and open it up in IPython (inside iTerm2), assigning it to the variable x
.
Here's what it looks like (the macro is triggered through LaunchBar with this custom action):
After the macro runs, I type in x
to show the object in the Pickle file (in this case, it's just {"hello": "world"}
.
Here's what the macro looks like in Keyboard Maestro:
Here's the code:
Note that this requires your Python virtualenv to be set up like this and you have to have a "data" virtualenv. If either of these things aren't true, you'll need to modify the shell commands in the AppleScript accordingly.
]]>Put one of the following in a Jupyter notebook cell and run. If you make changes, clear cell contents, save, and refresh the browser.
All cells:
import IPython.core.display as di di.display_html(""" $('<style>.cell { margin-bottom: 100px !important;}</style>').appendTo('head'); """, raw=True)
To add padding just after code cells:
import IPython.core.display as di di.display_html(""" $('<style>.code_cell { margin-bottom: 100px !important;}</style>').appendTo('head'); """, raw=True)
To add padding just between code cells:
import IPython.core.display as di di.display_html(""" $('<style>.code_cell+.code_cell { margin-top: 100px !important;}</style>').appendTo('head'); """, raw=True)]]>
SHOW FULL PROCESSLIST;
Sometimes the built-in camera on my Mac will stop working (no green light; no video). Restarting will fix it, but this appears to also work: sudo killall VDCAssistant
From How-To Geek:
Run sudo nvram SystemAudioVolume=%80
in the Terminal to disable the startup chime. Works like a charm on my late 2014 iMac.
To re-enable, run sudo nvram -d SystemAudioVolume
(I didn't test this part).
On multiple Macs, I have had the left/right balance of my headphones randomly drift. Apple says this can be caused by changing the volume while the CPU is under load, which doesn't seem to be what is happening to me. In any case, it's annoying.
Here's an AppleScript that will automatically reset the sound balance to the middle for the current output device:
There's also a $5 app called Balance Lock that in theory will solve this problem – I haven't tested it.
]]>Note: This could be done in a loop. I don't do this because I often have results set-specific code that I want to run (e.g. renaming variables).
]]>The following ImageMagick command will take an image with gray text (and a white background) and make the text black.
magick convert -level 80%,100%,1 in.png out.png
I've written some helper methods for quickly generating 2x2 tables (or contingency tables, cross tabulations, crosstabs) in Jupyter/IPython notebooks. This achieves the same thing, more or less, as Stata's tabulate twoway command.
The notebook below has the helper methods and examples of their output. I've added these to my ipython-setup file because I use them so frequently.
]]>