How do I get grid.draw to render in a RMarkdown chunk? - grid

I am trying to include a venndiagram in my Rmarkdown report. In order to return the object itself filename is set to NULL; the figure has then to be rendered using grid.draw(). When I do this in the console the figure displays just fine; when I do it in the code chunk nothing is returned in line. How do you get grid.draw to return a image in line from an Rmarkdown code chunk?
The following code is in one of the code chunks:
require(VennDiagram)
list1<-c("A", "B", "C", "D")
list2<-c("B", "D", "E")
list3<-c("A", "D", "Z")
p<-venn.diagram(
x=list( list1, list2, list3),
category.names= c("list1", "list2", "list3"),
filename= NULL
)
grid.draw(p)

What output format are you using? might worth updating your R/Rstudio programs as well. When I run this in a plain Rmarkdown file, I get what I assume is the desired output. I always try to output to HTML, and then print the HTML document to a .PDF from the web browser if needed to retain as much HTML formatting as possible
---
title: "Untitled"
author: "Author"
date: "10/13/2021"
output: html_document
---
```{r}
library(VennDiagram)
list1<-c("A", "B", "C", "D")
list2<-c("B", "D", "E")
list3<-c("A", "D", "Z")
p<-venn.diagram(
x=list( list1, list2, list3),
category.names= c("list1", "list2", "list3"),
filename= NULL
)
grid.draw(p)
```

Related

How to set default values in filter_select() in Crosstalk() in R - Plotly

I'm developing a rmarkdown HTML with plotly based interactive charts. While i could accomplish everything i want to have in the chart, the filter_select() from crosstalk() is not letting me to set default values in it. So my charts during initial loading looks clumpsy and bad.
Selecting a default value in an R plotly plot using a selectize box via crosstalk in R, using static html not shiny
The above discussion has some inputs but i don't know how to make those edits in the crosstalk() as i'm not in to HTML/JS. can someone help me with some detailed approach pls,
thanks
Borrowing the example from the linked question, you've got the following for starters:
---
output:
html_document
---
```{r echo=FALSE, message=FALSE, warning=FALSE}
library(plotly)
# example data
dat <- tibble::tribble(~filterBy, ~x, ~y,
"a", 1, 1,
"b", 2, 1,
"a", 1, 2,
"b", 2, 2,
"a", 1, 3,
"b", 2, 3,
"a", 1, 2,
"b", 2, 3,
"c", 3, 1,
"c", 3, 2,
"c", 3, 3
)
# initializing a crosstalk shared data object
plotdat <- highlight_key(dat)
# Filter dropdown
question_filter <- crosstalk::filter_select(
"filter", "Select a group to examine",
plotdat, ~filterBy, multiple = F
)
# Plotting:
plot <- plot_ly( plotdat,
x = ~x, y = ~y, text = ~filterBy, mode = "markers+text",
textposition = "top", hoverinfo = "x+y"
)
```
You would paste the js from the accepted answer below the end of the r code, just as if you were making a new {r} block beneath the first one.
```{js}
function filter_default() {
document.getElementById("filter").getElementsByClassName("selectized")
[0].selectize.setValue("a", false);
}
window.onload = filter_default;
```
To get this to work for you, there are a few arguments you'll likely need to change in the {js} block.
1. First, you'll want to look back at what you used as the tag for your filter_select element. This is the first argument. In the example above, filter_select("filter", means that you have used "filter" as your tag for the filter.
Say we had instead used "lantern" as our id for the crosstalk filter. You would change document.getElementById("filter") to document.getElementById("lantern") in the {js}.
2. Next, you'll want to look at what value is the default selection. In the example, the value is set to "a" with the selectize.setValue("a" bit in the {js} block. You have the ability to choose whatever value that exists in your data as a default. If, for example, you have source data:
other_dat<-data.frame(light=c("bulb","sun","biological"),amount=c(50,1000,3))
you could use (keeping in mind that we have chosen to tag our filter_select as "lantern"):
```{js}
function filter_default() {
document.getElementById("lantern").getElementsByClassName("selectized")
[0].selectize.setValue("bulb", false);
}
window.onload = filter_default;
```
to set the default filter_select value to "bulb", or:
```{js}
function filter_default() {
document.getElementById("lantern").getElementsByClassName("selectized")
[0].selectize.setValue("sun", false);
}
window.onload = filter_default;
```
to set the default value to "sun".
In case there are multiple select inputs on one page. Also, $(document).ready() works better than window.onload.
function filter_default(){
document.getElementById("course_name").getElementsByClassName("selectized")[0].selectize.setValue("Course 1",false)
document.getElementById("Student_Group_Name").getElementsByClassName("selectized")[0].selectize.setValue("Group 1",false)
document.getElementById("student_group_name_2").getElementsByClassName("selectized")[0].selectize.setValue("Group 1",false)
}
$(document).ready(filter_default);

R Shiny split lines of text output from renderText()

