我构建了一个闪亮的应用程序,它接受用户上传的CSV文件并添加标题 以及几个新专栏,以便事后进行一些计算。
上传的CSV文件包含2列,如下所示:
1 0.21
1 0.20
1 0.23
2 0.40
2 0.42
2 ...
要上传文件我使用此代码(正在运行):
data1<- reactive({
inFile <- input$file1
if(is.null(inFile)){return()}
data1<- read.csv(file=inFile$datapath,header =input$header,sep=input$sep)
})
下一步是将标题添加到这两列,并以这种方式添加另外两列:
Dose Response SquareRoot Log
1 0.21 sq(Response) log(Response)
1 0.20 ... ...
1 0.23 ... ...
2 0.40 ... ...
2 0.42 ... ...
2 ...
在R会话中,我将使用的代码是:
data1<- read.csv(file=inFile$datapath,header =input$header,sep=input$sep)
colnames(data1) <-c("Dose","Response")
data1$ResponseLog <- log10(data1$Response + 1)
data1$ResponseSqRoot <- sqrt(data1$Response + 1)
如果我这样做有光泽地将这些行添加到我的Rshiny应用程序中它将无法正常工作并给我错误:
错误:长度为0的参数,即使我只使用colnames()
定义列名。
所以我的问题是,有没有办法编辑我刚上传的数据帧?我已经问过这个问题你能不能重定向我,因为我找不到解决办法。 我希望我能给你足够的细节来理解这个问题。
答案 0 :(得分:7)
希望这会有所帮助:我也添加了一个按钮
rm(list = ls())
library(shiny)
ui = fluidPage(
sidebarPanel(
fileInput('file1', 'Choose file to upload',accept = c('text/csv','text/comma-separated-values','text/tab-separated-values','text/plain','.csv','.tsv')),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',c(Comma=',',Semicolon=';',Tab='\t'),'Comma'),
radioButtons('quote', 'Quote',c(None='','Double Quote'='"','Single Quote'="'"),'Double Quote'),
actionButton("Load", "Load the File"),width = 3),
mainPanel(tableOutput("my_output_data"))
)
server = function(input, output) {
data1 <- reactive({
if(input$Load == 0){return()}
inFile <- input$file1
if (is.null(inFile)){return(NULL)}
isolate({
input$Load
my_data <- read.csv(inFile$datapath, header = input$header,sep = input$sep, quote = input$quote,stringsAsFactors =FALSE)
colnames(my_data) <-c("Dose","Response")
my_data$ResponseLog <- log10(my_data$Response + 1)
my_data$ResponseSqRoot <- sqrt(my_data$Response + 1)
})
my_data
})
output$my_output_data <- renderTable({data1()},include.rownames=FALSE)
}
runApp(list(ui = ui, server = server))