Search code examples
cssrr-markdownflexdashboardrpivottable

Scroll long pivot table (package rpivottable and knitr)


I would like to generate a pivot table from rpivotTable library with a vertical scrollbar to allow viewing long outputs.

The pivot table is generated with knitr in RStudio and is embedded in a flexdashboard template.

I am not using shiny dashboard, for which the issue has already been cleared, mine is just a html dashboard generated with knitr.

Before you give me a minus... I really tried, thinking that maybe a setting is undocumented: my part of the Rmd code looks as follows (scroll in both cases brings no result):

---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(rpivotTable)

state_table <- data.frame(key=c("CA", "NY", "WA", "ON", "QU"), 
                          name=c("California", "new York", "Washington", "Ontario", "Quebec"), 
                          country=c("USA", "USA", "USA", "Canada", "Canada")) 

month_table <- data.frame(key=1:12, 
                          desc=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"), 
                          quarter=c("Q1","Q1","Q1","Q2","Q2","Q2","Q3","Q3","Q3","Q4","Q4","Q4")) 

prod_table <- data.frame(key=c("Printer", "Tablet", "Laptop"), 
                         price=c(225, 570, 1120)) 

# Function to generate the Sales table 

gen_sales <- function(no_of_recs) { # Generate transaction data randomly 
                      loc <- sample(state_table$key, no_of_recs, replace=T, prob=c(2,2,1,1,1)) 
                      time_month <- sample(month_table$key, no_of_recs, replace=T) 
                      time_year <- sample(c(2012, 2013), no_of_recs, replace=T) 
                      prod <- sample(prod_table$key, no_of_recs, replace=T, prob=c(1, 3, 2)) 
                      unit <- sample(c(1,2), no_of_recs, replace=T, prob=c(10, 3)) 
                      amount <- unit*prod_table[prod,]$price 

                      sales <- data.frame(month=time_month, 
                                          year=time_year, 
                                          loc=loc, 
                                          prod=prod, 
                                          unit=unit, 
                                          amount=amount) 

                      # Sort the records by time order 

                      sales <- sales[order(sales$year, sales$month),] 

                      row.names(sales) <- NULL 
                      return(sales) 
} 

# Now create the sales fact table 

sales_fact <- gen_sales(100000) 


```

Column {data-width=650}
-----------------------------------------------------------------------

### Table

```{r}
library(rpivotTable)
rpivotTable(
sales_fact,
rows = c("year","month"),
cols = c("prod"),
aggregatorName = "Sum",
vals = "amount",
rendererName = "Heatmap",
height = "600px",
overflow = "scroll")

```

Column {data-width=350}
-----------------------------------------------------------------------

### Chart B

```{r}

```

### Chart C

```{r}

```

I will be very thankful for any indication how to achieve vertical scrolling of rpivottable.


Solution

  • I found the answer that works here: rpivotTable not fitting in my Shiny Dashboard page

    In my Rmd file I need in the header a link to a css file:

    ---
    title: "Untitled"
    output: 
      flexdashboard::flex_dashboard:
        css: styles.css
        orientation: columns
        vertical_layout: fill
    ---
    

    And a styles. css file shall contain

    .rpivotTable{ overflow-x: scroll; }
    

    It works like a charm.