一个 tabPanel 中的两个 rhandsontables 在闪亮的应用程序中
Two rhandsontables in one tabPanel in shiny app
在闪亮的应用程序中将两个 rhandsontables
合二为一 tabPanel
时,迷你图会混淆。第一个 table 的迷你图显示正确,而第二个 table 包含与第一个 table 相同的迷你图,反之亦然。但是,如果 "plotted" 在不同的 tabPanel 中,它们就可以了。
有没有办法将两个 rhandsontables
合二为一 tabPanel
?
这是我的代码:
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)
})
}
)
)
感谢任何帮助,谢谢!
这是一个有点古怪和肮脏的解决方案——我相信有一个正确的方法来做!但问题似乎出现了,因为 span class
在两个 table 中指向相同的名称(例如 span class="sparklines_r0_c3"
)。因此,一种解决方法是让迷你图出现在每个 table 的不同列中。我在第一个数据框中添加了一个 NA
列:
dat1$x <- NA
然后在第一个 table:
中插入一个 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)
})
输出:
在闪亮的应用程序中将两个 rhandsontables
合二为一 tabPanel
时,迷你图会混淆。第一个 table 的迷你图显示正确,而第二个 table 包含与第一个 table 相同的迷你图,反之亦然。但是,如果 "plotted" 在不同的 tabPanel 中,它们就可以了。
有没有办法将两个 rhandsontables
合二为一 tabPanel
?
这是我的代码:
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)
})
}
)
)
感谢任何帮助,谢谢!
这是一个有点古怪和肮脏的解决方案——我相信有一个正确的方法来做!但问题似乎出现了,因为 span class
在两个 table 中指向相同的名称(例如 span class="sparklines_r0_c3"
)。因此,一种解决方法是让迷你图出现在每个 table 的不同列中。我在第一个数据框中添加了一个 NA
列:
dat1$x <- NA
然后在第一个 table:
中插入一个 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)
})
输出: