我有一个闪亮的应用程序,它需要大量时间下载 zip 文件。我正在尝试使用futures和promises包来管理下载,以便其他用户可以在下载过程中访问该应用程序。
该应用程序如下所示:
library(shiny)
ui <- fluidPage(
downloadButton("Download", "Download")
)
server <- function(input, output){
output$Download <- downloadHandler(
filename = "Downloads.zip",
content = function(file){
withProgress(message = "Writing Files to Disk. Please wait...", {
temp <- setwd(tempdir())
on.exit(setwd(temp))
files <- c("mtcars.csv", "iris.csv")
write.csv(mtcars, "mtcars.csv")
write.csv(iris, "iris.csv")
zip(zipfile = file, files = files)
})
}
)
}
shinyApp(ui, server)
Run Code Online (Sandbox Code Playgroud)
我试过将函数write.csv内部包装起来future并设置`,虽然这不会引发错误,但在下载过程中其他用户无法使用该应用程序。
library(shiny)
library(promises)
library(future)
plan(multiprocess)
ui <- fluidPage(
downloadButton("Download", "Download")
)
server <- function(input, output){ …Run Code Online (Sandbox Code Playgroud) 我想从字符串末尾开始每五个字符插入一个冒号,最好在R中使用regex和gsub。
text <- "My Very Enthusiastic Mother Just Served Us Noodles!"
Run Code Online (Sandbox Code Playgroud)
我已经能够使用以下命令从文本开头每隔五个字符插入一个冒号:
gsub('(.{5})', "\\1:", text, perl = T)
Run Code Online (Sandbox Code Playgroud)
我为实现这一目的编写了一个优雅的函数,如下所示:
library(dplyr)
str_reverse<-function(x){
strsplit(x,split='')[[1]] %>% rev() %>% paste(collapse = "")
}
text2<-str_reverse(text)
text3<-gsub('(.{5})', "\\1:", text2, perl = T)
str_reverse(text3)
Run Code Online (Sandbox Code Playgroud)
得到期望的结果
[1]“ M:y Ver:y Ent:husia:stic:Mothe:r Jus:t Ser:ved U:s Noo:dles!”
有没有办法可以使用正则表达式直接实现?