How to tell if expense is R&D vs SG&A

I’m having trouble figuring out what the litmus test is for an expense being R&D. My understanding of R&D expense is that it’s anything tied to the discovery of a new technology or building a new product the company hasn’t seen before (?). What if it’s new to the company but not new to society? Or what if it’s new to society but it’s closely linked to something that already exists.

Example 1: A small team builds an online CFA prep platform from scratch. It’s in the same vein as what you’d see on Kaplan Schweser. A new product to the company, but nothing cutting edge or technologically advanced. Is the software developer’s payroll considered R&D?

Example 2: A startup develops a data compression algorithm for compressing x-ray images, hoping to become the Dropbox of hospitals. Data compression isn’t a new idea obviously. Is the software developer’s payroll considered R&D? If yes, does the answer change after the product has proven itself in the market and generated $ millions in revenue?

Example 3: A battery startup is trying to synthesize a battery material that extends battery life by 1%. The exact chemical composition or the manufacturing process might be unique, but the idea of composing new materials is not. Is the chemical engineer’s payroll considered R&D? If yes, does the answer change after the product has proven itself in the market and generated $ millions in revenue?