2

我正在尝试为媒体数据构建一个简单的数据输入表,用户也可以添加数据并将其保存在所需的位置。目前,每当我保存场景时,它都会将变量保存为数据,我认为这是由于保存函数中的参数。我希望将变量保存为 input$name 值,但我一生都无法弄清楚如何。我试过使用 assign 等,但无济于事。对此的任何帮助将不胜感激。

谢谢!

    library(shiny)
    library(rhandsontable)
    library(data.table)

    setwd(choose.dir())

    saveData <- function(data,name) {
    # Create a unique file name
    # Write the file to the local system
    save(
    x = data,
    file =  paste0(name,".rdata")
    )
    }

    ui <- fluidPage(
     rHandsontableOutput('table'),
     textInput("savename",label = "File Name"),
     actionButton("save",label = "save")
    )

    server <- function(input, output) {
      #build table via user controls
      start.date <- as.Date("2017/1/1")
      end.date <- as.Date("2017/1/5")
      date.range <- as.character(seq(start.date,end.date,by='day'))
      Channels <- c("TV","OOH","AFFILIATES","DIGITAL")
      df <- as.data.frame(matrix(0,length(Channels),length(date.range),dimnames = list(Channels,date.range)))
  output$table <- renderRHandsontable({rhandsontable(df, height = 220,width = 1500, colWidths = 200,rowHeights = 40, rowHeaderWidth  = 200, colHeaderHeight = 30, className =  "htCenter htMiddle")})

  #data input
  data1 <- eventReactive(input$save, {
    df <- hot_to_r(input$table)
    df <- t(df)
    df <- as.data.frame(df)
    df <- setDT(df, keep.rownames = TRUE)[]
    colnames(df)[1] <- "Date"
    df$Date <- as.Date(df$Date)
    df
    })

  observeEvent(input$save, {
    name <- input$savename
    saveData(data1(),name)
    })
}
4

1 回答 1

0

您可以使用assign()然后调用save(list= )

saveData <- function(data,name) {
  assign(name, data)
  save(
    list = name,
    file = paste0(name, ".rdata")
  )
}
于 2017-04-28T00:05:48.607 回答