Skip to content

Instantly share code, notes, and snippets.

@b-rodrigues
Created November 14, 2020 21:53
Show Gist options
  • Save b-rodrigues/0e28cddbaa64ec1bbcbb0cd3cbb26ad9 to your computer and use it in GitHub Desktop.
Save b-rodrigues/0e28cddbaa64ec1bbcbb0cd3cbb26ad9 to your computer and use it in GitHub Desktop.
---
title: "Title goes here"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
runtime: shiny
---
```{r global, include=FALSE}
library(tidyverse)
library(shiny)
library(flexdashboard)
benford <- tibble::tribble(~first_digit, ~percent,
1, 0.301,
2, 0.176,
3, 0.125,
4, 0.097,
5, 0.079,
6, 0.067,
7, 0.058,
8, 0.051,
9, 0.046
) %>%
mutate(distribution = "benford")
```
Column {.sidebar}
-----------------------------------------------------------------------
```{r}
sliderInput("n_samples", label = "Sample draws",
min = 1, max = 1000, value = 100, step = 1)
sliderInput("param_sd", label = "Standard deviation",
min = 1, max = 100, value = 1, step = 1)
```
Column
-----------------------------------------------------------------------
```{r}
renderPlot({
observations <- round(rnorm(as.numeric(input$n_samples), 0, as.numeric(input$param_sd))) %>%
as_tibble() %>%
rename(obs = value) %>%
filter(obs > 0) %>%
mutate(obs = as.character(obs),
first_digit = str_extract(obs, "^\\d")) #%>%
#mutate(across(.cols = everything(), as.numeric))
observations <- janitor::tabyl(observations, first_digit) %>%
select(-n) %>%
mutate(distribution = "empirical") %>%
rbind(benford)
ggplot(observations) +
geom_col(aes(y = percent, x = first_digit, fill = distribution), position = "dodge") +
labs(title = paste0("Number of samples: ", input$n_samples)) +
theme_minimal() +
theme(legend.position = "bottom")
})
# rmarkdown::run("benford.rmd")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment