如何在R中保存当前工作区
我希望允许我的用户将他们当前的工作区保存到他们选择的目录中。我不能让它工作 下面是我的代码。有什么建议吗?提前谢谢 ui.R:如何在R中保存当前工作区,r,download,shiny,R,Download,Shiny,我希望允许我的用户将他们当前的工作区保存到他们选择的目录中。我不能让它工作 下面是我的代码。有什么建议吗?提前谢谢 ui.R: shinyUI(fluidPage( titlePanel("Save RData"), sidebarLayout( sidebarPanel( ), mainPanel( downloadButton('download_structure', "Save") ) ) )) textInput("RData_
shinyUI(fluidPage(
titlePanel("Save RData"),
sidebarLayout(
sidebarPanel(
),
mainPanel(
downloadButton('download_structure', "Save")
)
)
))
textInput("RData_name_save", "RData name"),
textInput("RData_dir_save", "Save directory"),
server.R
shinyServer(function(input, output) {
output$download_structure <- downloadHandler(
filename = function() {paste("Test.RData")},
content = function(file) {
list = ls(all.names = TRUE)
}
)
})
library(shiny)
shinyUI(fluidPage(
fluidRow(
column(
width = 10,
titlePanel("Save RData"),
directoryInput('directory', label = 'select directory'),
hr(),
textOutput("dir"),
textInput("file_name","give file name"),
actionButton("save","Save RData"),
conditionalPanel(
condition="output.saved!=0",
h4('Saved successfully')
)
)
)
))
library(shiny)
shinyServer(function(input, output, session) {
observeEvent(input$save,{
SaveRData()
})
observeEvent(
ignoreNULL = TRUE,
eventExpr = {
input$directory
},
handlerExpr = {
if (input$directory > 0) {
# condition prevents handler execution on initial app launch
path = choose.dir(default = readDirectoryInput(session, 'directory'))
updateDirectoryInput(session, 'directory', value = path)
}
}
)
output$directory = renderText({
readDirectoryInput(session, 'directory')})
SaveRData <- reactive({
if(!(is.null(dir) && is.null(input$save) && is.null(input$file_name))){
dir <- readDirectoryInput(session, 'directory')
file_name <- paste(input$file_name,".RData", sep="")
save.image(file = paste(dir,file_name, sep = "\\"))
}
})
output$saved<-reactive({input$save})
outputOptions(output, 'saved', suspendWhenHidden = FALSE)
})
我的服务器的代码片段。R:
tempdir <- paste0(input$RData_dir_save,input$RData_name_save,'.RData"')
save.image(file=eval(parse(text=tempdir)))
tempdir我尝试了使用directoryInput
并从用户处获取文件名的解决方法。此解决方案不使用downloadHandler
功能,它只是一个临时解决方案。
对于目录的选择,我使用了配置的directoryInput
ui.R
shinyServer(function(input, output) {
output$download_structure <- downloadHandler(
filename = function() {paste("Test.RData")},
content = function(file) {
list = ls(all.names = TRUE)
}
)
})
library(shiny)
shinyUI(fluidPage(
fluidRow(
column(
width = 10,
titlePanel("Save RData"),
directoryInput('directory', label = 'select directory'),
hr(),
textOutput("dir"),
textInput("file_name","give file name"),
actionButton("save","Save RData"),
conditionalPanel(
condition="output.saved!=0",
h4('Saved successfully')
)
)
)
))
library(shiny)
shinyServer(function(input, output, session) {
observeEvent(input$save,{
SaveRData()
})
observeEvent(
ignoreNULL = TRUE,
eventExpr = {
input$directory
},
handlerExpr = {
if (input$directory > 0) {
# condition prevents handler execution on initial app launch
path = choose.dir(default = readDirectoryInput(session, 'directory'))
updateDirectoryInput(session, 'directory', value = path)
}
}
)
output$directory = renderText({
readDirectoryInput(session, 'directory')})
SaveRData <- reactive({
if(!(is.null(dir) && is.null(input$save) && is.null(input$file_name))){
dir <- readDirectoryInput(session, 'directory')
file_name <- paste(input$file_name,".RData", sep="")
save.image(file = paste(dir,file_name, sep = "\\"))
}
})
output$saved<-reactive({input$save})
outputOptions(output, 'saved', suspendWhenHidden = FALSE)
})
注:
conditionalPanel
显示在action按钮
触发保存到server.R
server.R
shinyServer(function(input, output) {
output$download_structure <- downloadHandler(
filename = function() {paste("Test.RData")},
content = function(file) {
list = ls(all.names = TRUE)
}
)
})
library(shiny)
shinyUI(fluidPage(
fluidRow(
column(
width = 10,
titlePanel("Save RData"),
directoryInput('directory', label = 'select directory'),
hr(),
textOutput("dir"),
textInput("file_name","give file name"),
actionButton("save","Save RData"),
conditionalPanel(
condition="output.saved!=0",
h4('Saved successfully')
)
)
)
))
library(shiny)
shinyServer(function(input, output, session) {
observeEvent(input$save,{
SaveRData()
})
observeEvent(
ignoreNULL = TRUE,
eventExpr = {
input$directory
},
handlerExpr = {
if (input$directory > 0) {
# condition prevents handler execution on initial app launch
path = choose.dir(default = readDirectoryInput(session, 'directory'))
updateDirectoryInput(session, 'directory', value = path)
}
}
)
output$directory = renderText({
readDirectoryInput(session, 'directory')})
SaveRData <- reactive({
if(!(is.null(dir) && is.null(input$save) && is.null(input$file_name))){
dir <- readDirectoryInput(session, 'directory')
file_name <- paste(input$file_name,".RData", sep="")
save.image(file = paste(dir,file_name, sep = "\\"))
}
})
output$saved<-reactive({input$save})
outputOptions(output, 'saved', suspendWhenHidden = FALSE)
})
库(闪亮)
shinyServer(功能(输入、输出、会话){
observeEvent(输入$save{
SaveRData()
})
敏锐的(
ignoreNULL=TRUE,
eventExpr={
输入$directory
},
handlerExpr={
如果(输入$directory>0){
#条件阻止处理程序在初始应用程序启动时执行
path=choose.dir(默认值为readDirectoryInput(会话,'directory'))
updateDirectoryInput(会话“目录”,值=路径)
}
}
)
输出$directory=renderText({
readDirectoryInput(会话“目录”)}
SaveRData问题在于对象不在闪亮的环境中,save.image()不会保存任何有趣的内容
也许这是有趣的,抄袭自
我经常希望我可以在闪亮的应用程序中以交互方式工作,类似于在函数中使用browse(),有时我使用闪亮的GUI来达到只想保存东西的状态
所以,我写了一些可以保存所有闪亮的应用程序对象的东西,它使用Hadley的pryr包递归地列出对象
图书馆(普赖尔)
在ui.R中,您需要一个操作按钮。您可以将其添加到现有的示例中。
操作按钮(“保存对象”、“保存对象”)
在server.R中,我有以下代码,它侦听save_objs:
observeEvent(input$save_objs, {
# Run whenever save_objs button is pressed
print("** saving objects! **")
## Print the objects being saved
print(rls())
# ## Put objects into current environment
for(obj in unlist(rls())) {
if(class(get(obj, pos = -1))[1] == "reactive"){
## execute the reactive objects and put them in to this
## environment i.e. into the environment of this function
assign(obj, value = eval(call(obj)))
} else {
## grab the global variables and put them into this
## environment
assign(obj, value = get(obj, pos = -1))
}
}
input_copy <- list()
for(nm in names(input)){
# assign(paste0("input_copy$", nm), value <- input[[nm]])
input_copy[[nm]] <- input[[nm]]
}
## save objects in current environment
save(list = ls(), file = "shiny_env.Rdata", envir = environment())
print("** done saving **")
})
observeEvent(输入$save_objs{
#每当按下save_objs按钮时运行
打印(“**正在保存对象!**”)
##打印正在保存的对象
打印(rls())
###将对象放入当前环境
对于(未列出的对象(rls())){
if(类(get(obj,pos=-1))[1]=“无功”){
##执行反应对象并将其放入此
##环境,即进入该功能的环境
赋值(obj,value=eval(调用(obj)))
}否则{
##抓取全局变量并将它们放入此
##环境
赋值(obj,value=get(obj,pos=-1))
}
}
输入\复制从另一个解决方案中我猜文件已保存,但将为空。它真的有效吗?如果有效,您知道原因吗?