Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 516
Jacek Kotowski Avatar asked Dec 21 '25 16:12

Jacek Kotowski


2 Answers

This is an easier alternative:

  1. Leave the top portion as this:

    ---
    title: "Untitled"
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    ---
    
  2. Then simply add a CSS chunk after your pivot table chunk, like this:

    .rpivotTable{ overflow : scroll; }
    
  3. This header of the chunk can look like this:

    {css, Pivot Table CSS, echo = FALSE}

Hope this helps!

like image 52
Lahi S Avatar answered Dec 24 '25 10:12

Lahi S


your code is not actually reproducible because we don't have "test2". im trying some generic datasets, but they're not even appearing in the table.

like image 27
Bruna Wundervald Avatar answered Dec 24 '25 09:12

Bruna Wundervald



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!