Upload dataframe to Google Spreadsheets in R

library(googlesheets)

# dataframe
df <- data.frame(x = c(1,2), y=c(2,3))

# access googlesheets
all_my_sheets_in_drive <- gs_ls()

# access spreadheet
gs <- gs_title("my spreadsheet")

# create worksheet
gs_ws <- gs_ws_new(gs, ws_title = "new worksheet")
 
# upload dataframe to worksheet "new worksheet"
gs_edit_cells(gs, ws="new worksheet", input = df, trim = TRUE)

Search/Filter with grepl with multiple patterns

Goal: Filter a dataframe, that contains parts of strings, that are in another dataframe.

# dataframe that contains strings
d1 <- c("halloauch", "hieronymus", "grüßdich", "hello", "hi", "hallo")
d1 <- data.frame(d1)
# dataframe that contains patterns
d2 <- c("hi", "hallo")
d2 <- data.frame(d2)

grepl can’t search through a dataframe. It needs a vector with the or-operator

# convert patterns to vector
d2 <- as.vector(as.matrix(d2))
# combine patterns to a single element
d2 <- paste(d2, collapse="|")

check, if dataframe contains patterns and filter these elements:

library(dplyr)
d %>% filter(grepl(d2, d1$name))

Use custom fonts in R, e.g. with ggplot2

The package showtext can import .otf font files, that can be used with ggplot2 theme options.

library(showtext)
font.add("NameOfFont", "NameOfFont.otf")
showtext.auto() 

library(ggplot2)
ggplot(d, aes(x = x, y = y)) +
geom_point() +
theme(text = element_text(family="NameOfFont")) 

Alternativly, there is extrafont. The downside: It can only use .ttf files.

Order of variables ggplot2 with reorder()

The order of appearance is controlled in the ggplot aesthetics via redorder()

p <- ggplot(
data = d, aes(x = variable, y = reorder(film, index), fill = value, colour=”white”))

# sort my films depending on the index variable
ggplot(d, aes(x = variable, y = reorder(film, index))

# descending
ggplot(d, aes(x = variable, y = reorder(film,desc(index))