Re: [問題] Shiny 套件請益

看板R_Language作者 (johnson)時間11年前 (2013/04/19 01:51), 編輯推噓2(204)
留言6則, 4人參與, 最新討論串2/2 (看更多)
※ 引述《acecc (cc)》之銘言: : [問題類型]: : 程式諮詢(我想用R 做某件事情,但是我不知道要怎麼用R 寫出來) : [軟體熟悉度]: : R的基本使用沒問題,但使用Shiny套件是完全的新手... : 先寫個基本款來試試看... : [問題敘述]: : 程式做的事情很單純, : 1. upload 一個檔案 : 2. 程式對每個Column做簡單的線性預測 : 3. 將結果download下來 : 程式我有丟上Glimmer Server : http://glimmer.rstudio.com/glen/Pred/ : 並附上一個簡易的測試檔案,可以用此檔案直接upload,並算出結果。 : https://www.asuswebstorage.com/navigate/s/14B96034DC784748976F45174C9F274BY : 但我有三個技術上的問題想克服,請教各位~ : 1. 若同時間此程式有好幾個人同時使用,那麼很有可能會download到別人的output. : 例如:A先跑完但還沒做download的動作,此時B也上傳了另一個檔案跑了另一個output出來 : 那麼這時候A 與 B去download檔案的時候,就都會download到B產生的output : 請問我要怎麼能讓每個End user都能下載到自己產生的output? : 這是server端的問題,還是我程式寫法的問題? 我的解決辦法是每次使用者開啟網頁就隨機生成一個暫存檔(tempRD1), 然後把資料寫到暫存檔裡面(run datasetInput時順便寫入), 當使用者要下載時再從暫存檔裡面抓(readRDS(tempRD1))。 大概可以經由以下code實踐 (未經測試 汗 = =") shinyServer(function(input, output) { ... tempRD1 <- paste(tempfile(), ".RData", sep="") datasetInput <- reactive({ out <- switch(input$dataset, "FIT" = fit) saveRDS(out, tempRD1) out }) output$downloadData <- downloadHandler( filename = function() { paste(input$dataset, '.csv', sep='') }, content = function(file) { out <- readRDS(tempRD1) write.csv(out, file) } ) }) : 2. 請教一下有沒有辦法讓流程縮短為 : a. upload 一個檔案 : b. 程式對每個Column做簡單的線性預測且自動跳出結果問你要不要download : 而不用讓End user再去點download? : Shiny套件能做到這個效果嗎? 就我對Shiny 0.4.0的印象,沒有這個預設功能, 不過這樣的效果還是做得到(頗麻煩),我想大概的流程如下: 1. 判斷程式甚麼時候跑完 2. 利用conditionalPanel(), 當程式跑完跳出一個yes/no box (note: Shiny 目前沒有yes/no box, 你會用到javascript: confirm()) : 3. 有沒有辦法讓程式跑完自動將結果寄到指定的的E mail信箱而不用手動下載。 那你還是要先填email地址啊... 你是想要多一個欄位填email,然後按下發送鍵這樣? 你可能需要這兩個套件: {sendmailR} and {shinyIncubator} 然後利用sendmail() + actionBotion() 完成你要的效果 : [程式範例]: : 因為有兩段程式碼,我就直接貼在這兒了! : #######ui.R : library(shiny) : shinyUI(pageWithSidebar( : headerPanel("Hello!"), : sidebarPanel( : fileInput('file1', 'Input Data', : accept=c('text/csv', 'text/comma-separated-values,text/plain')), : tags$hr(), : selectInput("dataset", "Download:", : choices = "FIT"), : downloadButton('downloadData', 'Download')), : mainPanel( : tableOutput("contents") : ) : )) : #######server.R : library(shiny) : shinyServer(function(input, output) { : output$contents <- renderTable({ : inFile <- input$file1 : if (is.null(inFile)) : return(NULL) : y <- read.csv(inFile$data, header= TRUE) : data <- apply(y, 2, cumsum) : fit <<- apply(data, 2, function(z){ : z <- z[!is.na(z)] : x <- 1:length(z) : tempfit <- lm(z[-(1:6)] ~ x[-(1:6)]) : coef(tempfit)[1] + coef(tempfit)[2] * 1:70 : }) : fit : }) : #browser() : datasetInput <- reactive({ : switch(input$dataset, : "FIT" = fit) : }) : output$table <- renderTable({ : datasetInput() : }) : output$downloadData <- downloadHandler( : filename = function() { paste(input$dataset, '.csv', sep='') }, : content = function(file) { : write.csv(datasetInput(), file) : } : ) : }) : 有任何不清楚的地方麻煩再告訴我 : 感謝各位! My Shiny app: http://glimmer.rstudio.com/tchsieh/inext/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.135.249.109

04/19 02:02, , 1F
04/19 02:02, 1F

04/19 17:10, , 2F
04/19 17:10, 2F

04/20 21:54, , 3F
04/20 21:54, 3F

04/20 21:55, , 4F
但第一個問題 我用您的方式改會變成檔案無法下載
04/20 21:55, 4F

04/20 21:56, , 5F
而且小弟太弱一直找不出問題在哪
04/20 21:56, 5F

04/23 21:16, , 6F
要把saveRDS(fit, tempRD1)放在output$contents裡面才對
04/23 21:16, 6F
文章代碼(AID): #1HS3AeC5 (R_Language)
討論串 (同標題文章)
文章代碼(AID): #1HS3AeC5 (R_Language)