Skip to content

Commit

Permalink
added two more R examples
Browse files Browse the repository at this point in the history
  • Loading branch information
brandmaier committed Mar 12, 2024
1 parent 797dd2b commit 865aba6
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 1 deletion.
51 changes: 51 additions & 0 deletions R/demo8.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
library(shiny)
library(tidyverse)
library(ggraph)
library(tidygraph)



# Define UI for application
ui <- fluidPage(

# Application title
titlePanel("Social Network"),

# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("radius",
"Node radius",
min = 1,
max = 50,
value = 30),

selectInput("style","Layout",c("dendrogram","stress","circlepack"))
),

# Show outputs
mainPanel(
plotOutput("graphPlot",width = "600px"),
textOutput("description")
)
)
)


server <- function(input, output) {

# load data
graph <- as_tbl_graph(highschool)

output$graphPlot <- renderPlot({

ggraph(graph(), layout = "dendrogram") +
geom_edge_link() +
geom_node_circle(aes(r=input$radius/100.0),fill="black")+
ggraph::theme_graph()

})
}

# Run the application
shinyApp(ui = ui, server = server)
80 changes: 80 additions & 0 deletions R/normal.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
library(shiny)
library(dplyr)
library(ggplot2)

# Define UI for application that draws a histogram
ui <- fluidPage(

# Application title
titlePanel("Is this normal?"),

fluidRow(
column(12,
numericInput(inputId="n",label = "Sample size",value=100,min=5,max=100),
plotOutput(outputId = "result"),
shiny::actionButton(inputId = "yes", label="Yes"),
shiny::actionButton(inputId = "no", label="No"),
htmlOutput(outputId = "msg")
)
)


)


server <- function(input, output) {


values <- reactiveValues(normal = 1, status = "")

observe({
input$yes
cat("YES\n")
check(TRUE)
})

observe({
input$no
cat("NO\n")
check(FALSE)
})

check <- function(user_choice) {
if (user_choice == values$normal) {
values$status <- "<p style='color:green;'>CORRECT!</p>"
} else {
values$status <- "<p style='color:red;'>WRONG!</p>"
}

values$normal <- sample(c(TRUE,FALSE),1)

}

output$result <- renderPlot({


if ( values$normal ) {
dat<-rnorm(input$n)
} else {
type = sample(c(1,2,3),1)
if (type==1) {
dat<-rchisq(input$n,df = 3)-1
} else if (type==2) {
dat<-runif(input$n,-2,2)
} else if (type==3) {
dat<-rlogis(input$n)
}
}
dat <- data.frame(dat)
names(dat) <- c("x")
dat %>% ggplot(aes(x=x))+geom_density(fill="grey")+geom_point(y=0,size=4)
})

output$msg <- renderText({
return(values$status)
})

}

# Run the application
shinyApp(ui = ui, server = server)
2 changes: 1 addition & 1 deletion challenges/02-graph/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ui <- fluidPage(

# Show a plot of the generated distribution
mainPanel(
plotOutput("graphPlot"),
plotOutput("graphPlot",width = "600px"),
textOutput("description")
)
)
Expand Down

0 comments on commit 865aba6

Please sign in to comment.