Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用formatStyle设置条件变量列格式_R_Shiny - Fatal编程技术网

使用formatStyle设置条件变量列格式

使用formatStyle设置条件变量列格式,r,shiny,R,Shiny,我有一个数据帧列表,每个数据帧都有一个共同的列名,但其他列可能不同。我的目标是格式化除公共列之外的所有列,因为每个df都显示在一个闪亮的应用程序中。 这就是我所做的: library(shiny) library(DT) library(dplyr) set.seed(101) df1 <- data.frame(id = runif(10), col1 = rnorm(10), col2 = rnorm(10)) df2 <- data.frame(id = runif(10)

我有一个数据帧列表,每个数据帧都有一个共同的列名,但其他列可能不同。我的目标是格式化除公共列之外的所有列,因为每个df都显示在一个闪亮的应用程序中。 这就是我所做的:

library(shiny)
library(DT)
library(dplyr)

set.seed(101)

df1 <- data.frame(id = runif(10), col1 = rnorm(10), col2 = rnorm(10))
df2 <- data.frame(id = runif(10), col1 = rnorm(10), col3 = rnorm(10))
df3 <- data.frame(id = runif(10), col3 = rnorm(10), col4 = rnorm(10))

df <- list(data1 = df1, data2 = df2, data3 = df3)

ui <- fluidPage(
  fluidRow(
    column(3, selectInput('select', 'Choose dataframe: ', choices = c('data1', 'data2', 'data3'),
                          selected = 1)),
    column(9, DTOutput('table'))
  )
)

server <- function(input, output) {

  selected <- reactive({
    input$select
  })

  col_names <- c('col1', 'col2', 'col3', 'col4')

  output$table <- renderDT(
    df[[selected()]] %>% formatStyle(names(.)[names(.) %in% col_names], backgroundColor = 'yellow'), 
    options = list(pageLength = 15))

}

shinyApp(ui=ui,server=server)

库(闪亮)
图书馆(DT)
图书馆(dplyr)
种子集(101)

df1考虑将其更改为
datatable

server <- function(input, output) {

  selected <- reactive({
  input$select
  })

  col_names <- c('col1', 'col2', 'col3', 'col4')

  output$table <- DT::renderDT(

    {dat <- df[[selected()]]
    nm1 <- intersect(names(dat), col_names)

    DT::datatable(dat, options = list(pageLength = 15))   %>%
        formatStyle(nm1, backgroundColor = 'yellow')
    }


  )

}

shinyApp(ui=ui,server=server)

server考虑将其更改为
datatable

server <- function(input, output) {

  selected <- reactive({
  input$select
  })

  col_names <- c('col1', 'col2', 'col3', 'col4')

  output$table <- DT::renderDT(

    {dat <- df[[selected()]]
    nm1 <- intersect(names(dat), col_names)

    DT::datatable(dat, options = list(pageLength = 15))   %>%
        formatStyle(nm1, backgroundColor = 'yellow')
    }


  )

}

shinyApp(ui=ui,server=server)

服务器可能用
req
包装请详细说明好吗?可能用
req
包装请详细说明好吗?非常感谢!我以前没有意识到这一点,因为我添加了一个DataExplorer::drop_columns(),它呈现了一个dataframe而不是datatable。非常感谢!我以前没有意识到这一点,因为我添加了一个DataExplorer::drop_columns(),它呈现了一个dataframe而不是datatable。