2013-09-24 41 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 /]

相關問題