R Shiny Reactive: Object not found when table is imported from a txt file -
the following script runs fine if define data table within script "data.table(...)". larger data sets more comfortable import data text file using "read.table(...)".
unfortunately script not run when data imported text file. error message is: "warning: error in grepl: object 'material' not found"
the column "material" cannot identified in case. have idea how solve problem?
#this content of example_data.txt (sep. tabulator): #id londd latdd material application name color #1 20 60 stone 1 red #2 38 56 water,sand b 2 green #3 96 30 sand c 3 blue #4 32 31 wood d 4 yellow # filtermap library(data.table) library(shiny) library(dplyr) library(leaflet) #mydat <- data.table( id=c(1,2,3,4), # londd=c(20, 38, 96, 32), # latdd=c(60, 56, 30, 31), # material=c("stone", "water,sand", "sand", "wood"), # application=c("a","b","c","d")) mydat <- read.table("example_data.txt",header=true,sep="\t", encoding="utf-8") #set ui ui <- shinyui(fluidpage( sidebarpanel(h5("", width=2), checkboxgroupinput(inputid="matflag",label=h4("material"), choices=setnames(object=c("stone","water","sand", "wood"), nm=c("stone", "water", "sand", "wood")), ), checkboxgroupinput(inputid="appflag",label=h4("application"), choices=setnames(object=c("a","b","c","d"), nm=c("a","b","c","d")), ), position="left"), #app mainpanel content , styles mainpanel(fluidrow(leafletoutput(outputid="lmap"))) )) #set server server <- function(input, output){ #build leaflet map lmap <- leaflet(data=mydat)%>% addprovidertiles("stamen.tonerlite", options =providertileoptions(nowrap = true)) %>% fitbounds(~min(londd), ~min(latdd), ~max(londd), ~max(latdd)) #filter data datfilt <- reactive({ matsearch <- paste0(c('xxx',input$matflag),collapse = "|") matsearch <- gsub(",","|",matsearch) mydat[grepl(matsearch,material) & application %in% input$appflag] }) #add markers based on selected flags observe({ if(nrow(datfilt())==0) { print("nothing selected") leafletproxy("lmap") %>% clearmarkerclusters() } else { print("something selected") leafletproxy("lmap", data=datfilt()) %>% clearmarkerclusters() %>% addcirclemarkers(lng=~londd, lat=~latdd, clusteroptions=markerclusteroptions(), weight=3, color="#33cc33", opacity=1, fillcolor="#ff9900", fillopacity=0.8) } }) output$lmap <- renderleaflet(lmap) } #run app shinyapp(ui = ui, server = server)
Comments
Post a Comment