我设置了一个 Shiny 的应用程序来检查 GET 字符串并在文件匹配 id
时显示一个链接。论点存在。现在我想做的是,如果在 URL 中检测到有效查询,则让页面直接重定向到下载文件。有谁知道要插入的语法,例如一个<meta http-equiv=...>
来自 server.R 的 header ?
动机:我希望能够从指向 Shiny 应用程序的 URL 将文件直接下载到 R 控制台 session 中。因此,非极客用户使用 Shiny 指定他们的初步统计模型,然后统计学家将其下载到他们通常的工作环境中,并完成剩下的工作。我需要在服务器端执行此操作,而不是使用 javascript 的 window.location
之类的东西因为客户端不支持 javascript。
这里是服务器.R
shinyServer(function(input, output, clientData) {
query <- reactive(parseQueryString(clientData$url_search));
revals <- reactiveValues();
## obtain ID from GET string
observe({revals$id <- query()$id});
## alternatively obtain ID from user input if any
observe({input$submitid; if(length(id<-isolate(input$manualid))>0) revals$id <- id;});
## update filename, path, and existance flag
observe({ revals$filename <- filename <- paste0(id<-revals$id,".rdata");
revals$filepath <- filepath <- paste0("backups/",filename);
revals$filexists <- file.exists(filepath)&&length(id)>0; });
## update download handler
output$download <- {downloadHandler(filename=function() revals$filename, content=function(oo) if(revals$filexists) system(sprintf('cp %s %s',revals$filepath,oo)))};
## render the download link (or message, or lack thereof)
output$link <- renderUI({
cat('writing link widget\n');
id<-revals$id;
if(length(id)==0) return(div(""));
if(revals$filexists) list(span('Session download link:'),downloadLink('download',id)) else {
span(paste0("File for id ",id," not found"));}});
});
这是ui.R
shinyUI(pageWithSidebar(
headerPanel(div("Banner Text"),"Page Name"),
sidebarPanel(),
mainPanel(
htmlOutput('link'),br(),br(),
span(textInput('manualid','Please type in the ID of the session you wish to retrieve:'),actionButton('submitid','Retrieve')))));
更新:
在尝试@jeff-allen 的建议时,我遇到了另一个问题:如何提取文件复制到的文件系统路径以供下载并将其转换为有效的 URL?通过在我的本地主机上修改 shell 脚本和 http 配置设置可能是可能的,但是如何以一种不需要 super 用户权限并且尽可能 Shiny 的原生方式来做到这一点?
最佳答案
Motivation: I want to be able to download files directly into an R console session from a URL pointing at a Shiny app.
...即这相当于尝试从 Shiny 的应用程序提供静态内容的一种非常迂回的方式。事实证明,我根本不需要重定向或使用 downloadHandler
。作为this post on the Shiny forum说,我在本地 www
目录中创建的任何文件都可以访问,就好像它位于我的应用程序目录的根目录中一样。 IE。如果我让我的应用执行 save.image(file='www/foo.rdata')
那么我将能够从 [http://www.myhost.com/appname/foo .rdata] 如果应用程序本身位于 [http://www.myhost.com/appname/]
关于r - 如何使 Shiny 立即重定向?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18982440/