Hi,
Recently I've been trying to develop a Shiny App that uses 2 imported tables and creates a 3rd table that reflects the sum of the first two tables. In addition, I'm also trying to filter the imported tables by an input$choice from a selectInput expression.
The problem I'm currently running into is this line within the table modules:
data <- reactiveValues(table = test[test$num == reactive({input$var}),])
The code will run if I replace the reactive({input$var}) with an integer that exists within test$num, so I guess I'm just not sure how to call on the input$var value as an integer? Can anyone help me with this problem? The code is below and using an edited mtcars dataset from base R.
### Libraries
library(shiny)
library(dplyr)
library(DT)
### Data----------------------------------------
set.seed(0)
x <- data.frame(num = rep(c(1:5),2))
test <- cbind(mtcars, x)
module1 <- function(id) {
moduleServer(
id,
function(input, output, session) {
# initialise the reactive data object for the table
data <- reactiveValues(table = test[test$num == reactive({input$var}),])
# render the table
output$table <- renderDT({
datatable(data$table,
editable = TRUE)
})
# update the underlying data
observeEvent(input$table_cell_edit, {
data$table <- editData(data$table, input$table_cell_edit)
})
# return the data as a reactive
return(reactive(data$table))
}
)
}
module2 <- function(id) {
moduleServer(
id,
function(input, output, session) {
# initialise the reactive data object for the table
data <- reactiveValues(table = test[test$num == reactive({input$var}),])
# render the table
output$table <- renderDT({
datatable(data$table,
editable = TRUE)
})
# update the underlying data
observeEvent(input$table_cell_edit, {
data$table <- editData(data$table, input$table_cell_edit)
})
# return the data as a reactive
return(reactive(data$table))
}
)
}
module_add <- function(id, data_input_1, data_input_2) {
moduleServer(
id,
function(input, output, session) {
# do the calculations
data_table <- reactive({
data_input_1() + data_input_2()
})
# render the table
output$table <- renderDT({
datatable(data_table())
})
}
)
}
modFunctionUI <- function(id) {
ns <- NS(id)
selectInput(ns("var"), "Variable", choices = test$num)
dataTableOutput(ns("table"))
}
ui <- fluidPage(
modFunctionUI("table_1"),
modFunctionUI("table_2"),
modFunctionUI("table_3")
)
server <- function(input, output, session) {
# call the modules for the editable tables and store the results
amptable <- module1("table_1")
refreshtable <- module2("table_2")
# call the module for the table that takes inputs
# the reactives musn't be evaluated
module_add("table_3",
data_input_1 = amptable,
data_input_2 = refreshtable)
}
shinyApp(ui, server)