I'm working with RMarkdown and created a report using the gt package. Before I knit the document, the graph output shows 2 parts: "R Console" and "shiny.tag.list". While the "shiny.tag.list" has the table, R Console is what appears after i knit the document, "no non-missing arguments to min; returning Infno non-missing arguments to max; returning -Inf". How do I get it to show the table? Is there a way to address the R Console comments so it won't appear?
PS. Knit output is in Microsoft Word.
gt_tbl <-
df_tbl %>%
gt(rowname_col="date")
# removes date so you can add a line alignment for that column
gt_tbl <-
gt_tbl %>%
tab_stubhead(label="date")
# adds the line alignment + new label for column you just took out name for
gt_tbl <-
gt_tbl %>%
tab_header(
title=md("**Weekly Screener Report**"),
subtitle= {current_range}) %>%
tab_style(
style=list(
cell_fill(color="light blue"),
cell_text(weight="bold")
),
locations = cells_body(
columns=vars(week_total)
)
)
# md("**text**") bolds "text"
# tab_style() adds color where you want it, can be done to rows as well
# tab_header is for title and subtitle
# Create 2 row groups w/ 'tab_row_group()' function
# Group the rows in opposite order you want to see it in
gt_tbl <-
gt_tbl %>%
tab_row_group(
group="current",
rows= 5:8
) %>%
tab_row_group(
group="former",
rows= 1:4
)
# view table
gt_tbl
before_knit_rconsole
before_knit_plot
after_knit_output
I can reproduce the error.
But first, what you see are in your output are warnings. You can get rid of these by setting the header of the chunk as follows:
{r, warning=FALSE}
A reproducible example:
---
title: "test"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(gt)
```
```{r, warning=FALSE}
gt_tbl <-
gt::gtcars
gt_tbl %>% gt(rowname_col = "mfr")
```
When I run this code, I don't get the table printed out in the .docx. If I change to pdf_document the table appears.
A quick search indicated that word outputs are not supported (https://www.danieldsjoberg.com/gtsummary/articles/rmarkdown.html). But I might be wrong.
Related
I'm using checkbox filters from crosstalk to filter entries in a table made with reactable.
I need a simple way to get the checkboxes to show the number of corresponding entries next to them.
Here's a minimal example of a table with checkbox filters:
Example.Rmd
---
title: "Filtering with crosstalk"
---
```{r}
library(reactable)
library(crosstalk)
data <- SharedData$new(iris)
filter_checkbox("species", "Species", data, ~Species)
reactable(data)
```
It spits out this:
I need the checkboxes to show the number of corresponding entries next to them, like this:
What is the simplest way to do this?
You can create the desired category label as a column to the dataset itself, then pass the data to SharedData$new() and create filter_checkbox based on that newly created column. Then hide this newly created column in reactable using colDef(show = FALSE).
---
title: "Filtering with crosstalk"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, message=FALSE)
```
```{r}
# preparing the label into the data itself
library(dplyr)
iris_df <- iris %>%
group_by(Species) %>%
mutate(
Species_label = paste0(Species, " (",n(), ")")
) %>% ungroup()
```
```{r}
library(reactable)
library(crosstalk)
data <- SharedData$new(iris_df)
filter_checkbox("species", "Species", data, ~Species_label)
reactable(data,
columns = list(
Species_label = colDef(show = FALSE)
))
```
I'm trying to generate a report with each row of a tibble printing vertically in a table on its own page using a for loop. The tables are printing fine, but I want to put headers at the top of each page (and eventually, text). Since the line of code to print the header (which appears as a header in my own document but not in my reprex, not sure why) is above the line of code to print the table, using kable, I expect the header to print above the table. I suspect kable is doing something I don't understand behind the scenes but I cannot determine what.
What am I doing wrong, and how may I print headers at the top of each page?
I've provided below: a screenshot of the current output.
Relevant portion:
---
title: "kable-order-reprex"
author: "Rob Creel"
date: "6/4/2021"
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(magrittr)
library(kableExtra)
```
```{r chunk1, echo=FALSE, results='asis'}
for (i in 1:nrow(mtcars)) {
# Generate Table
mtcars %>%
slice(1) %>%
stack() %>%
select(ind, values) %>%
kable(col.names = NULL) %>%
kable_styling() -> my_table
cat("\n\n\\pagebreak\n")
# Print Header
cat(paste0("## ", mtcars %>% rownames() %>% extract2(i)))
# Print Table
print(my_table)
cat("\n\n\\pagebreak\n")
}
```
Pic of mis-ordered output.:
As far as I can tell everything is working as intented. The problem is float management in LaTeX.
You can change the behaviour of the kable output by setting the argument latex_options in kable_styling(). E.g.
mtcars %>%
slice(1) %>%
stack() %>%
select(ind, values) %>%
kable(col.names = NULL) %>%
#Use either "hold_position" or "HOLD_position"
kable_styling(latex_options = "hold_position") -> my_table
hold_position corresponds to h, while HOLD_position is H, (read more about float options here), where H is stricter than h and forces the position of the float output to be at the position where it is in the code. Also see ?kable_styling (latex_options entry)
See similar question with answer here
Context: I am knitting an Rmarkdown report mixing landscape and portrait layouts. I use the technique described here: https://stackoverflow.com/a/41945462/10264278.
Problem: there is an unexpected table layout problem. when the table is made using kableExtra::kable() it does not follow the expected orientationif a caption is filled in kable(caption =...).
Reproducible example:
---
output: pdf_document
header-includes:
- \usepackage{pdflscape}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=FALSE, warning=FALSE, comment=FALSE, message=FALSE)
library(dplyr)
library(kableExtra)
```
\blandscape
```{r}
mtcars # displays properly
mtcars %>% kable(caption = "test", digits = 2) # the table follows the landscape layout and does not remains "still"/"horizontal"
```
\elandscape
The output:
Table on the left (mtcars) is displayed as expected. But, if I use kable() the table "turns with the page" witch is not expected.
EDIT:
I am aware that kableExtra::landscape() exists. But this function creates a new page for each table. Here I have many few-rows tables that are very wide. Thus I do not want to have a single table per page.
Example:
```{r}
head(mtcars, 5) %>% kable(caption = "test", digits = 2) %>% landscape(margin = NULL)
head(mtcars, 5) %>% kable(caption = "test", digits = 2) %>% landscape(margin = NULL)
```
Output:
EDIT 2:
This problem occurs only if a caption is specified in kable(caption = ...).
Example:
\blandscape
```{r}
mtcars # displays properly
mtcars %>% kable(digits = 2) # the table follows the landscape layout and does not remains "still"/"horizontal"
```
\elandscape
Output:
When using rmarkdown to render pdf document, we can use three options for printing data.frame: default, kable and tibble (see here)
With the default option, it is possible to limit the number of rows printed, with the option: max.print
For tibble, we can use: dplyr.print_max
I can't find a way to limit the number of rows for kable. Is it possible?
kable renders the full data frame passed to it as a table in the output document. AFAIK there's no argument that will limit the number of rows. However, you can preselect the number of rows in the output table (for example, kable(head(dat)) or kable(dat[1:5, ])). If you want to avoid having to select the rows each time, you could write a helper function to limit the number of rows printed. For example:
---
output: pdf_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(knitr)
```
```{r}
my_kable = function(x, max.rows=6, ...) {
kable(x[1:max.rows, ], ...)
}
```
```{r}
my_kable(mtcars, caption="My first caption")
```
```{r}
iris$Sepal.Length = 1000 * iris$Sepal.Length
my_kable(iris, 3, caption="My second caption", format.args=list(big.mark=","))
```
A really simple solution is to wrap kable around head:
kable(head(mtcars, n = 5))
You can create a custom method for printing data frames like this.
Adapted from Yihui Xie's explanation here
---
title: "Untitled"
output: html_document
---
```{r include=FALSE}
knit_print.data.frame <- function(x, ...) {
head(x, 5) |>
knitr::kable() |>
paste(collapse = "\n") |>
knitr::asis_output()
}
registerS3method(
genname = "knit_print",
class = "data.frame",
method = knit_print.data.frame,
envir = asNamespace("knitr")
)
```
```{r}
iris
tibble::as_tibble(iris)
```
I have a problem with a RMD script. It runs. Once. After that, I get partial results.
The code:
---
title: "Dashboard"
output:
flexdashboard::flex_dashboard:
runtime: shiny
---
```{r global, include=FALSE}
library(dplyr)
library(flexdashboard)
Name=c('Test1', 'Test1', 'Test2')
Number = c(8, 9, 7)
zt <- data.frame(Name, Number)
PersonList <- sort(unique(zt$Name))
```
Selections {.sidebar}
===============================
```{r}
## The shiny part
selectInput("PersonSel", "Person: ", PersonList, selected = 'Test1')
```
Tab 1
======================================================================
Row {.tabset}
-----------------------------------------------------------------------
### Table
```{r}
renderTable({
cqi <- zt %>%
na.omit() %>%
filter(Name %in% input$PersonSel) %>%
group_by(Number) %>%
summarise(Count = n())
}, align='l')
```
Result:
When I run the script again, I get this:
In R Studio I see this:
But only this - it doesn't continue.
The only way to get it working again is to edit the script. Add a space or whatever.
Is seems very strange behavior. What can be the problem (and solution) here?