Thomas
Thomas

Reputation: 1454

Two rhandsontables in one tabPanel in shiny app

When putting two rhandsontables in one tabPanel within a shiny app, the sparklines get mixed up. Either the sparklines of the first table are displayed correctly while the second table contains the same sparklines as the first table or vice versa ca. However, if "plotted" in different tabPanels, they are just fine.

Is there a way to combine two rhandsontables in one tabPanel?

Here is my code:

library(shiny)
library(dplyr)
library(rhandsontable)

#example data set1
dat1 <- data.frame(a=sample(1:10, 10), b=sample(1:10, 10), c=sample(1:10, 10),  d=sample(1:10, 10),  e=sample(1:10, 10))
dat1$a1 <- sapply(1:nrow(dat1), function(x) jsonlite::toJSON(list(values=c(as.numeric(dat1$a[x]), as.numeric(dat1$b[x]), as.numeric(dat1$c[x]), as.numeric(0)), options = list(type = "line"))))

dat1$a2 <- sapply(1:nrow(dat1), function(x) jsonlite::toJSON(list(values=c(as.numeric(dat1$d[x]), as.numeric(dat1$e[x]), as.numeric(0)), options = list(type = "line"))))

#example data set1
dat2 <- data.frame(a=sample(1:10, 10), b=sample(1:10, 10), c=sample(1:10, 10),  d=sample(1:10, 10),  e=sample(1:10, 10))
dat2$a1 <- sapply(1:nrow(dat2), 
              function(x) jsonlite::toJSON(list(values=c(as.numeric(dat2$a[x]), as.numeric(dat2$b[x]), as.numeric(dat2$c[x]), as.numeric(0)), options = list(type = "bar"))))
dat2$a2 <- sapply(1:nrow(dat2),
              function(x) jsonlite::toJSON(list(values=c(as.numeric(dat2$d[x]), as.numeric(dat2$e[x]), as.numeric(0)), options = list(type = "bar"))))



runApp(launch.browser = TRUE, 
   list(ui=shinyUI(fluidPage(

     titlePanel("MYTITLE"),
     mainPanel(
       tabPanel("A",
                rHandsontableOutput("first"),
                br(),
                rHandsontableOutput("second"))

     )
   )),
     server = function(input, output) {

       output$first <- renderRHandsontable({
       dat1 %>%
         select(a, b, c, a1, d, e, a2)%>%
         rhandsontable(readOnly = TRUE, width = 800, 
                       allowedTags = "<em><b><span><strong><a><big>") %>%
         hot_cols(colWidths = c(50, 50, 50,80, 50, 50, 80)) %>%
         hot_col("a", format = "0")%>%
         hot_col("b", format = "0") %>%
         hot_col("c", format = "0") %>%
         hot_col("d", format = "0") %>%
         hot_col("e", format = "0") %>%
         hot_col("a1", renderer = htmlwidgets::JS("renderSparkline")) %>%
         hot_col("a2", renderer = htmlwidgets::JS("renderSparkline")) %>%
         hot_table(highlightCol = TRUE, highlightRow = TRUE) 
       })

       output$second <- renderRHandsontable({
         dat2 %>%
           select(a, b, c, a1, d, e, a2)%>%
           rhandsontable(readOnly = TRUE, width = 800, 
                         allowedTags = "<em><b><span><strong><a><big>") %>%
           hot_cols(colWidths = c(50, 50, 50,80, 50, 50, 80)) %>%
           hot_col("a", format = "0")%>%
           hot_col("b", format = "0") %>%
           hot_col("c", format = "0") %>%
           hot_col("d", format = "0") %>%
           hot_col("e", format = "0") %>%
           hot_col("a1", renderer = htmlwidgets::JS("renderSparkline")) %>%
           hot_col("a2", renderer = htmlwidgets::JS("renderSparkline")) %>%
           hot_table(highlightCol = TRUE, highlightRow = TRUE) 
       })

     }
   )
)

Any help is appreciated, thanks!

Upvotes: 0

Views: 423

Answers (1)

Weihuang Wong
Weihuang Wong

Reputation: 13128

This is a bit of a hacky and dirty solution -- and I'm sure there is a proper way of doing it! But the problem appears to arise because span class points to the same names in both tables (e.g. span class="sparklines_r0_c3"). So one workaround is to make the sparklines go in different columns in each table. I add an NA column to the first dataframe:

dat1$x <- NA

then insert a blank 1-pixel wide column in the first table:

output$first <- renderRHandsontable({
dat1 %>%
 select(x, a, b, c, a1, c, d, e, a2)%>%
 rhandsontable(readOnly = TRUE, width = 801, 
               allowedTags = "<em><b><span><strong><a><big>") %>%
 hot_cols(colWidths = c(1, 50, 50, 50,80, 50, 50, 80)) %>%
 hot_col("a", format = "0")%>%
 hot_col("b", format = "0") %>%
 hot_col("c", format = "0") %>%
 hot_col("d", format = "0") %>%
 hot_col("e", format = "0") %>%
 hot_col("a1", renderer = htmlwidgets::JS("renderSparkline")) %>%
 hot_col("a2", renderer = htmlwidgets::JS("renderSparkline")) %>%
 hot_table(highlightCol = TRUE, highlightRow = TRUE) 
})

Output:

enter image description here

Upvotes: 1

Related Questions