Skip to content

Instantly share code, notes, and snippets.

View sbalci's full-sized avatar
🔬
🔬👀📑🗃📊🏨🗄📇📖⚗📝🎶📈📉📃🖍🔬🔬🏋🏻‍♂🚴🏻‍♂🚙👨‍💻🤷‍♂📸📺🎛🔭🔬💊🔐🍫🌸

Serdar Balcı sbalci

🔬
🔬👀📑🗃📊🏨🗄📇📖⚗📝🎶📈📉📃🖍🔬🔬🏋🏻‍♂🚴🏻‍♂🚙👨‍💻🤷‍♂📸📺🎛🔭🔬💊🔐🍫🌸
View GitHub Profile
# What's the most natural way to express this code in base R?
library(dplyr, warn.conflicts = FALSE)
mtcars %>%
group_by(cyl) %>%
summarise(mean = mean(disp), n = n())
#> # A tibble: 3 x 3
#> cyl mean n
#> <dbl> <dbl> <int>
#> 1 4 105. 11
#> 2 6 183. 7
@mine-cetinkaya-rundel
mine-cetinkaya-rundel / theming_rprofile.R
Last active June 19, 2022 19:11
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")
}
}
@DarwinAwardWinner
DarwinAwardWinner / .Rprofile
Created January 11, 2020 17:32
The environment setup for my R profile
local({
## Put everything in a separate environment and attach that
## environment. Re-use the existing one from the search path it
## it's available, so that re-executing this file doesn't add a
## new environment to the search path every time.
custom_env <- tryCatch(as.environment("rct_custom_env"),
error=function(...) {
attach(NULL, name="rct_custom_env")
as.environment("rct_custom_env")

How to use Gephi to visualize from Wikidata

I'm a long-time fan of the graph visualization tool Gephi and since Wikimania 2019 I got involved with Wikidata. I was aware of the Gephi plugin "Semantic Web Importer", but when I check it out, I only find old tutorials connecting to DBpedia, not Wikidata:

# Find Packages Used in a Project Using Regular Expressions RegEx
list_of_Rmd_R <- c(
list.files(path = here::here(),
pattern = "\\.Rmd|.R$", # find files ending with .Rmd and .R
full.names = TRUE
),
list.files(path = here::here("childRmd"),
pattern = "\\.Rmd|.R$",
@sbalci
sbalci / 00.README.md
Created November 12, 2019 16:18 — forked from rmoff/00.README.md
RMarkdown - repeating child block with changing variable value

When you use knit_expand it appears that the inclusion of the Rmd is done on the first pass, and then the complete document evaluated. This means that a Rmd block referenced in loop with knit_expand will only evaluate changing variables at their last value.

This can be worked around by passing the literal value of the variable at the time of the knit_expand with {{var}} syntax.

This is documented in the knitr_expand docs, but less clear (to an R noob like me) for embedded documents rather than strings.

@CGMossa
CGMossa / .Rprofile
Last active February 11, 2020 17:34
#TODO: Find a way to restart R without using this particular .Rprofile.
#
# CLEAN_SESSION <- TRUE
CLEAN_SESSION <- FALSE
if (interactive() && !CLEAN_SESSION) {
# Configure session -------------------------------------------------------
message("Interactive mode configuration")
@seasmith
seasmith / keyword-vector-subsetting.R
Created July 18, 2019 16:44
Intuitive vector subsetting using keywords
`[.hmmm` <- function (x, i, ...) {
isub <- substitute(i)
is_seq <- identical(as.list(isub)[[1L]], as.name(":"))
if (is_seq) {
end_seq <- as.list(isub)[[3L]]
is_end <- identical(end_seq, as.name("end"))
# =======================================
# = Enhancements to data tidying =
# = Hadley Wickham =
# = https://rstd.io/tidyhancements-2019 =
# =======================================
# What is tidy data? ----------------------------------------------------------
# 1. Each column is a variable.
# 2. Each row is an observation.
# 3. Each cell is a value.
@summerofgeorge
summerofgeorge / rename-jpg-files.r
Last active October 29, 2019 10:08
Rename all jpg files in a folder
# List the jpg files in the folder
old_files <- list.files("C:/ImageTest", pattern = "*.JPG", full.names = TRUE)
old_files
# Create vector of new files
new_files <- paste0("C:/NewFiles/file_",1:length(old_files),".JPG")
new_files