2016-08-25 35 views
0

把两个rhandsontables当一个tabPanel一个闪亮的应用程序内的迷你搞混。所述第一表的火花线被正确地显示,而第二表包含相同的火花线作为第一表或反之亦然约但是,如果在不同tabPanel中“绘制”,它们就好了。两个rhandsontables于一体的TabPanel闪亮的应用

有没有办法在一个tabPanel结合两个rhandsontables

这里是我的代码:

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) 
     }) 

    } 
    ) 
) 

任何帮助表示感谢,谢谢!

回答

1

这是一个有点哈克和肮脏的解决方案 - 我敢肯定有做这件事的正确方法!但问题似乎出现因为span class分两个表(例如span class="sparklines_r0_c3")在相同的名称。所以一个解决方法是让每个表中的不同列中的迷你曲线走到一起。我的NA列添加到第一数据帧:

dat1$x <- NA 

然后插入在所述第一表中的坯件1像素宽的柱:

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) 
}) 

输出:

enter image description here

+0

保存我的天。 thx为黑客! – Thomas

相关问题