Skip to content

Instantly share code, notes, and snippets.

@mine-cetinkaya-rundel
Last active May 15, 2024 15:41
Show Gist options
  • Select an option

  • Save mine-cetinkaya-rundel/465335e8ff4d0e8beb38b296a6ac712d to your computer and use it in GitHub Desktop.

Select an option

Save mine-cetinkaya-rundel/465335e8ff4d0e8beb38b296a6ac712d to your computer and use it in GitHub Desktop.
Add to your .Rprofile to change RStudio theme based on hour of the day (dark mode 8pm-6am, light mode 7am-7pm)
# theme options based on hour of day -------------------------------------------
# based on https://github.com/rstudio/rstudio/issues/1579
setHook("rstudio.sessionInit", function(chooseTheme) {
if (chooseTheme) {
if (as.numeric(format(as.POSIXct(Sys.time(), format = "%H:%M:%S"), "%H")) %in% c(0:5, 21:23)) {
if (rstudioapi::getThemeInfo()$editor != "Solarized Dark") rstudioapi::applyTheme("Solarized Dark")
} else {
if (rstudioapi::getThemeInfo()$editor != "Solarized Light") rstudioapi::applyTheme("Solarized Light")
}
}
},
action = "replace"
)
@cakcora
Copy link
Copy Markdown

cakcora commented Feb 7, 2020

use guide from Twitter

library(usethis)
edit_r_profile()

Copy and paste the code there and close the document. Then restart R for the new Rprofile to go into effect, but you probably want to restart RStudio to actually see the effect of theme change.

@sbalci
Copy link
Copy Markdown

sbalci commented May 2, 2020

Hi, thank you for the code.

Between 0-6am the formula format(as.POSIXct(Sys.time(), format = "%H:%M:%S"), "%H" gives result as 03 04 05 etc. And not recognized in c(0:6).

I am not good in date and numbers, so I would like to ask your suggestion. How can I correct it?

@mine-cetinkaya-rundel
Copy link
Copy Markdown
Author

@sbalci Wrapping it in as.numeric() converts it to numeric, without the leading 0:

as.numeric("03")
#> [1] 3

@sbalci
Copy link
Copy Markdown

sbalci commented May 2, 2020

Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment