RStudio connectwidget grid layout - r

I am using the connectwidgets R package within a Quatro document to build a catalog page. I plan is to use the grid layout, however it results in a "grid list" rather than a 4x4 grid layout. I am hoping there is a small syntax issue that can be done to resolve my problem.
Here is a reproducable example:
---
title: "Home"
output: html_document
---
```{r message = FALSE, warning = FALSE, echo = FALSE}
library(connectwidgets)
library(dplyr, warn.conflicts = FALSE)
library(readr)
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE)
content <- readr::read_csv("./data/dataset-metadata.csv")
```
```{r}
df <- data.frame(
guid = c("abc100", "abc101", "abc102", "abc103"),
url = c("http://google.com", "http://nfl.com", "http://cbc.com", "http://linkedin.com"),
title = c("Dataset 1", "Dataset 2", "Dataset 3", "Dataset 4"),
app_mode = c("static", "static", "static", "static"),
owner_username = c("owner1", "owner1", "owner2", "owner3"),
updated_time = c("2022-12-26 12:00:00", "2022-12-26 15:15:00", "2022-12-27 09:45:00", "2028-12-28 16:20:00")
)
```
```{r}
df %>%
connectwidgets::rsc_grid()
```
The above code produces the following:

Related

How to suppress downloading progress in HTML file

