1

My plan is to import a large, raw dataset called rawdata and then use a sourced scripts.R file to do some munging on the dataset. The munged data is passed back to server.R as a list of several dataframes created by tidyr::gather named heap.

Next, I want to display a ggplot of heap[[2]] through heap[[10]].

Using the example plot interaction on the Shiny website, I want to be able to brush away points that are obviously outliers from the graph. BUT, I'm struck with a pesky reactive context error that I can't debug. I'm fairly certain that it involves lines 77 to 80:

vals.temp <- reactiveValues(
if(!is.null(heap())) {
  keeprows = rep(TRUE, nrow(heap()[[2]]))
})

In the example provided on the Shiny website, it uses the mtcars dataset, but mine is a dataset reactive based on user input. I suspect there's a disconnect there somewhere.

Link to files: Dropbox

Thanks so much for your expertise!

1 Answer 1

1

You can declare an empty list vals <- reactiveValues() and then you can add to it an element some <- reactive({ ... vals$keeprows <- nrow...}).

In the example below plotOutput('figure') is interactive now. I used mentioned example from shiny gallery.

Crucial part of the server script:

output$AddCustom <- renderUI(
    AddCustomInputs()
  )


  # Based on data() variables column, populate a selectInput called "subset" with variable data
  output$selector <- renderUI({
    selectInput('subset', 'Graph Parameter', choices = levels(heap()[[1]]$variable))
  })

  # Changes from here: ----------------------------------------------------------------------

  vals <- reactiveValues() # keeprows = rep(TRUE, 100000 )

  # Subset the data() set by the value selected in the "subset" selectInput and save it as df()
  df <- reactive({

    vals$keeprows <- rep(TRUE, nrow(heap()[[1]][heap()[[1]]$variable %in% input$subset, ]))

    return(heap()[[1]][heap()[[1]]$variable %in% input$subset, ])
  })


  observeEvent(input$exclude_toggle, {
    res <- brushedPoints( df(), input$figure_brush, allRows = TRUE)

    vals$keeprows <- xor(vals$keeprows, res$selected_)
  })

  observeEvent(input$exclude_reset, {
    vals$keeprows <- rep(TRUE, nrow( df() ))
  })


  # create a plot based on df()
  output$figure <- renderPlot({

    keep <- df()[ vals$keeprows, , drop = FALSE ] 

    if(!is.null(df())) {
      plot <- isolate(
        ggplot(data = na.omit(keep), aes(x = NST_DATI, y = value)) + 
          geom_line() + 
          labs(x = "Date", y = input$subset, title = paste(input$subset, "vs. Date"))
      )
      return(plot)
    }
  })
  output$table <- renderDataTable(
    if(!is.null(rawdata())) {
      return(rawdata())
    }
  )
})

ui script:

shinyUI(fluidPage(
    titlePanel("Data Fix-it"),
    sidebarLayout(
      sidebarPanel(
        fileInput('rawfile', 'Input *.CSV'),
        helpText("Files downloaded from ADRS Console must be saved as *.csv for import."),
        h4("Other Parameters"),
        tags$body("Only the 'Big 7' parameters will be retained, unless specified."),
        br(),
        checkboxInput('AddCustomCheck', 'Add custom parameters'),
        uiOutput('AddCustom'),
        hr(),
        numericInput('sequnceminutes', 'Water Quality Interval (mins)', value = 60),
        actionButton('groomgo', 'Groom Data'),
        textOutput('linesaltered'),
        hr(),
        downloadButton('downloadcsv', 'Download *.csv file')
      ),
      mainPanel(
        tabsetPanel(
          tabPanel("Plot",
                   uiOutput('selector'),
                   plotOutput('figure', brush = brushOpts(id = "figure_brush")),

                   actionButton("exclude_toggle", "Toggle points"),
                   actionButton("exclude_reset", "Reset")
          ),
          tabPanel("Table",
                   dataTableOutput('heaptable')),
          tabPanel("Report",
                   actionButton('MakePlotsgo', 'Make Plots'),
                   plotOutput('heaptemp'),
                   plotOutput('heapph'),
                   plotOutput('heapcond'),
                   plotOutput('heaptds'),
                   plotOutput('heapdomgl'),
                   plotOutput('heapdosat'),
                   plotOutput('heapturb'),
                   plotOutput('heapflow'),
                   plotOutput('heapcustom1'),
                   plotOutput('heapcustom2'),
                   plotOutput('heapcustom3')
          )
        )
      )
    )))

I hope this is what you wanted :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for going above-and-beyond.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.