我正在尝试为媒体数据构建一个简单的数据输入表,用户也可以添加数据并保存在所需的位置。在我保存场景的那一刻,它将变量保存为数据,我假设这是由于save函数中的参数。我希望将变量保存为输入$ name值,但我不能为我的生活找出方法。我尝试过使用分配等但无济于事。对此有任何帮助将非常感激。
谢谢!
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)
})
}
答案 0 :(得分:0)
您可以使用assign()
,然后拨打save(list= )
:
saveData <- function(data,name) {
assign(name, data)
save(
list = name,
file = paste0(name, ".rdata")
)
}