2017-04-04 36 views
1

我已經建立了一個閃亮的應用程序,基於selceted數據knitr的降價PDF的。生成的報告需要在會議中引用某種唯一ID。 我知道我可以使用會話ID,但我需要一種方法來計算每個會話的下載次數。 有沒有一種方法可以計算會話之外的事件或計算自首次部署以來會話ID的數量?shinyapps.io下載次數

+0

是什麼 –

+0

對不起,我的意思是[https://www.shinyapps.io/](https://www.shinyapps.io/) –

+0

哦,我沒有意識到這是在該網站上運行。這可能與重複計數有關。 –

回答

1

我需要這樣做一次,所以我有一些代碼躺在。它基本上跟蹤了我追加的csv中的所有內容。在這裏,我將它建成了一個閃亮的測試平臺。

  • 它採用了session$token的ID(也許有更好的東西)
  • 它使用write.tableread.table,因爲它們的行爲與append選項更好。
  • 它由閃亮的下載處理程序呼喚writetolog遞增計數,但您也可以手動一個額外的按鈕增加計數(僅做顯然測試目的)
  • 它有兩個輸出,一個是總結另一個是日誌內容的轉儲。這些用於調試,因爲downloadHandler在與所有這些反應性進行交互時可能有點「具有挑戰性」。

下面是代碼修改爲一個例子像你需要什麼:

library(shiny) 

logfname <- "log.csv" 
writetolog <- function(newcount,newsessionid,operation){ 
    time <- format(Sys.time(), "%Y-%m-%d %H:%M:%S %Z") 
    df <- data.frame(time=time,count=newcount,sessionid=newsessionid,operation=operation) 
    doappend <- file.exists(logfname) 
    if (doappend){ 
    write.table(df,logfname,append=T,quote=F,col.names=F,sep=",",row.names=F) 
    } else { 
    write.table(df,logfname,append=F,quote=F,sep=",",row.names=F) 
    } 
} 
getcounts <- function(){ 
    if (!file.exists(logfname)){ 
    return(list(count=0,sessioncount=0)) 
    } 
    df <- read.table(logfname,header=T,sep=",") 
    nr <- nrow(df) 
    rlst <- list(count=sum(df$count),sessioncount=length(unique(df$sessionid)),          
       lastop=df$operation[nr],lasttime=df$time[nr]) 
    return(rlst) 
} 

ui <- fluidPage(
    titlePanel("Keep a download log"), 
    sidebarLayout(
    sidebarPanel(
     actionButton("inccount","Increment Count"), 
     actionButton("getcount","Refresh Summary"), 
     actionButton("showlog","Show Log"), 
     downloadButton("dodownload", "Save to .csv") 
    ), 
    mainPanel(
     h2("Summary of Download Log"), 
     verbatimTextOutput("showcount"), 
     h2("Dump of Download Log"), 
     verbatimTextOutput("loglog") 
    ) 
) 
) 

server <- function(input, output,session) { 
    observeEvent(input$inccount,{ 
    print("writetolog") 
    writetolog(1,session$token,"inc count") 
    }) 

    output$showcount <- renderPrint({ 
    input$getcount 
    rv <- getcounts() 
    time <- format(Sys.time(), "%Y-%m-%d %H:%M:%S %Z") 
    print(sprintf("%s - count:%d sessioncount:%d",time,rv$count,rv$sessioncount)) 
    }) 
    output$loglog <- renderPrint({ 
    input$showlog 
    if (!file.exists(logfname)) return(NULL) 
    ldf <- read.csv(logfname) 
    print(ldf) 
    }) 

    output$dodownload<-downloadHandler(  
    filename = function() { 
     paste(input$table_name, '.csv', sep='') 
    }, 
    content = function(file) { 
     write.csv(mtcars, file) 
     writetolog(1,session$token,"save file") 
    } 
) 
} 
shinyApp(ui = ui, server = server) 

屏幕快照: 「shinyapp.oi」

enter image description here

+0

在csv中存儲會話值是有意義的,並且爲唯一ID進行過濾對於下載計數器來說非常合適。我發現'downloadButton'工作在w /'downloadHandler'而不是'actionButton','observeEvent'不會聽'downloadButton'或'downloadHandler'。任何想法如何讓'observeEvent'聽'downloadButton'? –

+0

謝謝加載!完美的作品,但它必須觸發'寫入(1,會話$令牌)'兩次。點擊按鈕後,點擊次數會增加,點擊保存時再次點擊。 –

+0

當然,你不要在兩個地方調用它? –