5

我有一个表格显示在一个闪亮的应用程序中。我想根据值格式化表格并相应地着色。我已经看到了可格式化区域着色,它根据值的范围定义了中断,然后生成了应用于表格的颜色渐变。我想要做的是允许用户填写最小值和最大值,并根据它对表中的值进行着色。因此,如果值的范围是 1-20 并且如果用户输入是 5 和 15 ,则低于 5 和高于 15 的值不应应用任何颜色渐变。下面是我目前如何使用可格式化区域格式的代码。

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

1 回答 1

2

这是您的工作代码。

您必须使用该input最小值和最大值作为序列的限制(我只是将其更改为范围 - 用户更容易放置这样的范围)然后您生成序列 - 根据您的符号 -brks()在我的情况下我使用length.out10 但您可以根据需要或动态放置尽可能多的休息时间。然后生成

颜色数 - 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)
于 2018-09-03T15:32:35.370 回答