简体   繁体   English

根据用户输入格式化R shine中的表格输出

[英]Format table output in R shiny based on user inputs

I have a table being display in a shiny app. 我有一张表在一个闪亮的应用程序中显示。 I want to format the tables based on the values and color it accordingly. 我想根据值格式化表格并相应地对其进行着色。 I have seen the formattable area coloring where based on the range of the values it defines the breaks and then color gradients are generated which are applied to the table. 我已经看到了格式化区域着色,其中基于它定义断点的值的范围,然后生成应用于表格的颜色渐变。 What I want to do is allow the user to fill the min and max value and depending on it the values in the table will be colored. 我想要做的是允许用户填充最小值和最大值,并根据它将表中的值着色。 So if the values range from 1-20 and if the user inputs are 5 and 15 , values below 5 and above 15 shouldnt have any color gradients applied to them. 因此,如果值的范围为1-20,并且如果用户输入为5和15,则低于5且高于15的值不应对其应用任何颜色渐变。 Below is the code of how I am doing currently using 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)

Here is your working code. 这是你的工作代码。

You must use that input minimal and maximal value as limits for your sequence (I just change it to range - is easier for user to put a range like that) Then you generate sequence - according your notation - brks() - in my case I use length.out of 10 but you can put as many breaks as you want or dynamically. 您必须使用input最小值和最大值作为序列的限制(我只需将其更改为范围 - 用户更容易放置类似的范围)然后生成序列 - 根据您的符号 - brks() - 在我的情况下我使用length.out of 10但你可以根据需要或动态放置多个中断。 Then generate on 然后生成

number of colors - 1 颜色数量 - 1

and in the end in styleInterval() for background add limits of white - or any other color you want. 并最终在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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM