R语言 在shiny中输入文件(excel)后的数据操作

14ifxucb  于 2023-05-26  发布在  其他
关注(0)|答案(1)|浏览(158)

我有一个Excel数据集,我想在上传后操作它。假设在这种情况下,我想添加一个newcolumn <- 3,我该如何解决这个问题?非常感谢。

用户界面

library(shiny)

ui <- fluidPage(titlePanel("daily data manipulation"),
                sidebarLayout(sidebarPanel(
                  fileInput('file1', 'Choose xlsx file',
                            accept = c(".xlsx"))
                ),
                
                
                
                mainPanel(tableOutput(outputId = "mytable") 
                  
                  )))

server.r

server <- function(input, output) {
  
  

  data <- reactive({
    inFile <- input$file1
    if(!is.null(inFile)) {
      readxl::read_excel(inFile$datapath)   
    }
  })
  

 dat <- data()
 

 
 output$mytable <- renderTable({
   dat$newcolumn <- 3
   
 })
 
  }
mzaanser

mzaanser1#

React式表达式只能在React式上下文中使用,因此dat <- data()应该移动到renderTable内部。

server <- function(input, output, session) { 
  data <- reactive({
    inFile <- input$file1
    if(!is.null(inFile)) {
      readxl::read_excel(inFile$datapath)
    }
  })

  output$mytable <- renderTable({
    dat <- data()
    if(!is.null(dat))
    {
      dat$newcolumn <- 3
      dat
    }
  }) 
}

此外,在添加新列之前检查reactive值是否为null可确保在用户上传excel文件之前不会呈现新列。

相关问题