r - 使用 R shiny 上传后编辑数据框

标签 r shiny edit import-from-csv

我构建了一个 Shiny 的应用程序,它接受用户上传的 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 session 中,我将使用的代码是这样的:

  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)

如果我在 shiny 中将这些行添加到我的 Rshiny 应用程序中,它将无法工作并给我错误: 错误:长度为 0 的参数,即使我只是使用 colnames() 定义列名也是如此。

所以我的问题是,有没有办法编辑我刚刚上传的数据框?我已经问过这个问题了,你能不能把我重定向到那里,因为我找不到解决方案。 我希望我给了你足够的细节来理解这个问题。

最佳答案

希望这有帮助:我也添加了一个按钮

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))

关于r - 使用 R shiny 上传后编辑数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27861865/

相关文章:

r - 支持 R(或仅外部程序)的基于 Web 的报告生成

正则表达式 - 在逗号上拆分字符串,跳过平衡括号之间的任何内容

r - R 的 `ByteCompile` 什么时候适得其反?

R Shiny,如何让观察者先执行?

r - Shiny 的应用程序在许多同时请求时不稳定

Azure B2C EditProfile 自定义策略,无需先登录

installation - Inno Setup 如何在安装过程中编辑 ini 文件

r - geom_text() 中超过 1 种颜色

database - 字符编码,带有数据库的 dplyr (postgresql)

file - 如何通过 Ubuntu 终端编辑/保存文件