问题描述
我正在尝试编写一个简单的闪亮应用程序来上传 .csv 文件,编辑它然后下载它。我几乎在那里,只有下载的文件有一个额外的行号列。关于如何抑制这种情况的任何建议?
这是我的代码:
library(rhandsontable)
library(shiny)
ui <- fluidPage(
fluidPage(
titlePanel("Upload,edit and save a csv file"),sidebarLayout(
sidebarPanel(
fileInput('file1','Choose CSV File',accept=c('text/csv','text/comma-separated-values,text/plain','.csv')),downloadButton('downloadData','Download')
),mainPanel(
rHandsontableOutput("hot")
)
)
)
)
server <- function(input,output,session) {
getData <- reactive({
req(input$file1) # Don't run any more code until the user has selected this
inFile <- input$file1
read.csv(inFile$datapath,header=T)
})
output$hot = renderRHandsontable({
req(input$file1) # Don't run any more code until the user has selected this
if (!is.null(input$hot)) {
rhandsontable(hot_to_r(input$hot))
} else {
rhandsontable(getData())
}
})
output$downloadData <- downloadHandler(
filename = function() {
paste("data-",Sys.Date(),".csv",sep="")
},content = function(file) {
write.csv(hot_to_r(input$hot),file)
}
)
}
# Run the application
shinyApp(ui = ui,server = server)
解决方法
我们可以使用 readr::write_csv()
来避免额外的列,或者 write.csv(hot_to_r(input$hot),file,row.names = FALSE)
将 row.names 参数设置为 FALSE。
output$downloadData <- downloadHandler(
filename = function() {
paste("data-",Sys.Date(),".csv",sep="")
},content = function(file) {
readr::write_csv(hot_to_r(input$hot),file)
}
)