I am making a shiny app, and in the app one feature is to output text that is generated as a list of character strings.
For example, one function outputs a list of text, for example
("a", "b", "c", "d")
When I render this text, it outputs it as such
a b c d
Instead I would like the text to be rendered so that there is a line in between each value of the list, so instead it would look like
a
b
c
d
How can I accomplish this?
You could try:
library(shiny)
ui <- fluidPage(
htmlOutput("mytext")
)
server <- function(input, output, session) {
output$mytext <- renderUI({
mylist <- c("a", "b", "c", "d")
HTML(paste(mylist, sep = "", collapse = '<br/>'))
})
}
shinyApp(ui, server)
Adding to #AndS. answer, another option would be using renderText() with htmlOutput(), and then simply paste(mylist, collapse = "<br>")

Dollar sign causes error in colored rows

I have a table that I'm trying to put into a pdf with R Markdown.
dt <- c(name = c("a", "b", "c"),
money = c("$1", "$10", "$100")
dt %>%
kable(format = "latex") %>%
row_spec(1, background = "blue")
The above code does not work, producing the following error:
Error in stri_replace_first_regex(string, pattern,
fix_replacement(replacement), : Trying to access the index that is out
of bounds. (U_INDEX_OUTOFBOUNDS_ERROR) Calls: ...
row_spec_latex -> str_replace -> stri_replace_first_regex ->. Call
If I remove the row_spec portion, it works. If I remove the dollar signs, it works. If I change the row to be row 0 instead of row 1, it works. But I cannot change the colors of rows with dollar signs in them. I know that $ is a reserved character in latex, but as an experiment I looked up all the reserved characters and tried it with them instead of the dollar sign and everything worked. Dollar sign is the only one that is giving me trouble.
Is there some way to make this work or am I doomed to having a stark white table?
We can use the latex code \textdollar for the dollar sign and escape it. Additional using kable() option escape=FALSE.
---
title: "Untitled"
author: "C.garner"
date: "2 Februar 2018"
output:
pdf_document:
keep_tex: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r table1, message=FALSE}
library(dplyr)
library(kableExtra)
library(knitr)
dt <- c(name = c("a", "b", "c"),
money = c("\\textdollar 1", "\\textdollar 10", "\\textdollar 100"))
dt %>%
kable(format = "latex", escape = FALSE) %>%
row_spec(4, background = "blue")
```

Display a data.frame with mathematical notation in table header R Markdown html output

Say I'd like to display a table of coefficients from several equations in an R Markdown file (html output).
I'd like the table to look somewhat like this:
But I can't for the life of me figure out how to tell R Markdown to parse the column names in the table.
The closest I've gotten is a hacky solution using cat to print custom table from my data.frame... not ideal. Is there a better way to do this?
Here's how I created the image above, saving my file as an .Rmd in RStudio.
---
title: "Math in R Markdown tables"
output:
html_notebook: default
html_document: default
---
My fancy table
```{r, echo=FALSE, include=TRUE, results="asis"}
# Make data.frame
mathy.df <- data.frame(site = c("A", "B"),
b0 = c(3, 4),
BA = c(1, 2))
# Do terrible things to print it properly
cat("Site|$\\beta_0$|$\\beta_A$")
cat("\n")
cat("----|---------|---------\n")
for (i in 1:nrow(mathy.df)){
cat(as.character(mathy.df[i,"site"]), "|",
mathy.df[i,"b0"], "|",
mathy.df[i,"BA"],
"\n", sep = "")
}
```
You can use kable() and its escape option to format math notation (see this answer to a related question). Then you assign your mathy headings as the column names, and there you go:
---
title: "Math in R Markdown tables"
output:
html_notebook: default
html_document: default
---
My fancy table
```{r, echo=FALSE, include=TRUE, results="asis"}
library(knitr)
mathy.df <- data.frame(site = c("A", "B"),
b0 = c(3, 4),
BA = c(1, 2))
colnames(mathy.df) <- c("Site", "$\\beta_0$", "$\\beta_A$")
kable(mathy.df, escape=FALSE)
```

Different pander performance between .Rmd and .Rnw

I'm having some trouble getting pander to work the same way in a .Rnw file as it does in my .Rmd file. In both, I'm using knitr to weave the pdf. My .Rmd file looks like
---
title: "My title"
output: pdf_document
---
```{r}
library(pander)
panderOptions('big.mark', ',')
panderOptions('table.style','rmarkdown')
panderOptions('table.alignment.default','right')
panderOptions('table.alignment.rownames','left')
panderOptions('digits','10')
df <- data.frame('a' = 1:3, 'b' = 4:6, 'c' = 7:9)
pander(df)
```
The dataframe looks really nice in the output as it converts the dashed lines into solid ones. However when I try to do something similar in my .Rnw file, everything is printed as if it were a sentence rather than a table.
\documentclass{article}
\begin{document}
<<model_data>>=
library(pander)
panderOptions('big.mark', ',')
panderOptions('table.style','rmarkdown')
panderOptions('table.alignment.default','right')
panderOptions('table.alignment.rownames','left')
panderOptions('digits','10')
df <- data.frame('a' = 1:3, 'b' = 4:6, 'c' = 7:9)
pander(df)
#
\end{document}
This looks terrible, I can call pandoc.table(df) instead and then its printed in a table format at least but it has the dashed lines unlike when its printed in the .Rmd file. How to I get it to print exactly like it does in the .Rmd file but inside of an .Rnw file?

Resources