2013-09-24 43 views
3

我设置了一个闪亮的应用程序,用于检查GET字符串,并在存在匹配id参数的文件时显示链接。现在我想要做的是,如果在URL中检测到有效的查询,该页面会直接重定向到下载文件。有人知道插入的语法,例如一个<meta http-equiv=...>来自server.R的头文件?如何使Shiny立即重定向?

动机:我希望能够通过指向Shiny应用程序的URL直接将文件下载到R控制台会话中。因此,一个非怪异的用户使用Shiny指定他们的初步统计模型,然后统计员将其下载到他们平常的工作环境中,并将其余的部分下载。我需要做这个服务器端,而不是像JavaScript的window.location,因为JavaScript不会被支持客户端。

这里是server.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'))))); 

更新:

在试图@杰夫 - 阿伦的建议下,我遇到了另一个问题:如何提取文件被复制到的文件系统路径以供下载把它变成一个有效的URL?这可能是通过在本地主机上使用shell脚本和http配置设置来实现的,但是如何以可移植的方式执行此操作,而不需要超级用户权限并且尽可能使用本地闪存?

+3

您可以使用'tag()'在UI中插入任意HTML标签(如'meta')。如果您的标签需要在服务器端生成,您可以使用'dynamicUI()'将服务器上生成的UI元素传递给UI。 –

回答

4

动机:我希望能够通过指向Shiny应用程序的URL直接将文件下载到R 控制台会话中。

......即,这相当于试图从一个闪亮的应用程序提供静态内容的一个非常迂回的方式。原来我根本不需要重定向或使用downloadHandler。正如this post on the Shiny forum所说,我在本地www目录内创建的任何文件都可以访问,就好像它位于我的应用程序目录的根目录下一样。即如果我有我的应用程序save.image(file='www/foo.rdata'),那么如果应用程序本身位于[http://www.myhost.com/],我将可以从[http://www.myhost.com/appname/foo.rdata]访问它。 appname /]

相关问题