Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.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
R 从反应表中访问列名,以便使用DT包进行格式化_R_Shiny_Dt - Fatal编程技术网

R 从反应表中访问列名,以便使用DT包进行格式化

R 从反应表中访问列名,以便使用DT包进行格式化,r,shiny,dt,R,Shiny,Dt,我试图在一个闪亮的应用程序中格式化一个反应表,并用DT包显示它。关于如何使用非反应性表(如:。但是,当我尝试以相同的方式使用被动函数的输出时,我会得到一个列名称错误。以下是一个例子: library(shiny) library(DT) ui <- fluidPage( fluidRow( column(3,numericInput("nrows","Enter the number of rows",value=10)), column(9,DT::dataTab

我试图在一个闪亮的应用程序中格式化一个反应表,并用DT包显示它。关于如何使用非反应性表(如:。但是,当我尝试以相同的方式使用被动函数的输出时,我会得到一个列名称错误。以下是一个例子:

library(shiny)
library(DT)


ui <- fluidPage(
  fluidRow(
    column(3,numericInput("nrows","Enter the number of rows",value=10)),
    column(9,DT::dataTableOutput("table"))
  )
)
server <- function(input, output, session) {

  cars2 <- reactive({
    cars <- datasets::cars
    cars2 <- cars[1:input$nrows,]
    return(cars2)
  })

  output$table <- DT::renderDataTable(cars2()%>% formatStyle( 'speed',backgroundColor = c('yellow')),options=list(pageLength=50))


}


shinyApp(ui=ui,server=server)
这项工作:

server <- function(input, output, session) {

  cars2 <- reactive({

    cars <- datasets::cars
    cars2 <- cars[1:input$nrows,]
    return(cars2)

  })

  output$table <- renderDataTable({

    datatable(cars2(), options = list(pageLength = 50)) %>%
      formatStyle('speed', backgroundColor = 'yellow')

  })

}

服务器谢谢!在我的实际应用程序中,我需要将反应表分开,以便以后使用。我可以在示例中使用它,但我只是在我的应用程序中尝试了这个,我得到了相同的列名错误。关于可能发生的其他事情有什么想法吗?更新了我上面的代码以合并您的评论。@user29609-重新思考在应用程序开始时加载
汽车
。谢谢,我重新启动了R,这个示例现在可以在我的实际应用程序中使用了!被动变量需要作为函数调用。例如,可以使用
cars2()
访问
cars2
。类似地,可以使用
cars2()$speed
等访问其列。
server <- function(input, output, session) {

  cars2 <- reactive({

    cars <- datasets::cars
    cars2 <- cars[1:input$nrows,]
    return(cars2)

  })

  output$table <- renderDataTable({

    datatable(cars2(), options = list(pageLength = 50)) %>%
      formatStyle('speed', backgroundColor = 'yellow')

  })

}