根据用户输入,在R Shiny中格式化表格输出

5
我有一个表格在shiny app中显示。我想根据值格式化表格并相应地着色。我已经看到了形式可区域着色的方式,它根据值的范围定义断点,然后生成颜色渐变,并应用于表格。我的目标是允许用户填写最小和最大值,根据此,将对表格中的值进行着色。因此,如果值的范围为1-20,并且如果用户输入的值为5和15,则在5以下和15以上的值不应该应用任何颜色渐变。以下是我目前使用的formatable area formatting的代码。
library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(DT)

sidebar <- dashboardSidebar(
  sidebarMenu(id = "tab",
              menuItem("1", tabName = "1")
  )
)
body <-   ## Body content
  dashboardBody(box(width = 12,fluidRow(
    fluidRow(  column(
      width = 3,  textInput("text1", label = h5("Min"), value = "Enter min")),
      column(
        width = 3, textInput("text2", label = h5("Max"), value = "Enter max"))),
    DT::dataTableOutput("op")
  )))

ui <-   dashboardPage(dashboardHeader(title = "Scorecard"),
                      sidebar,
                      body)

# Define the server code
server <- function(input, output,session) {
  df <- data.frame(month = c("mazda 3", "mazda cx5", "mazda 6","mazda miata","honda civic","honda accord"),
                   april = c(.1,.2,.3,.3,.4,.5),
                   may = c(.3,.4,.5,.2,.1,.5),
                   june = c(.2,.1,.5,.1,.2,.3))

  brks <- reactive({ quantile(df$april, probs = seq(.05, .95, .05), na.rm = TRUE)})
  clrs <- reactive({ round(seq(255, 175, length.out = length(brks()) + 1), 0) %>%
  {paste0("rgb(",.,",", ., ",255 )")}})

  df_format<- reactive ({datatable(df,options = list(searching = FALSE,pageLength = 15, lengthChange = FALSE))%>%
           formatStyle(names(df),backgroundColor = styleInterval(brks(), clrs()))})

  output$op <-renderDataTable({
    df_format()
  })

}

shinyApp(ui = ui, server = server)

最低分位数已经没有颜色(白色),你想如何处理? - pogibas
如果用户选择5-15,则颜色可以是从绿色到红色的渐变。因此,5为绿色,15为红色。 - SNT
但是您提供的示例从白色到蓝色。 - pogibas
我之前在另一个示例中使用了这个颜色范围,那时我没有考虑用户输入。 - SNT
1个回答

2
这是你的工作代码。
你必须使用该输入的最小值和最大值作为序列的限制(我只是将其更改为范围 - 这样用户更容易放置这样的范围),然后根据你的符号生成序列 - brks() - 在我的情况下,我使用了长度为10的length.out,但你可以放置任意多的断点或者动态生成。接着生成

颜色数量-1

最后在styleInterval()中添加white的限制作为背景 - 或者任何其他你想要的颜色。
library(shiny)
library(shinyWidgets)
library(shinydashboard)
library(DT)

sidebar <- dashboardSidebar(
   sidebarMenu(id = "tab",
            menuItem("1", tabName = "1")
   )
)
body <-   ## Body content
   dashboardBody(box(width = 12,fluidRow(
      fluidRow(column(
                width = 3, 
                sliderInput("range_value", 
                            label = h3("Put a range value"), 
                            min = 0, 
                            max = 100, 
                            value = c(5, 15)
                            )
                    )
             ),
    DT::dataTableOutput("op")
)))

ui <-   dashboardPage(dashboardHeader(title = "Scorecard"),
                      sidebar,
                      body)

# Define the server code
server <- function(input, output,session) {
df <- data.frame(month = c("mazda 3", "mazda cx5", "mazda 6","mazda miata","honda 
                 civic","honda accord"),
                 april = c(9, 8, 11,14,16,1),
                 may = c(3,4,15,12,11, 19),
                 june = c(2,11,9,7,14,1))
brks <- reactive({
    seq(input$range_value[1], input$range_value[2], length.out = 10) 
})

clrs <- reactive({ round(seq(255, 175, length.out = length(brks()) - 1), 0) %>%
{paste0("rgb(",.,",", ., ",255)")}})

df_format<- reactive ({datatable(df,options = list(searching = FALSE, pageLength = 15, lengthChange = FALSE)) %>%
            formatStyle(names(df), 
                        backgroundColor = styleInterval(c(brks()), c('white', clrs() ,'white'))
                        )
    })

output$op <-renderDataTable({
    df_format()
  })
}

shinyApp(ui = ui, server = server)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接