Script for interactivity in bookdown::bs4_book vs bookdown::gitbook - r

I am interested in incorporating a simple, radio button-style quiz in a bookdown::bs4_book environment. I have a minimal example below that works when I specify bookdown::gitbook in the header, but stops working when I choose bookdown::bs4_book. Does anyone have any ideas how to get this working with the bookdown::bs4_book option?
(note: the .Rmd code below needs to be saved as "index.Rmd" in order to be knitted using bs4_book.)
Thank you,
Luke
---
title: "formative_test"
site: bookdown::bookdown_site
output:
# bookdown::gitbook:
bookdown::bs4_book:
css: [style.css, font-awesome.min.css]
repo: https://github.com/rstudio/bookdown-demo
---
# Chapter 1
```{r results = "asis", echo = FALSE}
question <- function(question, distractors, correct, no, fb = "", print_question = TRUE){
allanswers <- c(distractors, correct)[sample.int(length(distractors)+1)]
correctanswer <- which(allanswers == correct)
answercode <- paste0(sapply(1:length(allanswers), function(i){
x <- allanswers[i]
paste0('<div class="radio">\n <label>\n <input type="radio" name="question', no, '" id="opt', i,'" value="', i, '" onchange="check_answer', no, '()">\n ', x, '\n </label>\n</div>')
}), collapse = "\n\n")
out <- paste0(question, "\n\n", answercode,
'<div class="collapse" id="collapseExample', no,'">
<div class="card card-body" id="answerFeedback', no, '">
</div>
</div>',
paste0('<script type="text/javascript">
function check_answer', no, '()
{
var radioButtons', no, ' = document.getElementsByName("question', no, '");
document.getElementById("answerFeedback', no, '").innerHTML = "Try selecting an answer!!";
for(var i = 0; i < radioButtons', no, '.length; i++)
{
if(radioButtons', no, '[i].checked == true)
{
var feedback', no, ' = "<p style=\'color:red\'>Wrong', ifelse(fb == "", ".", paste0("; ", fb)), '</p>";
if(radioButtons', no, '[i].value == "', correctanswer, '") {
feedback', no, ' = "<p style=\'color:green\'>Correct!</p>"
}
document.getElementById("answerFeedback', no, '").innerHTML = feedback', no, ';
return true;
}
}
}
</script>
'))
if(print_question){
cat(out)
} else {
return(out)
}
}
questionnaire <- function(x, shuffle = TRUE, print_question = TRUE){
if(inherits(x, "character")) x <- read.csv(x, stringsAsFactors = FALSE, fileEncoding="UTF-8-BOM")
if(!all(names(x) == c("question", "distractors", "correct", "fb"))){
stop("Incorrect column names")
}
if(shuffle){
x <- x[sample.int(nrow(x)), ]
}
out <- ""
for(i in 1:nrow(x)){
out <- paste0(out,
"**Question ",
i,
":**\n",
question(question = x$question[i],
distractors = eval(parse(text = x$distractors[i])),
correct = x$correct[i],
no = i,
fb = ifelse(is.na(x$fb[i]), "", x$fb[i]),
print_question = FALSE),
"\n\n"
)
}
if(print_question){
cat(out)
} else {
return(out)
}
}
questionnaire(
x = data.frame(
question = "True or False?",
distractors = "\"FALSE\"",
correct = TRUE,
fb = "here is some example feedback"
)
)

Related

How to add multiple scrollbar in R Shiny data table

I am trying to build a shiny app that has the following feature:
It has a parent-child feature that can expand and collapse as per
user interaction (Requirement 1 - Done)
When the rows are expanded, several child rows are displayed in the
data table. I want to introduce multiple scroll bars in this table.
1st scrollbar will be for 1st 4 columns and another scrollbar for the
rest of the columns. (Requirement 2 - Not Done)
The below code is able to produce results for the 1st requirement (using JQuery) however, I am unable to find a way out for Requirement 2.
Can anyone assist here?
packages = c(
'shiny',
'shinydashboard',
'tidyverse',
'dplyr',
'magrittr',
'plotly',
'ggplot2',
'scales',
'DT',
"shinyWidgets",
"fontawesome"
)
for (p in packages) {
if (!require(p, character.only = T)) {
install.packages(p)
}
library(p, character.only = T)
}
DataIn <- mtcars
DataIn <- DataIn %>% tidyr::nest(-cyl)
DataIn <- DataIn %>%
{
bind_cols(data_frame(
' ' = rep(
'<img src=\"https://raw.githubusercontent.com/DataTables/DataTables/master/examples/resources/details_open.png\"/>',
nrow(.)
)
), .)
}
# get dynamic info and strings
nested_columns <-
which(sapply(DataIn, class) == "list") %>% setNames(NULL)
not_nested_columns <-
which(!(seq_along(DataIn) %in% c(1, nested_columns)))
not_nested_columns_str <-
not_nested_columns %>% paste(collapse = "] + '_' + d[") %>% paste0("d[", ., "]")
CallBack <- paste0(
"
table.column(1).nodes().to$().css({cursor: 'pointer'});
// Format data object (the nested table) into another table
var format = function(d) {
if(d != null){
var result = ('<table id=\"child_' + ",
not_nested_columns_str,
" + '\">') + '<thead><tr>'
for (var col in d[",
nested_columns,
"][0]){
result += '<th>' + col + '</th>'
}
result += '</tr></thead></table>'
return result
}else{
return '';
}
}
var format_datatable = function(d) {
var dataset = [];
for (i = 0; i <= d[",
nested_columns,
"].length-1; i++) {
var datarow = $.map(d[",
nested_columns,
"][i], function(value, index) {
return [value];
});
dataset.push(datarow);
}
var subtable = $(('table#child_' + ",
not_nested_columns_str,
")).DataTable({
'data': dataset,
'autoWidth': true,
'deferRender': true,
'info': false,
'lengthChange': false,
'ordering': true,
'paging': false,
'scrollX': false,
'scrollY': false,
'searching': false
// 'fnRowCallback': function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
// $('td', nRow).css('background-color', 'Red')}
});
};
table.on('click', 'td.details-control', function() {
var td = $(this), row = table.row(td.closest('tr'));
if (row.child.isShown()) {
row.child.hide();
td.html('<img src=\"https://raw.githubusercontent.com/DataTables/DataTables/master/examples/resources/details_open.png\"/>');
} else {
row.child(format(row.data())).show();
td.html('<img src=\"https://raw.githubusercontent.com/DataTables/DataTables/master/examples/resources/details_close.png\"/>');
format_datatable(row.data())
}
});"
)
shinyApp(
ui = fluidPage(DT::dataTableOutput('tbl')),
server = function(input, output) {
output$tbl = DT::renderDataTable(datatable(
DataIn,
escape = -2,
# raw HTML in column 2
options = list(columnDefs = list(
list(visible = FALSE, targets = c(0, nested_columns)),
# Hide row numbers and nested columns
list(
orderable = FALSE,
className = 'details-control',
targets = 1
) # turn first column into control column
)),
callback = JS(CallBack)
))
}
)

How to start DT datatable with all child rows expanded?

In this DT example with child rows, how to start the table with all the child rows expanded?
library(DT)
datatable(
cbind(' ' = '⊕', mtcars), escape = -2,
options = list(
columnDefs = list(
list(visible = FALSE, targets = c(0, 2, 3)),
list(orderable = FALSE, className = 'details-control', targets = 1)
)
),
callback = JS("
table.column(1).nodes().to$().css({cursor: 'pointer'});
var format = function(d) {
return '<div style=\"background-color:#eee; padding: .5em;\"> Model: ' +
d[0] + ', mpg: ' + d[2] + ', cyl: ' + d[3] + '</div>';
};
table.on('click', 'td.details-control', function() {
var td = $(this), row = table.row(td.closest('tr'));
if (row.child.isShown()) {
row.child.hide();
td.html('⊕');
} else {
row.child(format(row.data())).show();
td.html('&CircleMinus;');
}
});"
))
PS: stackoverflow forced me to include more details to the question but there is nothing else to add...
You can use your existing callback to also iterate over each row in the table. In that iteration you can create and open each child record:
table.rows().every( function () {
this.child( format(this.data()) ).show();
} );
This snippet needs to be appended to the end of your callback = JS(...) option as shown below:
callback = JS(
"
table.column(1).nodes().to$().css({cursor: 'pointer'});
var format = function(d) {
return '<div style=\"background-color:#eee; padding: .5em;\"> Model: ' +
d[0] + ', mpg: ' + d[2] + ', cyl: ' + d[3] + '</div>';
};
table.on('click', 'td.details-control', function() {
var td = $(this), row = table.row(td.closest('tr'));
if (row.child.isShown()) {
row.child.hide();
td.html('⊕');
} else {
row.child(format(row.data())).show();
td.html('&CircleMinus;');
}
});
table.rows().every( function () {
this.child( format(this.data()) ).show();
} );"
)
The result:

Exporting an Excel file from R with columns color coded?

When exporting the excel file the columns are not color coded. I am using the condformat package. i know that condformat creates a html file, i am using the condformat2excel command but its not color coding the columns i need. any help or tip are welcome thank you.
library(readxl)
library(condformat)
teast1 <- read_excel("Kenilworth_DataIssues-testR.xls",sheet = "Data Cleanup")
color_pick <- function(column) {
sapply(column,
FUN = function(value) {
if (value >= 25) {
return("red")
} else {
return("dodgerblue")
}
})
}
color_pick2 <- function(column2) {
sapply(column2,
FUN = function(value2) {
if (value2 >9000) {
return("red")
} else if(value2 < 8000) {
return("dodgerblue")
}else{
return("green")
}
})
}
condformat2excel(teast1,"flip.xlsx",sheet_name = "lebeon",overwrite_wb = FALSE,overwrite_sheet = TRUE)%>%
rule_fill_discrete("PlannedPremiseTime",expression = color_pick(PlannedPremiseTime),colours = identity)%>%
rule_fill_discrete("PostalCode",expression = color_pick2(PostalCode),colours = identity)

Error in textConnection(): all connections are in use

I have read most of the posts concerning an error of this type but neither applies to my case. I am new in R, working on an assignment for school based on Nolan and Lang's book Data Science Case Studies in R. I am working on using stats to identify spam, url for the code can be found here, which require working with files from http://spamassassin.apache.org/old/publiccorpus/ (which are pretty big)
Now the problem I am facing is the following (just posting the chunks of code where I have encountered the issue):
sampleSplit = lapply(sampleEmail, splitMessage)
processHeader = function(header)
{
# modify the first line to create a key:value pair
header[1] = sub("^From", "Top-From:", header[1])
headerMat = read.dcf(textConnection(header), all = TRUE)
headerVec = unlist(headerMat)
dupKeys = sapply(headerMat, function(x) length(unlist(x)))
names(headerVec) = rep(colnames(headerMat), dupKeys)
return(headerVec)
}
headerList = lapply(sampleSplit,
function(msg) {
processHeader(msg$header)} )
contentTypes = sapply(headerList, function(header)
header["Content-Type"])
names(contentTypes) = NULL
contentTypes
hasAttach = grep("^ *multi", tolower(contentTypes))
hasAttach
boundaries = getBoundary(contentTypes[ hasAttach ])
boundaries
boundary = boundaries[9]
body = sampleSplit[[15]]$body
bString = paste("--", boundary, sep = "")
bStringLocs = which(bString == body)
bStringLocs
eString = paste("--", boundary, "--", sep = "")
eStringLoc = which(eString == body)
eStringLoc
diff(c(bStringLocs[-1], eStringLoc))
### This code has mistakes in it - and we fix them later!
processAttach = function(body, contentType){
boundary = getBoundary(contentType)
bString = paste("--", boundary, "$", sep = "")
bStringLocs = grep(bString, body)
eString = paste("--", boundary, "--$", sep = "")
eStringLoc = grep(eString, body)
n = length(body)
if (length(eStringLoc) == 0) eStringLoc = n + 1
if (length(bStringLocs) == 1) attachLocs = NULL
else attachLocs = c(bStringLocs[-1], eStringLoc)
msg = body[ (bStringLocs[1] + 1) : min(n, (bStringLocs[2] - 1),
na.rm = TRUE)]
if ( eStringLoc < n )
msg = c(msg, body[ (eStringLoc + 1) : n ])
if ( !is.null(attachLocs) ) {
attachLens = diff(attachLocs, lag = 1)
attachTypes = mapply(function(begL, endL) {
contentTypeLoc = grep("[Cc]ontent-[Tt]ype", body[ (begL + 1) : (endL - 1)])
contentType = body[ begL + contentTypeLoc]
contentType = gsub('"', "", contentType )
MIMEType = sub(" *Content-Type: *([^;]*);?.*", "\\1", contentType)
return(MIMEType)
}, attachLocs[-length(attachLocs)], attachLocs[-1])
}
if (is.null(attachLocs)) return(list(body = msg, attachInfo = NULL) )
else return(list(body = msg,
attachDF = data.frame(aLen = attachLens,
aType = attachTypes,
stringsAsFactors = FALSE)))
}
bodyList = lapply(sampleSplit, function(msg) msg$body)
attList = mapply(processAttach, bodyList[hasAttach],
contentTypes[hasAttach],
SIMPLIFY = FALSE)
lens = sapply(attList, function(processedA)
processedA$attachDF$aLen)
head(lens)
attList[[2]]$attachDF
body = bodyList[hasAttach][[2]]
length(body)
body[35:45]
processAttach = function(body, contentType){
n = length(body)
boundary = getBoundary(contentType)
bString = paste("--", boundary, sep = "")
bStringLocs = which(bString == body)
eString = paste("--", boundary, "--", sep = "")
eStringLoc = which(eString == body)
if (length(eStringLoc) == 0) eStringLoc = n
if (length(bStringLocs) <= 1) {
attachLocs = NULL
msgLastLine = n
if (length(bStringLocs) == 0) bStringLocs = 0
} else {
attachLocs = c(bStringLocs[ -1 ], eStringLoc)
msgLastLine = bStringLocs[2] - 1
}
msg = body[ (bStringLocs[1] + 1) : msgLastLine]
if ( eStringLoc < n )
msg = c(msg, body[ (eStringLoc + 1) : n ])
if ( !is.null(attachLocs) ) {
attachLens = diff(attachLocs, lag = 1)
attachTypes = mapply(function(begL, endL) {
CTloc = grep("^[Cc]ontent-[Tt]ype", body[ (begL + 1) : (endL - 1)])
if ( length(CTloc) == 0 ) {
MIMEType = NA
} else {
CTval = body[ begL + CTloc[1] ]
CTval = gsub('"', "", CTval )
MIMEType = sub(" *[Cc]ontent-[Tt]ype: *([^;]*);?.*", "\\1", CTval)
}
return(MIMEType)
}, attachLocs[-length(attachLocs)], attachLocs[-1])
}
if (is.null(attachLocs)) return(list(body = msg, attachDF = NULL) )
return(list(body = msg,
attachDF = data.frame(aLen = attachLens,
aType = unlist(attachTypes),
stringsAsFactors = FALSE)))
}
readEmail = function(dirName) {
# retrieve the names of files in directory
fileNames = list.files(dirName, full.names = TRUE)
# drop files that are not email
notEmail = grep("cmds$", fileNames)
if ( length(notEmail) > 0) fileNames = fileNames[ - notEmail ]
# read all files in the directory
lapply(fileNames, readLines, encoding = "latin1")
}
processAllEmail = function(dirName, isSpam = FALSE)
{
# read all files in the directory
messages = readEmail(dirName)
fileNames = names(messages)
n = length(messages)
# split header from body
eSplit = lapply(messages, splitMessage)
rm(messages)
# process header as named character vector
headerList = lapply(eSplit, function(msg)
processHeader(msg$header))
# extract content-type key
contentTypes = sapply(headerList, function(header)
header["Content-Type"])
# extract the body
bodyList = lapply(eSplit, function(msg) msg$body)
rm(eSplit)
# which email have attachments
hasAttach = grep("^ *multi", tolower(contentTypes))
# get summary stats for attachments and the shorter body
attList = mapply(processAttach, bodyList[hasAttach],
contentTypes[hasAttach], SIMPLIFY = FALSE)
bodyList[hasAttach] = lapply(attList, function(attEl)
attEl$body)
attachInfo = vector("list", length = n )
attachInfo[ hasAttach ] = lapply(attList,
function(attEl) attEl$attachDF)
# prepare return structure
emailList = mapply(function(header, body, attach, isSpam) {
list(isSpam = isSpam, header = header,
body = body, attach = attach)
},
headerList, bodyList, attachInfo,
rep(isSpam, n), SIMPLIFY = FALSE )
names(emailList) = fileNames
invisible(emailList)
}
Everything runs fine right up to:
emailStruct = mapply(processAllEmail, fullDirNames,
isSpam = rep( c(FALSE, TRUE), 3:2))
emailStruct = unlist(emailStruct, recursive = FALSE)
sampleStruct = emailStruct[ indx ]
save(emailStruct, file="emailXX.rda")
I get the error Error in textConnection(header) : all connections are in use, therefore it doesn't recognize "emailStruct", which I need later on. I seriously don't know how to overcome it so that I can continue with the rest of the code, which requires some of these variables to work.
When you run textConnection() you are opening a text connection, but you are never closing it. Try closing it explicitly after you read from it
read.dcf(tc<-textConnection(header), all = TRUE)
close(tc)

R Wait until system executable is finished

I am trying to run a series of input files (located in my C:/GenSoftware/Colony/datFiles/ directory) through an executable Colony2.exe that is located in my C:/GenSoftware/Colony/ directory. I attempt to rename file 1, copy it to the same directory as the executable, run Colony2.exe using the run.colony function (pasted at the bottom) in the package rcolony, delete the file, and proceed to file 2.
However, the code attempts to continue before the executable is finished. How can I get my loop to wait until the Colony2.exe is finished before it proceeds to the next line of code and then re-run the loop. run.colony invokes the system command (pasted at bottom).
Here is my code thus far...
rm(list=ls())
setwd("C:/GenSoftware/Colony/")
getwd()
datFiles <- list.files("datFiles")
library(rcolony)
d <- 0
for (d in 1:length(datFiles))
{
d <- d+1
setwd("C:/GenSoftware/Colony/datFiles/")
file.rename(datFiles[d],"Colony2.DAT")
file.copy(from = "C:/GenSoftware/Colony/datFiles/Colony2.DAT",to = "C:/GenSoftware/Colony/")
datPath <- "C:/GenSoftware/Colony/Colony2.DAT"
setwd("C:/GenSoftware/Colony/")
run.colony(colonyexecpath = "Colony2.exe", datPath, wait = TRUE, monitor = FALSE)
unlink(x = "C:/GenSoftware/Colony/Colony2.DAT", recursive = FALSE, force = TRUE)
setwd("C:/GenSoftware/Colony/datFiles/")
file.rename("Colony2.DAT",datFiles[d])
}
######## END OF MY CODE, START OF run.colony CODE
run.colony
function (colonyexecpath = "prompt", datfilepath = "prompt",
wait = FALSE, monitor = TRUE)
{
if (colonyexecpath == "prompt") {
cat("Please click to select your Colony2 executable (probably called Colony2.exe or Colony2).\n\n")
flush.console()
colonyexecpath <- file.choose()
}
if (datfilepath == "prompt") {
cat("Please click to select your DAT file.\n\n")
flush.console()
datfilepath <- file.choose()
}
datadir <- sub("([A-Z a-z0-9:/\\]+[/\\]+)([A-Z.a-z0-9]+)",
"\\1", datfilepath)
filename <- sub("([A-Z a-z0-9:/\\]+[/\\]+)([A-Z.a-z0-9]+)",
"\\2", datfilepath)
colonyexec <- sub("([A-Z a-z0-9:/\\]+[/\\]+)([A-Z.a-z0-9]+)",
"\\2", colonyexecpath)
current.wd <- getwd()
x <- readLines(paste(datadir, filename, sep = ""), n = 2)
outputfilename <- substring(x[2], 1, 20)
outputfilename <- sub("^[\t\n\f\r ]*", "", outputfilename)
outputfilename <- sub("[\t\n\f\r ]*$", "", outputfilename)
outputfilename
if (file.exists(paste(datadir, outputfilename, ".MidResult",
sep = ""))) {
stop("\nThere are output files already in the directory. \nColony has already run. \nTry deleting (or moving) these files and starting again.\n")
}
setwd(datadir)
if (monitor == TRUE & wait == TRUE) {
stop("If you want to monitor the output, you must set wait as FALSE. Otherwise you cannot run other functions in the same R console.")
}
cat("Be aware: this may take several minutes, hours, or even weeks to run, depending on the settings used.\n")
platform <- .Platform
if (platform$OS.type == "unix") {
if (file.exists("Colony2") == FALSE) {
system(paste("cp", colonyexecpath, datadir, sep = " "))
}
if (filename != "Colony2.DAT") {
system(paste("mv", paste(datadir, filename, sep = ""),
paste(datadir, "Colony2.DAT", sep = ""), sep = " "))
}
if (filename != "Colony2.DAT") {
system(paste("cp", paste(datadir, "Colony2.DAT",
sep = ""), paste(datadir, filename, sep = ""),
sep = " "))
}
cat("#! /bin/sh\necho Running Colony2\nexport G95_MEM_SEGMENTS=0\n./Colony2",
file = paste(datadir, "Colony2.sh", sep = ""), append = FALSE)
if (monitor == TRUE) {
system("sh Colony2.sh | tee temp.txt", wait = wait)
}
else {
system("sh Colony2.sh", wait = wait)
}
system(paste("rm", colonyexec))
if (file.exists("Colony2.sh")) {
system(paste("rm Colony2.sh"))
}
else {
}
if (filename != "Colony2.DAT") {
system("rm Colony2.DAT")
}
}
else {
if (platform$OS.type == "windows") {
shell(paste("copy", colonyexecpath, datadir, sep = " "))
if (filename != "Colony2.DAT") {
shell(paste("rename", paste(datadir, filename,
sep = ""), paste(datadir, "Colony2.DAT", sep = ""),
sep = " "))
}
shell.exec("Colony2.exe")
if (filename != "Colony2.DAT") {
shell(paste("rename", paste(datadir, "Colony2.DAT",
sep = ""), paste(datadir, filename, sep = ""),
sep = " "))
}
shell("del Colony2.exe")
}
else {
stop(paste("This function is not correctly configured to run on",
platform$OS.type, "systems."))
}
}
setwd(current.wd)
}

Resources