Reputation: 1
I have the following app to upload and display a datafile:
library(shiny)
library(DT)
library(leaflet)
ui <- fluidPage(
titlePanel('SNP_Review'),
sidebarLayout(
sidebarPanel(
fileInput('target_upload', 'Choose file to upload', accept = c(
'text/csv',
'text/comma-separated-values',
'.csv'
)),
),
mainPanel(
tabsetPanel(
type = "tabs",
tabPanel('Samples_table', DT::dataTableOutput("sample_table")),
tabPanel('Samples_map', leafletOutput("sample_map"))
)
)
)
)
server <- function(input, output) {
df_products_upload <- reactive({
inFile <- input$target_upload
if (is.null(inFile))
return(NULL)
df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
return(df)
})
output$sample_table <- DT::renderDataTable({
df <- df_products_upload()
DT::datatable(df)
})
table_data <- reactive({
})
output$sample_map <- renderLeaflet({
leaflet(data = table_data()) %>%
addProviderTiles(providers$OpenStreetMap.DE) %>%
addMarkers(lng = ~Longitude, lat = ~Latitude)
})
}
shinyApp(ui = ui, server = server)
It prints out my data in a table. In the next step, I would like to show the GPS-data of the table on a map with leaflet. How can I define the reactive table_data() function after the table output to use the data in the map and in possible further operations? The table looks like this:
Name_ID Population Latitude Longitude
2 F6537_S01 Finland 60,745411 25,468228
3 N0484_S13 Norway 59,480365 7,920383
4 N0485_S25 Norway 60,925393 10,61182
5 N0486_S37 Norway 62,903547 9,824941
6 N0487_S49 Norway 61,78245 11,918215
Upvotes: 0
Views: 460
Reputation: 5003
This is exactly what reactive
functions are for. Reactive statements are lazy, that means they are only evaluated once until the are labeled invalid, which happens when a reactive statement within(such as input or another reactive function) changes value. As long as a reactive statement is calculated and not labeled invalide shiny will handle it as an object.
Therefor I would jus change your server code like this
server <- function(input, output) {
df_products_upload <- reactive({
inFile <- input$target_upload
if (is.null(inFile))
return(NULL)
df <- read.csv(inFile$datapath, header = TRUE,sep = input$separator)
return(df)
})
output$sample_table <- DT::renderDataTable({
df <- df_products_upload()
DT::datatable(df)
})
output$sample_map <- renderLeaflet({
leaflet(data = df_products_upload()) %>%
addProviderTiles(providers$OpenStreetMap.DE) %>%
addMarkers(lng = ~Longitude, lat = ~Latitude)
})
}
Upvotes: 1