2016-10-13 22 views
1

私は、多くのユーザー入力を持つShinyアプリケーションを作成しています。そのため、後で使用するために入力を保存して戻すオプションがあります。私はオンラインで見つかった例にしたがってこれを行うことができましたが、今はコードの機能を変更する助けを頼みたいと思います。ここでは、コードは次のようになります。R後で使用するために入力を保存するShiny App

library(shiny) 

ui <- shinyUI(fluidPage(
    br(), 

    actionButton("load_inputs", "Load inputs"), 
    br(), 
    br(), 

    numericInput("n", "Number",min = 1, value = 5), 
    numericInput("upper", "Upper",min = 0, max = 100, value = 15), 
    numericInput("lower", "Lower",min = 0, max = 100, value = 5), 

    actionButton('save_inputs', 'Save inputs') 

)) 

server <- shinyServer(function(input, output,session) { 

    switch(Sys.info()[['sysname']], 
        Windows= {setwd(file.path(Sys.getenv("USERPROFILE"),"Desktop",fsep="\\"))}, 
        Mac = {  setwd("~/Desktop/")}) 

    observeEvent(input$load_inputs,{ 

    if(!file.exists('inputs.RDS')) {return(NULL)} 

    savedInputs <- readRDS('inputs.RDS') 

    inputIDs  <- names(savedInputs) 
    inputvalues <- unlist(savedInputs) 
    for (i in 1:length(savedInputs)) { 
     session$sendInputMessage(inputIDs[i], list(value=inputvalues[[i]])) 
    } 
    }) 

    observeEvent(input$save_inputs,{ 
    saveRDS(reactiveValuesToList(input) , file = 'inputs.RDS') 
    }) 
}) 

shinyApp(ui=ui,server=server) 

、むしろ.RDSオブジェクトとしてファイルを保存するよりも、私は.csvファイルとしてファイルを保存したいRに精通していないユーザが実際に読むことができるような方法は、必要に応じてファイルを更新してください。だから私は(と値の例を)次の形式で.csvファイルとして保存するための入力のための希望:

n, 5 
lower,10 
upper, 29 

簡単にRで操作することがあれば、それはまた

n, lower, upper 
5, 10, 29 
としてフォーマットすることができ

最後に、ファイルを読み込むときに、アプリケーションがファイルの場所を指すようにユーザーに依頼してから、ファイルを保存する場所をユーザーに尋ねるためにアプリケーションの入力を保存するといいでしょう。

これを達成するためにコードを編集するにはどうすればよいですか?

+1

http://shiny.rstudio.com/articles/persistent-data-storage.html – Carl

+1

@Carl私は、人々をhttp://deanattali.com/blog/shiny-japaneseの記事の元のソースに向けることを好む。 persistent-data-storage /にはRStudioのバージョンにはない更新があるので –

+0

残念ですが、それは二重になっていることがわかりませんでした。 – Carl

答えて

1
library(shiny) 

ui <- shinyUI(fluidPage(

    textInput(inputId = 'inputsLocation', label = 'Inputs Location', value = "~/Desktop/user_inputs.csv"), 
    actionButton('load_inputs', 'Load inputs'), 
    br(), 
    br(), 
    numericInput("n", "Number",min = 1, value = 5), 
    numericInput("upper", "Upper",min = 0, max = 100, value = 15), 
    numericInput("lower", "Lower",min = 0, max = 100, value = 5), 
    actionButton('save_inputs', 'Save inputs') 

)) 

server <- shinyServer(function(input, output,session) { 

    observeEvent(input$load_inputs, { 
    # Load inputs 
    uploaded_inputs <- read.csv(input$inputsLocation) 
    # Update each input 
    for(i in 1:nrow(uploaded_inputs)){ 
     updateNumericInput(session, 
         inputId = uploaded_inputs$inputId[i], 
         value = uploaded_inputs$value[i]) 
    } 
    }) 

    observeEvent(input$save_inputs, { 
    # Define inputs to save 
    inputs_to_save <- c('n', 'upper', 'lower') 
    # Declare inputs 
    inputs <- NULL 
    # Append all inputs before saving to folder 
    for(input.i in inputs_to_save){ 
     inputs <- append(inputs, input[[input.i]]) 
    } 
    # Inputs data.frame 
    inputs_data_frame <- data.frame(inputId = inputs_to_save, value = inputs) 
    # Save Inputs 
    write.csv(inputs_data_frame, file = input$inputsLocation, row.names = FALSE) 
    }) 

}) 

shinyApp(ui=ui,server=server) 

データ「user_inputs.csvは」次のようになります。

"inputId","value" 
"n",5 
"upper",7 
"lower",4 

コードをより明確にすることができますので、私は列名「inputId」と「価値」を追加しました。

コードは元の投稿から変更されています。 .csvファイルを使用するには、入力をロード/保存する別の方法が必要です。私は、この方法は、それを行うための簡単な方法だと思います。

注*手動でファイルパス入力を入力するときは、ファイルパスに二重スラッシュ「\\」または円記号「/」を使用する必要があります(Rと同じ)。

関連する問題