I am using imfr package to download some IMF data series
library(imfr)
t <- imf_data(database_id = "BOP", indicator = "BCA_BP6_USD",
country = "all", start = "1990", freq = "Q")
I prespecified certain parameters in a separate chunk before to suppress downloading progress
knitr::opts_chunk$set(echo = FALSE, message = FALSE, warning = FALSE, error = FALSE, results = 'hide', fig.keep = 'all')
However, neither of these options did the job. Moreover results = 'hide' suppressed all the output including text and figures.
How can I solve this issue without having a separate chunk for data downloading?
You can capture all those downloads message and progress bar in capture.output and wrap it with invisible. (Got this idea from this question and answer on SO).
And then make a wrapper function imf_data which works the same (mask the imfr::imf_data function) but does not print all those download messages and the progress bar.
---
title: "IMF Data"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
imf_data <- function(...) {
invisible(capture.output(dt <- imfr::imf_data(...)))
return(dt)
}
```
```{r}
library(imfr)
t <- imf_data(database_id = "BOP", indicator = "BCA_BP6_USD",
country = "all", start = "1990", freq = "Q")
```
```{r, comment=""}
head(t)
```

Is there a way to add a title to a table in R

I'm super new to R and I'm struggling to create a header above the column names.
colnames(system_table) <- c("System 1", "System 2", "System 3","System 4")
I want to add a title above these that says "All Systems" but I can't seem to find the function to do so.
You can use something like this in Rmarkdown:
---
title: "Test"
output: html_document
---
```{r, echo=FALSE, warning=FALSE, message=FALSE}
library(magrittr)
library(knitr)
library(formattable)
system_table <- data.frame(v1 = c(1,2,3),
v2 = c(3,2,5),
v3 = c(5,3,2),
v4 = c(3,2,1))
colnames(system_table) <- c("System 1", "System 2", "System 3","System 4")
formattable(system_table) %>%
kable("html", escape = F, caption = "All Systems")
```
Output:

Programmatically create a question_text with multiple answers in r learnr:tutorial

I have the following code with 4 correct answers. I want the students to input all 4 of them. Instead of defining 24 permutations of the answers, I want 4 field boxes that would only accept an answer once.
question_text(
"Input all paths:",
answer("ABEF", correct = TRUE),
answer("ABCDG", correct = TRUE),
answer("ABCDEF",correct = TRUE),
answer("ABDEF", correct = TRUE),
incorrect = "Direction from top to bottom of the plate",
allow_retry = TRUE,
trim = TRUE
)
EDIT
I tried this approach but I do not think I can set the answer as anything other than a single text:
library(gtools)
pat <- permutations(4, 4, c("ABEF","ABCDG","ABCDEF","ABDEF"))
question_text(
"Input all possible rupture paths:",
answer(pat, correct = TRUE),
allow_retry = TRUE,
trim = TRUE
)
Even if I set pat <- c("ABEF","ABCDG","ABCDEF","ABDEF") it does not run successfully. How can define multiple answers at the same time without writing them out.
I'm not sure about your desired output - however, please check the following.
Referring to:
How can define multiple answers at the same time without writing them
out.
You can use lapply to create the answers and do.call to pass the different arguments to question_text:
library(learnr)
do.call(question_text, c(
list("Input all paths:"),
lapply(c("ABEF", "ABCDG", "ABCDEF", "ABDEF"), answer, correct = TRUE),
list(
incorrect = "Direction from top to bottom of the plate",
allow_retry = TRUE,
trim = TRUE
)
))
as *.Rmd file:
---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
knitr::opts_chunk$set(echo = FALSE)
```
```{r two-plus-two, exercise=FALSE}
do.call(question_text, c(
list("Input all paths:"),
lapply(c("ABEF", "ABCDG", "ABCDEF", "ABDEF"), answer, correct = TRUE),
list(
incorrect = "Direction from top to bottom of the plate",
allow_retry = TRUE,
trim = TRUE
)
))
```
Regarding:
I want 4 field boxes that would only accept an answer once
Edit: Added an event handler to access to the answers provided by the user.
---
title: "Tutorial"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r setup, include=FALSE}
library(learnr)
knitr::opts_chunk$set(echo = FALSE)
questions <-
mapply(
FUN = question_text,
lapply(c("ABEF", "ABCDG", "ABCDEF", "ABDEF"), answer, correct = TRUE),
text = paste("Question", 1:4),
incorrect = paste("Incorrect", 1:4),
MoreArgs = list(allow_retry = TRUE,
trim = TRUE),
SIMPLIFY = FALSE
)
```
```{r q1, echo = FALSE}
do.call(quiz, c(list(caption = "Quiz 1"), questions))
```
```{r context="server-start"}
event_register_handler("question_submission", function(session, event, data) {
# names(data):
# "label" "question" "answer" "correct"
message("event: question_submission: ", data$answer)
})
```

Create table with subscripts and coloured rows using Bookdown

I am attempting to generate a PDF from a Bookdown script which includes a complex table. The table includes some parameter names that have subscripts in them. I would also like to colour some of the rows. An example script is shown below:
---
title: "Example problem"
author: "Frida Gomam"
site: bookdown::bookdown_site
documentclass: book
output:
#bookdown::gitbook: default
bookdown::pdf_book: default
always_allow_html: yes
---
This is a test example for the problem.
```{r}
library(magrittr)
library(knitr)
library(kableExtra)
df <- data.frame(Parameter = c("NO~x~ emissions", "SO~2~ emissions", "CO~2~ emissions"), "Value mg/Nm^3^" = c(800,900,1000),check.names=F)
knitr::kable(df,escape = F, caption = 'Example table!', booktabs = TRUE, format = "latex") %>% #
row_spec(0, bold = T, color = "white", background = "#045a8d") %>%
row_spec(c(2), bold = T, color = "white", background = "#3690c0")
```
blah blah
I can run the script using the kable format as 'format = "html"' and the result looks fine including the coloured rows and subscripts. When I change the format to Latex, the subscripts are not displayed properly in the produced pdf.
I have tried adding the argument escape = F to kable, but the build process fails.
Quitting from lines 14-23 (_main.Rmd)
Error in kable_latex(x = c("$NO_{x}$ emissions", "SO2 emissions", "CO2 emissions", :
unused argument (example = FALSE)
Calls: <Anonymous> ... eval -> %>% -> eval -> eval -> <Anonymous> -> do.call
Can anyone help solve this problem?
For me it works if I use (escaped) LaTeX syntax:
---
title: "Example problem"
author: "Frida Gomam"
site: bookdown::bookdown_site
documentclass: book
output:
bookdown::pdf_book: default
#bookdown::gitbook: default
always_allow_html: yes
---
This is a test example for the problem.
```{r}
library(magrittr)
library(knitr)
library(kableExtra)
df <- data.frame(Parameter = c("NO\\textsubscript{x} emissions", "SO\\textsubscript{2} emissions", "CO\\textsubscript{2} emissions"),
"Value mg/Nm\\textsuperscript{3}" = c(800,900,1000),
check.names = F)
knitr::kable(df,escape = F, caption = 'Example table!', booktabs = TRUE, format = "latex") %>% #
row_spec(0, bold = T, color = "white", background = "#045a8d") %>%
row_spec(c(2), bold = T, color = "white", background = "#3690c0")
```
blah blah

RMarkdown pdf_output of ztable | xtable | htmlTable

I successfully generate tables using either ztable, xtable or htmlTable packages with no problems. But, I'd like to know whether these tables may be output as pdf documents. When I try to knit a pdf_output, the table is not displayed. Instead the information is displayed as several strings. I've tried changing the latex engine (using ztable) and other methods with no luck.
I've looked here:
http://www.mzan.com/article/29773068-rmarkdown-latex-table-output-difficulties.shtml
I've also looked in the vignettes, etc.
Example (RMarkdown):
---
output: pdf_document
---
```{r, message = F, results = 'asis'}
# will throw out Error: pandoc document conversion failed with error 43
library(ztable)
data(iris)
options(ztable.type="latex")
zt <- ztable(iris[1:5,], caption = "ztable")
zt <- addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
print(zt)
```
```{r, message = F }
# since it's html, will produce text only
library(htmlTable)
data(mtcars)
colnames(mtcars) <- NULL
htmlTable(mtcars[1:5,], caption = "htmlTable",
cgroup = c("group 1", "group 2"),
n.cgroup = c(5,6))
```
Adding header-includes: \usepackage{colortbl} resolves the error for ztable. I don't think htmlTable has a latex engine.
full code:
---
output: pdf_document
header-includes: \usepackage{colortbl}
---
```{r, message = F, results = 'asis'}
library(ztable)
data(iris)
options(ztable.type="latex")
zt <- ztable(iris[1:5,], caption = "ztable")
zt <- addcgroup(zt,
cgroup = c("group 1", "group 2"),
n.cgroup = c(2,3))
print(zt)
```
```{r, message = F }
# since it's html, will produce text only
library(htmlTable)
data(mtcars)
colnames(mtcars) <- NULL
htmlTable(mtcars[1:5,], caption = "htmlTable",
cgroup = c("group 1", "group 2"),
n.cgroup = c(5,6))
```

Resources