【问题标题】:R shiny-server: User specific directory for each sessionR shiny-server:每个会话的用户特定目录
【发布时间】:2016-01-13 20:19:12
【问题描述】:

我的闪亮应用程序位于服务器上,每次用户使用该应用程序时,都会在本地应用程序文件夹中生成一些文本文件(不同的内容,相同的文件名)。我担心当多个用户使用该应用程序时,由于文本文件的位置和文件名相同,因此导出的文本文件可能会发生冲突。

为了避免这种可能性,我正在考虑在用户启动会话时创建目录的方向,所有工作文件都导出到该目录,当会话关闭时,必须删除该目录。

关于如何进行此操作的任何想法?有更好的解决方案吗?如何检测会话何时关闭?如何计算活动会话数?

【问题讨论】:

    标签: r shiny-server shiny


    【解决方案1】:

    这在闪亮服务器环境以及闪亮应用程序.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))
      })
    })
    

    【讨论】:

    • 如果两个用户同时访问服务器会怎样?完全有可能。
    • 附加一些额外的随机数。
    • 我发现你也可以使用 session$token,它是特定于会话的。
    猜你喜欢
    • 1970-01-01
    • 2010-12-18
    • 2013-11-06
    • 2014-10-18
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多