2017-12-22 8 views
1

CSV 파일 입력에서 포인트를 그릴 수있는 간단한 반짝이는 앱이 있습니다. 현재 파일을 반짝이는 앱에 업로드하면지도가 아무 것도하지 않습니다. 전단지 맵이 업로드되는 파일에 반응하지 않기 때문이라고 생각합니다. 이 문제를 어떻게 해결할 수 있습니까?반짝 반짝 빛나는 CSV fileInput에 반응하는 전단지지도

아래 코드를 참조하십시오. 샘플 데이터는 HERE입니다.

library(shiny) 
library(shinydashboard) 
library(leaflet) 
library(dplyr) 
library(htmltools) 

shinyApp(
    ui <- fluidPage(

    titlePanel("eBird Visualizer"), 
    fileInput("MyEBirdData_in", "MyEBirdData", buttonLabel = "Upload a .csv", 
       placeholder = "No File Selected...", width = "255px", 
       accept = ".csv"), 
    leafletOutput("myMap") 
), 

    server = function(input, output) { 

    output$contents <- renderTable({ 

     inFile <- input$MyEBirdData_in 
     if (is.null(inFile)) 
     return(NULL) 
     myData = read.csv(inFile$datapath, header = input$header) 

     df0 = data.frame(myData$Submission.ID, myData$Latitude, myData$Longitude) 
     df = unique(df0) 
     names(df)[2] = 'latitude' 
     names(df)[3] = 'longitude' 

    }) 

    output$myMap = renderLeaflet({ 

     leaflet(data = df) %>% addProviderTiles(providers$CartoDB.Positron) 
    }) 
    } 
) 
+0

당신은 ['reactiveValues'] (https://shiny.rstudio.com/reference/shiny/latest/reactiveValues.html) – SBista

답변

2

은 UI에 표시 할 반응 테이블을 만드는 데 사용됩니다. 원하는 것은 다른 반응식에서 업데이트를 트리거하는 데 사용할 수있는 변수를 갖는 것입니다.이 경우 reactive 또는 reactiveValue을 사용해야합니다 (예 : here 참조). 페이지 아래 2/3, 좋은 예를 들어 reactive을 먼저 소개합니다.

또한 현재 사용자는 정의되지 않은 input$header을 참조하고 있습니다. 따라서 csv를 업로드 할 때 오류가 발생합니다.

그래서 당신이 뭔가를 할 수 있습니다 :

library(shiny) 
library(shinydashboard) 
library(leaflet) 
library(dplyr) 
library(htmltools) 

shinyApp(
    ui <- fluidPage(

    titlePanel("eBird Visualizer"), 
    fileInput("MyEBirdData_in", "MyEBirdData", buttonLabel = "Upload a .csv", 
       placeholder = "No File Selected...", width = "255px", 
       accept = c(".csv","text/csv")), 
    leafletOutput("myMap") 
), 

    server = function(input, output) { 

    my_table <- reactive({ 

     inFile <- input$MyEBirdData_in 
     if (is.null(inFile)) 
     return(NULL) 

     myData = read.csv(inFile$datapath) 

     df0 = data.frame(myData$Submission.ID, myData$Latitude, myData$Longitude) 
     df = unique(df0) 
     names(df)[2] = 'latitude' 
     names(df)[3] = 'longitude' 
     print(df) 
     return(df) 
    }) 

    output$myMap = renderLeaflet({ 
     if(is.null(my_table())) 
     { 
     return(leaflet() %>% addProviderTiles(providers$CartoDB.Positron)) 
     } 
     else 
     { 
     leaflet(data = my_table()) %>% addProviderTiles(providers$CartoDB.Positron) %>% addMarkers() 
     } 
    }) 
    } 
) 
+0

이 사용을 고려해야합니다 훌륭해 보이고 훨씬 더 의미가 있습니다. 감사합니다. 하지만, 내 코드에 그것을 작성하고 애플 리케이션을 실행하고 CSV를 업로드하려고하면 그것은 오류가 발생합니다 : 잘못된 인수 유형. 정말로 이것이 어디서/왜 일어날 지 확신하지 못합니까? – Heliornis

+0

안녕하세요, 내가 확인하지 않은 코드에 다른 문제가있을 수 있습니다. 내일 당신을 확인해 드리겠습니다. – Florian

+0

답변을 수정하면 문제를 해결할 수 있습니다. – Florian

관련 문제