我正在开发一个通过 reactiveFileReader 占用 CSV 并应用一些功能的应用程序。我想在应用这些函数之前修改数据的内容。
我知道 reactive 对象不能直接修改,但我什至无法创建具有所需修改的新对象(在这种情况下,数据框中的新列名)。
这是我在服务器代码中的位置:
data <- reactiveFileReader(1000, session, "path", read.csv)
data_new <- reactive({ colnames(data) <- c("Col 1"," Col 2","Col 3") })
output$data <- renderDataTable(data_new())
不幸的是,这会产生错误“错误:尝试在小于二维的对象上设置 'colnames'”。
关于如何正确修改和 store 数据的任何建议?
非常感谢!
回答1
尝试这个
ui <- fluidPage(
uiOutput("data1"),
uiOutput("data")
)
server <- function(input, output, session) {
data <- reactiveFileReader(1000, session, "file2.csv", read.csv)
data_new <- reactive({
df <- data()
colnames(df) <- c("Col 1"," Col 2","Col 3")
df
})
output$data <- renderTable(data_new())
output$data1 <- renderTable(head(data()))
}
shinyApp(ui, server)