2
votes

I am developing some geo-analytical software with R-script using the rgdal, and RSAGA addon's. I have recently found the shiny addon, and I am pleased with how easy it is to wrap my loop controller in a UI. Unfortunately, I am having issues with the reactive functions required make a checkbox group of values (those being individual ecodistricts) plot a reactive preview of the outline of each selected ecodistrict in the checkbox.

I have a checkboxGroupInput with an integer list of 110 ecodistricts (244, 278, 302, etc..) and an outputPlot handler for the reactive output.

To get the gist, the shape file is read to extract a list of ecodistricts for the checkbox group ('ed_all'), and also extract a shape with the outline of every ecodistrict ('xcheck'). In essense, I am trying to accomplish two things:

1 - make a reactive variable 'ed_list' which changes a list of integers as Ecodistricts are selected with the checkboxes

2 - To do a reactive polygon intersection using the reactive variable 'ed_list' and the 'xcheck' shape, in order to plot a reactive graph of the selected ecodistricts.

I am currently using rsaga.geoprocessor wrapped in a reactive command to do the intersection but i am open to suggestion if there is a better way. Every time i try to execute this code it gives me...

"Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive function.)"

or something to that effect.

Below is the clipped code relevant to my question:

    ui.r

    library(shiny)
    library(rgdal)

    setwd("D:\\BC_soil_map")

    ed <- readOGR(".\\BC_data\\BC_Shapes\\BC_soils\\Soil_Polygons_SLC", layer = "EcoDistricts_BC")
    ed_all <- ed$ECODISTRIC
    ed_null <- ed$EDnull

    shinyUI(pageWithSidebar(

    headerPanel(div(align = "center", 
              "Soil Mapping Data-Quilt Application")), 

    sidebarPanel(div(align = "center",  
                     h4("Select and EcoDistrict"),

                     checkboxGroupInput("ed_chkbx", 
                                  label = "",
                                  choices = ed_all,
                                  selected = ed_all))),

    mainPanel(div(align = "center",  
                     h4("Preview"),

                     plotOutput("preview", 
                                width = "500px", 
                                height = "500px"))

               ))

This is the server part of the code..

    server.R

    library(shiny)
    library(RSAGA)
    library(rgdal)

    setwd("D:\\BC_soil_map")

    ed <- readOGR(".\\BC_data\\BC_Shapes\\BC_soils\\Soil_Polygons_SLC", layer = "EcoDistricts_BC")

    xcheck <- subset(ed, select = c("ECODISTRIC","SHAPE_LENG","SHAPE_AREA"))

    ed_all <- ed$ECODISTRIC
    ed_null <- ed$EDNONE

    shinyServer(function(input, output, session) {

    x <- reactiveValues(input$ed_chkbx == TRUE)
    ed_list<- reactiveValuesToList(x, all.names = FALSE)

    previewInput <- reactive({
         rsaga.geoprocessor("shapes_polygons",
                   "Polygon Intersection",
                   list(SHAPES_A=xcheck),
                   FIELD_A="ECODISTRIC",
                   SHAPES_B=ed_list(),
                   SHAPES_AB= ##THIS IS USUALLY THE DIRECTORY FOR THE OUTPUT OF THE INTERSECTED FILE##
                   METHOD=1)})

    output$preview <- renderPlot(previewInput(), 
                           width = "150px", 
                           height = "150px", 
                           res = 72, 
                           env = parent.frame(),
                           quoted = FALSE, 
                           func = NULL)

    })
1

1 Answers

0
votes

The error message is saying that you are accessing a "reactive" element inside the ShinyServer function, but outside any of the reactive functions.

I have gotten similar errors when I tried to use reactiveValues in a similar fashion. (I am using shiny 0.5). I got around them by wrapping everything inside reactives in server.R.

Suggestion

Try commenting out the line that accesses input$ed_chkbx

x <- reactiveValues(input$ed_chkbx == TRUE)
ed_list<- reactiveValuesToList(x, all.names = FALSE)

If you move those 2 lines, inside the previewInput reactive function, the getReactiveEnvironment error should go away.

Hope that works for you.