这在闪亮服务器环境以及闪亮应用程序.io 上都能正常工作。 Tareef Kawaf 的一个工作示例。
ui.R
#ui.R
shinyUI(fluidPage(
headerPanel("New directory issue"),
wellPanel(
helpText("I would like a new directory to be created each time a new connection is made to the app OR anytime new files are uploaded."),
helpText("This app uploads one text file, creates a new directory, extracts first 5 lines and exports a text file with the contents."),
helpText("Upload one text file with minimum 5 lines of content."),
fileInput('upload', label=h4('Upload file(s):'), multiple=FALSE)
),
mainPanel(
tags$h3("Str of uploaded content"),
verbatimTextOutput('display')
)
))
服务器.R
#server.R
#Get current wd and print
currwd <- getwd()
#FUNCTION - new directory
fn_dir <- function(currwd=NULL)
{
if(is.null(currwd)) stop("Argument 'currwd' is empty.\n")
#Create new working directory
newwd <- paste0(currwd,"/",format(Sys.time(),"%Y%m%d%H%M%S"))
dir.create(newwd)
newwd
}
shinyServer(function(input, output) {
#REACTIVE - store
store <- reactiveValues(currwd = currwd)
#REACTIVE - complete input
fn_getfilenames <- reactive({
inputdata <- input$upload
if (!is.null(inputdata))
{
store$newwd <- fn_dir(store$currwd)
return(inputdata)
}else{
return(NULL)
}
})
#OUTPUT
output$display <- renderPrint({
if (is.null(fn_getfilenames())) return(NULL)
inputdata <- fn_getfilenames()
#some stuff to export to working directory
rcontent <- as.vector(scan(file = inputdata$datapath,what = "text",n = 10))
full_file_path <- file.path(store$newwd,inputdata$name)
write(x = rcontent, file = paste0(full_file_path,"-test.txt"))
return(str(inputdata))
})
})