【发布时间】:2017-07-22 23:57:28
【问题描述】:
我正在尝试根据上表中的值格式化 DT。 例如,我想显示某事 增加、减少或保持不变。 我可以用 kable 做到这一点,但是 无法进行下一步,我想单击单元格并显示所有相关数据 到另一个 DT 中的那个值。
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
mainPanel(
dataTableOutput("iris_head")
)
)
server <- function(input, output) {
#df_data <- iris
df_data <- head(iris[-5])
# Just a dataset describing if iris has changed over a month
# If reference data is of the same size as the original data (df_data).
# If reference data is negative I want the cell in the df_data to be green;
# If zero blue and if positive then green.
# I can make changes with ranges within the current range, can we get the color encoding from another table?
# set the seed
set.seed(42)
reference_df <- (sapply(df_data, function(x) jitter(x, amount = 2)) - df_data) %>%
round(. , digits = 0)
print(reference_df)
output$iris_head <- renderDataTable(datatable(df_data, selection = "single" )%>%
formatStyle('Sepal.Width',
color = styleInterval(c(3.4, 3.8), c('green', 'blue', 'red')),
backgroundColor = styleInterval(3.4, c('gray', 'yellow'))) %>%
formatString('Sepal.Width', suffix = '<font color="red">⇑ </font>'))
}
shinyApp(ui = ui, server = server)
本例中的reference_df 是:
Sepal.Length Sepal.Width Petal.Length Petal.Width
2 1 2 0
2 -1 -1 0
-1 1 0 2
1 1 2 -1
1 0 2 2
0 1 -2 2
所需的输出如图所示,我还想根据 reference_df 中的值对文本和背景进行着色(如果可能)。
【问题讨论】: