2017-03-13 16 views
1

Shiny reactiveValuesToListの構築に苦労しました。目的は、プリロードされたCSVファイルとアップロードされたCSVファイルの両方からユーザが選択できるリストを許可することです。私の考えは、アップロードされた各CSVは「名前付きリスト」です。 csvテーブルの各列は「リスト」で、列ヘッダーは名前です。次に、各リストの要素を選択して、他の機能の入力として使用することができます。アップロードされたCSVファイルでreactListを更新する方法

私はreactListToValuesをよく理解していないと思うので、ShinyにアップロードされたCSV情報を反応的に更新させる方法を理解できません。

ここにサンプルCSVがあります。

> read.csv("~/scratch/tmp/mydummy.csv", header=T) 
    madeup1 madeup2 
1  332 6836 
2  9582 6184 
3  983 79139 
4 144455 79174 
5 90701  669 
6  4189 51566 
7 10959 7873 
8  4247 4189 
9 60559  419 
10 4247 13367 
11 1959  787 
12  447  489 
13 6559  419 
14  447 13367 

以下のコードはそのまま実行できます。多くの提案と助けに感謝します!

## load libraries 
library(shiny) 
library(stringr) 

customList <- list("custom1" = c("5825","6604","55037","952","55037","55037"), 
        "custom2" = c("23386","945","11314","951","11314","51495"), 
        "custom3" = c("51495","55037","26005")) 

## ShinyUI 
ui <- fluidPage(
     titlePanel("Uploading Files"), 
      sidebarLayout(
      sidebarPanel(
      width =3, 
      fileInput('file1', 'Choose CSV file', accept=c('txt/csv','text/comma-separated-values,text/plain','.csv')), 
      tags$hr(), 
      checkboxInput('header','Header', TRUE), 
      radioButtons('sep','Separator', c(Comma=',',Semicolon=';',Tab='\t'), ','), 
      radioButtons('quote','Quote', c(None ='', 
              'Double Quote'='"', 
              'Single Quote'="'"),'"'), 
      selectInput("DBname", label=h6("Select databases:"), 
         choices = c("custom" = "customList", "uploaded" = "upload_List")), 
      selectInput("path_name", label=h6("Select list"), 
         choices=""), 
      selectInput("elem_name", label=h6("Select element"), 
         choices="") 
      ), 

      mainPanel(
       column(12, 
       tabsetPanel(type="tabs", 
        tabPanel("CSV File", 
        tableOutput('contents'), verbatimTextOutput('contents2'), 
        verbatimTextOutput('contents3') 
        ) 
       ) 
       ) 
      ) 
      ) 
     ) 

#ShinyServer ################################################################## 
server <- function(input, output, session) { 
    ## under CSV tab 
    output$contents <- renderTable({ 
    inFile <- input$file1 
    if (is.null(inFile)) 
     return(NULL) 
    read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote) 
    }) 
    output$contents2 <- renderPrint(upload_List$dList) 
    output$contents3 <- renderPrint(customList) 

    #____________________________________________________________________________ 
    upload_List <- reactiveValues() 
    upload_List$fList <- c(isolate(upload_List$fList), list('same_as_custom1' = customList$custom1)) 

    #____________________________________________________________________________ 
    observe({ 
    inFile <- input$file1 
    if (is.null(inFile)) ## need action button? 
     return(NULL) 
    a <- read.csv(inFile$datapath, header=input$header, sep=input$sep, quote=input$quote, colClasses="character") 
    b <- lapply(a, as.character) ## break out the list and instead call by index 
    upload_List$dList <- c(isolate(upload_List$dList), b[1) ## new   
    #upload_List$dList <- c(isolate(upload_List$dList), lapply(a, as.character)) 
    }) 

    #____________________________________________________________________________ 
## incorrect use of reactiveValueToList 
    ##upload.asList <- isolate(reactiveValuesToList(upload_List)) 

    #____________________________________________________________________________ 
    DBname <- reactive(input$DBname) 
    pathVar <- reactive({ 
    `if`(str_detect(input$DBname, "custom"), names(customList), 
     `if`(str_detect(input$DBname, "upload"), names(upload_List), ##names(upload_List$dList), 
       customList$custom3)) 
    }) 

    #____________________________________________________________________________ 
    path_name <- reactive(input$path_name) 
    observe({ 
    updateSelectInput(session, "path_name", choices = pathVar()) 
    }) 

    #____________________________________________________________________________ 
    elem_name <- reactive(input$elem_name) 
    observe({ 
    updateSelectInput(session, "elem_name", choices = elemVar()) 
    }) 

    elemVar <- reactive({ 
    eval(as.symbol(DBname()))[[path_name()]] 
    }) 
} 

## shinyApp 
shinyApp(ui, server) 

答えて

1

技術的には、私はあなたの問題は、単にelemVar反応性のこの定義を使用して固定されていると思う:

elemVar <- reactive({ 
    # eval(as.symbol(DBname()))[[path_name()]] 
    if(str_detect(input$DBname,"custom")) return(customList[[path_name()]]) 
    if(str_detect(input$DBname,"upload")) return(upload_List$dList[[path_name()]]) 
    }) 

あなたはその後、アップロードされたデータを選択したときに更新されていない「を選択要素」の選択肢の問題去る。あなたがそれらを取得するためにupload_List[[path_name()]]を使用しようとしていたが、彼らはupload_List$dList[[path_name()]]

に格納されているが、すべてこのコードを使用すると、保存するだけでreactive機能と多分1 reactiveValueでより簡単にそれを書くことができますかなり確信して、私には非常に複雑なようですアップロードされたデータフレーム。 observeisolateの機能は必要ないと思っています。

+0

修正を指摘してくれてありがとう!提案したコードを単純化しようとします。どうもありがとう – seraphim711

関連する問題