R 在Shining中加载、编辑和下载csv文件
我正试图加载一个csv文件到一个闪亮的程序,编辑它的地方,并下载编辑后的文件。我发现包R 在Shining中加载、编辑和下载csv文件,r,shiny,yui-datatable,R,Shiny,Yui Datatable,我正试图加载一个csv文件到一个闪亮的程序,编辑它的地方,并下载编辑后的文件。我发现包DT提供了一些相同的功能。我修改了csv以从文件中读取csv并写入输出。我还尝试使用DT的实验特性就地编辑加载的CSV。下面的“我的代码”加载csv并保存csv,但在尝试就地编辑它时,它会崩溃,并出现以下错误: Warning: Error in <<-: invalid (NULL) left side of assignment Stack trace (innermost first): 65
DT
提供了一些相同的功能。我修改了csv以从文件中读取csv并写入输出。我还尝试使用DT的实验特性就地编辑加载的CSV。下面的“我的代码”加载csv并保存csv,但在尝试就地编辑它时,它会崩溃,并出现以下错误:
Warning: Error in <<-: invalid (NULL) left side of assignment
Stack trace (innermost first):
65: observeEventHandler [/home/user/ShinyApp/EditingValues/server.R#40]
我的server.R
是
fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
tags$hr(),
p('Once you are done download the data'),
downloadButton('x3', 'Download Data')
),
mainPanel(
DT::dataTableOutput('x1')
)
)
)
# By default, the file size limit is 5MB. It can be changed by
# setting this option. Here we'll raise limit to 9MB.
# Install DT, experimental version may not work well
# devtools::install_github('rstudio/DT@feature/editor')
library(shiny) # The main program
library(DT) # Edit the output
library(data.table) # Faster read and write
options(shiny.maxRequestSize = 9*1024^2)
function(input, output, session) {
dataframe<-reactive({
if (is.null(input$file1))
return(NULL)
dta_types = fread(input$file1$datapath, header = input$header,
sep = input$sep, quote = input$quote, nrows = 0,
strip.white = TRUE, data.table = FALSE)
data<-fread(input$file1$datapath, header = input$header,
sep = input$sep, quote = input$quote,
stringsAsFactors = FALSE, colClasses= rep("character", ncol(dta_types)))
data
})
output$x3 = downloadHandler('mtcars-filtered.csv', content = function(file) {
s = input$x1_rows_all
write.csv(dataframe()[s, , drop = FALSE], file)
})
output$x1 = DT::renderDataTable(dataframe(), selection = 'none')
proxy = dataTableProxy('x1')
observeEvent(input$x1_cell_edit, {
info = input$x1_cell_edit
str(info)
i = info$row
j = info$col
v = info$value
dataframe()[i, j] <<- DT:::coerceValue(v, dataframe()[i, j])
replaceData(proxy, dataframe(), resetPaging = FALSE)
})
}
以前
输出$x1=DT::renderDataTable(dataframe(),选择='none')
到处都使用x而不是dataframe(),但这会在加载应用程序之前使其崩溃。谢谢你的投入。
使用
rhandsontable
的问题也没有答案。使用rhandsontable
非常简单。只需使用hot\u to\u r
函数将用户编辑的表转换回r对象,然后保存即可。使用rhandsontable
非常简单。您只需要使用hot\u to\u r
函数将用户编辑的表转换回r对象,然后保存它。
x=dataframe()