R语言 在Shiny中进行数据操作后下载fileinput()

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

我有一个闪亮的应用程序,经过一些数据处理,我想下载mytable。非常感谢。

library(shiny)
ui <- navbarPage(
  title = 'Benvenuti',
  tabPanel('read.me', column(3,
                             
                             h3("Daily Data Manipulation"),
                             
                             h3("title1"),
                             tags$a("Upload data.xlsx")
  )),
  
  tabPanel(title = "title2", 
           sidebarPanel( width = 2, fileInput('file1', 'Choose xlsx file',
                                              accept = c(".xlsx")),
                         numericInput("ID", "Enter the starting ID number:",
                                      value = 1, min = -Inf, max = Inf),
                         downloadButton("Download.mytable", "Download mytable")
           ), 
           
           mainPanel(tableOutput(outputId = "mytable"))
  ) ) 


server <- function(input, output) {
  
  data.Consenso <- reactive({
  inFile <- input$file1
  if(!is.null(inFile)) {
    readxl::read_excel(inFile$datapath)
    #read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)    
  }
})

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

mytable2 <- reactive({mytable})

output$Download.mytable  <- downloadHandler(
  
  filename = function() { 
    paste("data-", Sys.Date(), ".csv", sep="")
  },
  
  content = function(file) {
    
    write.csv(mytable2(), file)
    
  })

}

shinyApp(ui = ui, server = server)

错误

Warning: Error in <reactive:mytable2>: object 'mytable' not found
  3: runApp
  2: print.shiny.appobj
  1: <Anonymous>

照片

yuvru6vn

yuvru6vn1#

您使用了mytable(),但它在任何地方都没有定义。假设您正在获取原始数据并以某种方式修改内容,您需要这样的东西:

mytable <- reactive({
  req(data.Consenso())
  something <- data.Consenso() |>
    subset(somevar > 5) |>
    transform(newvar = somevar - 42)
  something
})

我使用的是base-R管道等,这只是一个演示,在允许用户下载新创建的CSV之前,this 是您需要进行任何数据操作的地方。

相关问题