我正在尝试根据前面表格中的值来格式化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
所需输出如图所示,在其中我也想根据参考数据框中的值对文本和背景进行着色,如下图。