2017-08-03 103 views
0

我有幾個輸入文件包含n列座標,我如何設計選擇輸入面板閃亮的方式,我的選擇是我的輸入文件中的列數? 例如,一旦我讀取了一個zipfile,其中有5個座標文件,輸出中我想說要顯示與文件1或文件2中座標有關的地圖或...這意味着我的選擇輸入面板應該顯示我5選項。 如果我讀了一個帶有100個文件的zipfile,我想要下拉100個選擇! 我怎麼能這樣做?我檢查了this example但我不知道如何適應它!Shiny:更改選擇輸入的值

這裏是我的代碼的一個例子。不幸的是,這不是複製的,但是降低了複雜性很容易理解:

ui <- fluidPage(

    fileInput("File","upload your file"), 

    selectInput("Select1", "Select input",c()) 
    mainPanel(tags$head(tags$style(type="text/css", ".tab-content {overflow: visible;}")), 
    leafletOutput("mymap")) 
) 

server <- function(input, output, session) { 
    coor <- reactive({infile=input$File 

        if (is.null(infile)) 
         return(NULL) 
        temp_files <- unzip(infile$datapath) 
        T=length(temp_files) 
        A_new=c();for(i in 1:T){A_new[[i]]=c()} 
        for(i in 1:T){ 
         . 
         . 
         . 
         A_new[[i]]= ... 
        } 
        result <- list(A_new=A_new,T=T); 
        return(result); 

       }) 

    observe({ 
    x <- input$File 
    if (is.null(x)) 
     x <- character(0) 
    map=coor() 
    updateSelectInput(session, "Select1", 
         label = paste("Select input label", map$T), 
         choices = map$A_new, 
         selected = tail(map$A_new, 1) 
    ) 
    }) 
} 
output$mymap <- renderLeaflet({ 
    infile=input$File 
    if (is.null(infile)) 
    return(NULL) 
    a2=coor() 
    leaflet() %>% 
    addProviderTiles("OpenTopoMap", group = "MapQuestOpen.Aerial") %>% 
    addMarkers(data =a2$A_new[[1]],~long, ~lat, popup = ~as.character(mag), label = ~as.character(Name))%>% 

    addMeasure() 
}) 

在我讀我的文件活性功能,做計算,讓我A_new[[i]]文件看起來像這樣:

A_new[[1]] 
     long  lat mag 
1 60.20424 34.61457 1100.0 
2 60.20739 34.61155 1098.3 
3 60.21058 34.60852 1099.7 
4 60.21382 34.60544 1100.5 
5 60.21700 34.60239 1100.0 
6 60.22039 34.59930 1102.7 
7 60.22388 34.59596 1103.0 
8 60.22743 34.59245 1101.3 
9 60.20323 34.60650 1080.0 
10 60.20653 34.60346 1080.0 
11 60.20966 34.60040 1080.8 
12 60.21277 34.59748 1082.7 
13 60.21590 34.59462 1084.1 
14 60.21890 34.59161 1083.1 

我想在選擇輸入中有這個A_new [[i]],然後告訴output$mymap 返回相關的地圖。 目前,因爲我無法設計地圖輸出中的選擇輸入選項,我只是顯示第一個A_new[[i]]這就是A_new[[1]]

+1

這是什麼問題?顯示給你問題的代碼。如果可能的話可重現。 –

+0

@RomanLuštrik我已經把示例代碼與描述,我希望它現在更清楚 –

回答

0

您是否嘗試使用fileinput上傳任何CSV? 我嘗試了使用fileinput讀取CSV的解決方案,並重新更改了dropdownlist(selectInput)中的列名稱。

上傳1日CSV有4列

enter image description here

當4列顯示在列表

現在再次上傳CSV 21列

enter image description here

這都說明21列..

代碼: ui.r

library(shiny) 

fluidPage(

    fileInput("file","Upload your CSV",multiple = FALSE), 
    wellPanel(
    tags$h3("Select the parameters below"), 
    checkboxInput(inputId = 'header', label = 'Header', value = FALSE), 
    checkboxInput(inputId = "stringAsFactors", "stringAsFactors", FALSE), 
    radioButtons(inputId = 'sep', label = 'Separator', choices = c(Comma=',',Semicolon=';',Tab='\t', Space=''), selected = ',') 
), 

    uiOutput("list_item") 
) 

Server.r

library(shiny) 
shinyServer(function(input, output, session){ 

    data <- reactive({ 
    file1 <- input$file 
    if(is.null(file1)){return()} 
    read.table(file=file1$datapath, sep=input$sep, header = input$header, stringsAsFactors = input$stringAsFactors) 

    }) 




    output$list_item<-renderUI({ 
    f<-data() 
    selectInput("selected_list","Select from the list",choices = as.list(colnames(f))) 
    }) 



}) 
如果你想使用upadateselectinput我能做到這一點的,這也

..