# Exercice module : mtcars
#' @title Exercice module : mtcars
#' @description mtcars
#'
#' @param id shiny id
#' @param input internal
#' @param output internal
#' @param session internal
#'
#' @rdname mod_exercice_mtcars
#'
#' @keywords internal
#' @importFrom shiny NS tagList fluidRow
#' @importFrom shinyAce aceEditor
mod_exercise_mtcars_ui <- function(id){
ns <- NS(id)
fluidRow(
setup(),
column(
width = 12,
id = "mtcars",
exercice(
title = "mtcars",
info = "exercices/mtcars/intro.Rmd",
data = ns('expected_mtcars')
#data = list(mtcars = ns('mtcars'))
),
question(
ns = ns,
id = "select_cols",
question = "mtcars/select_cols.Rmd",
expected_data = ns('expected_selected_cols'),
sql = list(
code = list(
value = "SELECT disp, drat, gear \nFROM mtcars",
comment = HTML("This is a SQL editor block. \n-- Edit it and submit your answer by clicking on Run")
),
result = list(output = ns("sql_result_select_cols"))
),
r = list(
code = list(
value = "mtcars %>%\n select(disp, drat, gear)",
comment = "This is a R editor block. \n# Edit it and submit your answer by clicking on Run"
),
result = list(output = ns("r_result_select_cols"))
)
),
question(
ns = ns,
id = "avg_cyl",
question = "mtcars/avg_cyl.Rmd",
expected_data = ns('expected_avg_cyl'),
sql = list(
code = list(value = "SELECT AVG(cyl) AS avg_cyl \nFROM mtcars"),
result = list(output = ns("sql_result_avg_cyl"))
),
r = list(
code = list(value = "mtcars %>% \n summarise(avg_cyl = mean(cyl))"),
result = list(output = ns("r_result_avg_cyl"))
)
),
warning_section(file = "/exercices/mtcars/keep_training.Rmd")
)
)
}
# Module Server
#' @rdname mod_exercice_mtcars
#' @keywords internal
#' @import dplyr
#' @import shiny
#' @importFrom magrittr "%>%"
mod_exercise_mtcars_server <- function(id) {
moduleServer(
id,
function(input, output, session) {
ns <- session$ns
cars <- sqlnstructor::mtcars
output$expected_mtcars <- render_expected_table(cars, 5)
# 1st exercice : select_cols ----
select_cars <- cars %>% select('disp', 'drat', 'gear')
output$expected_selected_cols <- render_expected_table(select_cars)
output$sql_result_select_cols <- renderUI({
sql_evaluation(
input = input,
question_id = "select_cols",
expected_output = select_cars
)
})
output$r_result_select_cols <- renderUI({
r_evaluation(
input = input,
question_id = "select_cols",
expected_output = select_cars
)
})
####
# 2nd exercice : avg_cyl ----
avg_cyl <- cars %>% summarise(avg_cyl = mean('cyl'))
output$expected_avg_cyl <- render_expected_table(avg_cyl)
output$sql_result_avg_cyl <- renderUI({
sql_evaluation(
input = input,
question_id = "avg_cyl",
expected_output = avg_cyl
)
})
output$r_result_avg_cyl <- renderUI({
r_evaluation(
input = input,
question_id = "avg_cyl",
expected_output = avg_cyl
)
})
}
)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.