How do I remove this Rmarkdown output after the read_excel function? - r

How do I go about removing the output shown in the picture below after I use read_excel to import the data in r markdown. Basically I don't want there to be any output after this function. Please see attached image.
bdims <- read_excel("bdims.xlsx")
head(bdims)

You can set results = 'hide'. So if eg. for standard rmarkdown. This will stop the output from showing. Read on the cheat sheet how to use other settings such as echo and eval
{results = 'hide'}
bdims <- read_excel("bdims.xlsx")
or with roxygen document for rendering rmarkdown,
#+ results = 'hide'
bdims <- read_excel("bdims.xlsx")

Related

Print HTML formatted text in Jupyter with R from the list of citations of loaded packages

I have a R variable containing some html content inside, for exemple :
myvar = "<h3>Section Title</h3>"
I would like to print it not as <h3>Section Title</h3> but as a formated h3 title in html or markdown that jupyter notebook understands.
I have been looking a bit everywhere and found htmlwidget or knitr but I feel like it needs a HTML file. What I would like to do is just displaying a variable.
I have tried also with htmltools package and the HTML() function, but no success...
myvar = "<h3>Section Title</h3>"
# I would expect that I can do something like:
print(HTML(myvar), format='html')
# But it doesn't work, I just get:
# => <h3>Section Title</h3>
And to be very clear, the goal is also that when I save the notebook as HTML or as PDF, the html is displayed formatted (and not as raw text)
In Fine the goal is to display the citations for loaded packages, in a aesthetic way. I know that I can use print(citation("packagename"), style='html') to output it as HTML but I can't find how to format the HTML properly. That's the reason my idea is to capture this output in a variable and output it as formatted HTML.
So I found the simplest option (I think) which doesn't require any other packages than htmltools which I think is by default installed.
How to display and format HTML in R
The key is the function htmlTemplate(text_ = var) :
myvar ="<h3>Test Title</h3>"
htmlTemplate(text_ = var)
This will be displayed as raw HTML in the console, but on a Jupyter Notebook it will be displayed formatted. I didn't Try it on RStudio but I would expect it to be displayed correctly as well (anyone to confirm ?)
Another solution to display the html content (as mentionned by #krassowski) Nevertheless this works only in Jupyter Notebook.
# We still need to call `HTML()` function inside
IRdisplay::display_html( HTML(myvar) )
How to pretty print citations for loaded packages in R
My final goal was to display the citations for loaded R packages in HTML
Using the .packages() I could retrieve the list of attached packages.
Then I loop on the list and for each package name (str) I can call citation(package) on the top. To get it as html I only found the print(citation(package), style='html').
Then we need to capture the output of this into a variable. The function capture.output(func) do the trick. See below:
library('htmltools')
# Create a function to output the html of all packages
list_pkgs_html = function(){
# Remove warning for the time of the function
w = getOption("warn")
options(warn = -1)
# For each package loaded
for (p in .packages()){
# Catch error in case there is no citation for one of them.
tryCatch({
# Display the name of the package as a title h3
cat("<h3>", p, '</h3>\n')
# Print the citation in html style
print(citation(p), style = "html")
})
}
# Restore the warning as before
options(warn = w)
}
# Call the function and capture the output to a variable.
capture.output(list_pkgs_html()) -> myvar
# Display the html result.
htmlTemplate(text_ = myvar)
Output:

kableExtra save equation to png or jpg

I'm trying to save a kableExtra table to a png that contains an equation.
A really simple example:
tab <- kable("$a^2$")
tab
which gives me the table I'm looking for:
Now I want to save this to a png or jpg file with save_kable
save_kable(tab, file = "test.jpg")
which then returns this:
I have already tried changing the escape argument but the result remains the same
tab <- kable("$a^2$", escape = TRUE)
tab <- kable("$a^2$", escape = FALSE)
Any ideas how to make sure the png or jpg file also renders the equation/mathsymbols?
Thanks!
I propose one solution based on two other packages:
library(knitr)
library(gridExtra) # to display a table
library(latex2exp) # for TeX function which transform $$ to expression
png("test.png")
grid.table(TeX(kable("$a^2$", format = "simple")))
dev.off()
escape will work only for colnames in kable().

Print kable table to .tex file

I have a table generated in kable that I want to print to a .tex file, in the same way I would use print.xtable.
that is,
print(xtable(data.frame, xtable.options), print.options, file = filename.tex))
When I tacked print onto a kable table generation, it doesn't work. It seems to be ignored.
I am using:
kable(df, format = 'latex', kable.options) %>% print(file = filename.tex)
Note, the pipe operator %>% should drop the preceding into the first argument of the following function. I do not see a print.kable function available. Is there some other function I am missing?
I am using kable to allow for grouping columns; something xtable by default doesn't appear to allow.
You can use cat -
cat(kable(head(mtcars), format = 'latex'), file = 'file.tex')

Output from `kable()` combined with text as character vector and print table

I have the following RMarkdown Document (also here as a gist)
results in this
I would like to use the function fun() to create a proper table as from the first code chunk.
I thought that format = "markdown and results = "asis" should do the job, but apparently not.
What Am I missing here?

Add native knitr print methods

I'm looking for a way to adjust functions which usually show html tables in the viewer or browser in a way that they automatically insert the html in knitr documents like rnotebooks when called.
Specifically I want to change the way functions in the package sjPlot behave. At the moment they return an object with a $knitr attribute that has to be inserted manually and then produce an html table in the viewer:
`r sjt.frq(efc$e42dep, no.output=TRUE)$knitr`
One of these functions is sjt.frq. Is there a way to do this?
From knit_print:
library(knitr)
a = 42
class(a) = "my_class"
knit_print.my_class = function(x,...){
res = paste("{{", x,"}}")
asis_output(res)
}
a
It works when I knit document but doesn't work with RNotebooks and doesn't work with sjt.frq. However with RNotebook works the following statement:
library(sjPlot)
library(sjmisc)
library(knitr)
data(efc)
asis_output(sjt.frq(efc$e42dep, no.output=TRUE)$knitr)

Resources