r-从数据帧中重命名Colnames



我正在制作一个Shiny应用程序,我想重命名数据帧中的第一个变量,以在corrplot之后制作。

在正常R中,代码为:

library(lares) 
names(Dataset)[1] <- "DR"
corr_var(Dataset, DR, top=20)

在Shiny,我有一些东西:

dataReg2 = reactive({
inFile <- input$fileReg
if (is.null(inFile))
return(NULL)
else 
data1 = read_excel(inFile$datapath)
return(data1)
})
plot=reactive({
names(dataReg2())[[1]]='DR'
corr_var(dataReg2(), DR , top = 20 )
})

但它不起作用,错误是invalid (NULL) left side of assignment。。。提前谢谢。

您不能更改反应对象的列名。复制另一个变量中的数据,然后可以更改该变量的列名。请参阅这个使用mtcars的简单示例。

library(shiny)
ui <- fluidPage(
tableOutput('tab')
)
server <- function(input, output) {
data <- reactive(mtcars)

output$tab <- renderTable({
new_table <- data()
names(new_table)[1] <- 'new'
head(new_table)
})
}
shinyApp(ui, server)

最新更新