使用闪亮的包上传数据,更改数据框并下载结果 [英] Upload data, change data frame and download result using shiny package
问题描述
我是R的新手,也许有人可以帮助我解决有关光泽的问题.
我想将data.frame作为csv上载,然后通过一些计算来更改data.frame,然后再使用Shiny应用程序再次下载data.frame.可以说我有一个数据框:
x <- data.frame(value1=c(1:100), value2=c(101:200)) #data frame I uploaded
x$value2 <- x$value2/x$value1 # calculation
# then download it
到目前为止,在闪亮的画廊的帮助下,我的代码看起来像这样:(我只尝试上传一个csv并再次下载而没有计算):
ui.R:
library(shiny)
fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
downloadButton('downloadData', 'Download')
),
mainPanel(
tableOutput('contents')
)
)
)
server.R:
library(shiny)
function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$file1, '.csv', sep='')
},
content = function(file) {
write.csv(file1, file)
}
)
}
我可以上传一个csv,这没问题,但是下载不起作用,我没有引用上传的文件.
现在有人回答了吗,或者可以在这里帮助我?也许这是一个愚蠢的错误,但我五天前开始使用R,这对我来说是全新的.
非常感谢!
以下对我有用:
UI
ui <- fluidPage(
fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
downloadButton('downloadData', 'Download')
),
mainPanel(
tableOutput('contents')
)
)
)
)
服务器
server <- function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
output$contents <- renderTable(
getData()
)
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(getData(), file)
})
}
运行
shinyApp(ui, server)
如您所见,我创建了一个反应性对象来生成数据并将其保存.使用read.csv
时,不会在代码中的任何位置保存数据.您仅读取数据并将其提供给renderTable
,但数据并未保存在R中.本质上,您需要保存数据并将其提供给其他ouput$...
功能.这就是reactive
在这种情况下所做的.保存文件并使其可用于其他输出功能.
有一个教程此处
I am new to R and maybe someone could help me with a question about shiny.
I want to upload a data.frame as csv with shiny, after that I want to change the data.frame by some calculations, then I want to download the data.frame again with the shiny application. Lets say I have a dataframe:
x <- data.frame(value1=c(1:100), value2=c(101:200)) #data frame I uploaded
x$value2 <- x$value2/x$value1 # calculation
# then download it
with the help of the shiny gallery my code looks like this so far: (where I only tried to upload a csv and download it again with no calculations):
ui.R:
library(shiny)
fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
downloadButton('downloadData', 'Download')
),
mainPanel(
tableOutput('contents')
)
)
)
server.R:
library(shiny)
function(input, output) {
output$contents <- renderTable({
inFile <- input$file1
if (is.null(inFile))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
output$downloadData <- downloadHandler(
filename = function() {
paste(input$file1, '.csv', sep='')
},
content = function(file) {
write.csv(file1, file)
}
)
}
I can upload a csv this is no problem, but the download does not work, I have no reference to the uploaded file..
Does somebody now the answer or could help me here? Maybe its a silly error but I started with R five days ago and this is all new to me.
Many thanks!!
The following works for me:
UI
ui <- fluidPage(
fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
downloadButton('downloadData', 'Download')
),
mainPanel(
tableOutput('contents')
)
)
)
)
Server
server <- function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(input$file1))
return(NULL)
read.csv(inFile$datapath, header=input$header, sep=input$sep,
quote=input$quote)
})
output$contents <- renderTable(
getData()
)
output$downloadData <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(getData(), file)
})
}
Run
shinyApp(ui, server)
As you can see I create a reactive object to generate the data and keep it saved. You do not save the data anywhere in your code when you use read.csv
. You just read the data and feed it to renderTable
but the data is not saved in R. Essentially you need to save the data and also make it available to other ouput$...
functions. This is what reactive
does on this occasion. Saves the file and makes it available to the other output functions.
There is a tutorial here
这篇关于使用闪亮的包上传数据,更改数据框并下载结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!