Search code examples
rshinyrhandsontableselectinput

Updating rhandsontable from sliderInput


I am challenged with the various operations associated with rhandsontables, though they seem invaluable to the app I am trying to create. My first challenge is that I am trying to update the rhandsontable by filtering the original dataframe from user input, as selected from a dropdown (selectInput).

Here is a simplified reproducible example, that gets me the results I want, but not through the means I wish:

library(shiny)
library(rhandsontable)
library(tidyverse)

counties = as.factor(c(rep("County1", 2), rep("County2", 2)))
compnames = as.factor(c("comp1", "comp2", "comp3", "comp4"))
properties = 1:4
soilsData <- data.frame(county = counties, compname = compnames, property = properties)

ui <- fluidPage(
    
    selectInput(inputId = "selectCounty", label = h4("Select County"), 
                choices = counties,
                selected = "County1",
                multiple = FALSE),
    br(),
    br(),
    rHandsontableOutput('rTable'),
    
)

server <- function(input, output) {
 
    df1 <- df %>% 
        filter(county == "County1", compname == "comp1")
    
    datavalues <- reactiveValues(data = df1)

    output$rTable <- renderRHandsontable ({
        rhandsontable(datavalues$data,
                      rowHeaders = NULL)
    })
}

How do I create that df1 through the selected sliderInput?


Solution

  • You may want to use levels of counties for your choices in the ui.

    In addition, in server, you can use a reactive expression to filter your data. If you wish to react to changes in your selectInput (I assume you meant, instead of sliderInput) it should be in a reactive expression.

    When referencing, you will need to include parentheses. For example, if you call your reactive expression datavalues, then datavalues() should be used for data in rhandsontable.

    Please let me know if this is what you were looking for.

    library(shiny)
    library(rhandsontable)
    library(tidyverse)
    
    ui <- fluidPage(
      selectInput(inputId = "selectCounty", 
                  label = h4("Select County"), 
                  choices = levels(counties),
                  selected = "County1",
                  multiple = FALSE),
      br(),
      br(),
      rHandsontableOutput('rTable'),
    )
    
    server <- function(input, output) {
      
      datavalues <- reactive({
        soilsData %>% 
          filter(county == input$selectCounty, 
                 compname == "comp1")
      })
      
      output$rTable <- renderRHandsontable ({
        rhandsontable(datavalues(),
                      rowHeaders = NULL)
      })
      
    }