[emaillocker]Below are the solutions to these exercises on Shiny modules.
# load package library(shiny) #################### # # # Exercise 1 # # # #################### q1_module_UI <- function(id) { ns <- NS(id) selectInput( inputId = ns("letter"), label = "Select a letter", choices = LETTERS ) } #################### # # # Exercise 2 # # # #################### q1_module <- function(input, output, session) { observe(print(input$letter)) } #################### # # # Exercise 3 # # # #################### ui <- fluidPage(q1_module_UI(id = "q1")) #################### # # # Exercise 4 # # # #################### server <- function(input, output, session) { callModule(module = q1_module, id = "q1") } shinyApp(ui = ui, server = server) #################### # # # Exercise 5 # # # #################### q5_module_UI <- function(id) { ns <- NS(id) tagList( selectInput( inputId = ns("letter"), label = "Select a letter", choices = LETTERS ), textOutput(outputId = ns("selected_letter")) ) } q5_module <- function(input, output, session) { output$selected_letter <- renderText(paste("You selected", input$letter)) } shinyApp( ui = fluidPage(q5_module_UI(id = "q5")), server = function(input, output, session) { callModule(module = q5_module, id = "q5") } ) #################### # # # Exercise 6 # # # #################### shinyApp( ui = fluidPage( q5_module_UI(id = "q5_a"), br(), q5_module_UI(id = "q5_b") ), server = function(input, output, session) { callModule(module = q5_module, id = "q5_a") callModule(module = q5_module, id = "q5_b") } ) #################### # # # Exercise 7 # # # #################### q7_module_UI <- function(id, label = "Select a letter") { ns <- NS(id) tagList( selectInput( inputId = ns("letter"), label = label, choices = LETTERS ), textOutput(outputId = ns("selected_letter")) ) } q7_module <- function(input, output, session) { output$selected_letter <- renderText(paste("You selected", input$letter)) } shinyApp( ui = fluidPage(q7_module_UI(id = "q7", label = "Your letter goes here")), server = function(input, output, session) { callModule(module = q7_module, id = "q7") } ) #################### # # # Exercise 8 # # # #################### q8_module_UI <- function(id, subjects, button_label = "Submit!") { ns <- NS(id) tagList( textInput(inputId = ns("name"), label = "Name"), selectInput(inputId = ns("subject"), label = "Subject", choices = subjects), textAreaInput(inputId = ns("message"), label = "Message"), actionButton(inputId = ns("submit"), label = button_label) ) } q8_module <- function(input, output, session) { observeEvent(input$submit, { content <- paste(input$name, input$subject, input$message, "\n", sep = "\n") write(x = content, file = "content.txt", append = T) }) } shinyApp( ui = fluidPage( q8_module_UI( id = "q8", subjects = c("Question", "Suggestion"), button_label = "Send" ) ), server = function(input, output, session) { callModule(module = q8_module, id = "q8") } ) #################### # # # Exercise 9 # # # #################### q9_module_UI <- function(id) { ns <- NS(id) tagList( numericInput( inputId = ns("element_num"), label = "Number of elements", value = 3 ), uiOutput(outputId = ns("elements")) ) } q9_module <- function(input, output, session) { output$elements <- renderUI({ tagList( lapply(1:input$element_num, function(i) { textInput(inputId = paste0("element", i), label = paste0("Element", i)) }) ) }) } shinyApp( ui = fluidPage(q9_module_UI(id = "q9")), server = function(input, output, session) { callModule(module = q9_module, id = "q9") } ) #################### # # # Exercise 10 # # # #################### q10_module_inner_UI <- function(id) { ns <- NS(id) textOutput(outputId = ns("inner_text")) } q10_module_inner <- function(input, output, session, text) { output$inner_text <- renderText(paste("Your text:", text)) } q10_module_outer_UI <- function(id) { ns <- NS(id) q10_module_inner_UI(id = ns("q10_inner")) } q10_module_outer <- function(input, output, session, text) { callModule(module = q10_module_inner, id = "q10_inner", text = text) } shinyApp( ui = fluidPage(q10_module_outer_UI(id = "q10_outer")), server = function(input, output, session) { callModule(module = q10_module_outer, id = "q10_outer", text = "Hello") } )
[/emaillocker]
Leave a Reply