2017-03-19 9 views
2

アプリ内から光沢のあるアプリを再起動したいと思います。 global.Rのコードが再度実行されます(データを含むcsvファイルをリロードするため)。ここに私がしたいことを示す最小の例があります:アプリ内から光沢のあるアプリを再起動する(データを再読み込みする)

この光沢のあるアプリは、いくつかの座標データをロードし、地図上にマーカーをプロットします。マップに新しいマーカーが追加されると、新しい座標を古いデータに追加し、csvファイルとして保存する必要があります。その後、すべてのマーカーがマップに表示されるように、アプリケーションを再起動してdata.csvを再度読み込む必要があります。私はここから適応コードを試しました:Restart Shiny Sessionしかし、これは動作しません。アプリは再開しますが、csvファイルはリロードされません。

library(shinyjs) 
library(leaflet) 
library(leaflet.extras) 

jsResetCode <- "shinyjs.reset = function() {history.go(0)}" 

# data <- data.frame(latitude = 49, longitude = 13) 
data <- read.csv2("data.csv") # this should get executed whenever js$reset is called 

ui <- fluidPage(
    useShinyjs(),      
    extendShinyjs(text = jsResetCode), 
    leafletOutput("map") 
) 

server <- function(input, output, session){ 
    output$map <- renderLeaflet({ 
    leaflet(data) %>% addTiles() %>% 
     setView(11.5, 48, 7) %>% 
     addDrawToolbar() %>% 
     addMarkers() 
    }) 

    data_reactive <- reactiveValues(new_data = data) 

    # add new point to existing data and save data as data.csv 
    # after that the app should restart 
    observeEvent(input$map_draw_new_feature, { 
    data_reactive$new_data <- rbind(rep(NA, ncol(data)), data_reactive$new_data) 
    data_reactive$new_data$longitude[1] <- input$map_draw_new_feature$geometry$coordinates[[1]] 
    data_reactive$new_data$latitude[1] <- input$map_draw_new_feature$geometry$coordinates[[2]] 
    write.csv2(data_reactive$new_data, "data.csv", row.names = FALSE) 
    js$reset() # this should restart the app 
    }) 
} 

shinyApp(ui, server) 
+0

サンプルデータを提供できますか? – SBista

+0

あなたの質問に対する回答ではありませんが、同じ目的を達成する方法としてhttps://github.com/r-spatial/mapeditが好きかもしれません。 – timelyportfolio

答えて

2

サーバー内のファイルの読み込みを配置する必要があり、CSVファイルをリロードします。

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

    #Read the data inside the server!!! 
    data <- read.csv2("data.csv")# this should get executed whenever js$reset is called 

    output$map <- renderLeaflet({ 
     leaflet(data) %>% addTiles() %>% 
     setView(11.5, 48, 7) %>% 
     addDrawToolbar() %>% 
     addMarkers() 
    }) 

    data_reactive <- reactiveValues(new_data = data) 

    # add new point to existing data and save data as data.csv 
    # after that the app should restart 
    observeEvent(input$map_draw_new_feature, { 
     # browser() 
     data_reactive$new_data <- rbind(rep(NA, ncol(data)), data_reactive$new_data) 
     data_reactive$new_data$longitude[1] <- input$map_draw_new_feature$geometry$coordinates[[1]] 
     data_reactive$new_data$latitude[1] <- input$map_draw_new_feature$geometry$coordinates[[2]] 
     write.csv2(data_reactive$new_data, "data.csv", row.names = FALSE) 
     js$reset() # this should restart the app 
    }) 
    } 
+0

私の一日を保存しました!数時間のリファクタリングの後。この古いデータの問題で私は打撃を受けました。 –

関連する問題