I'm trying to create a shinny app with a default rhandsontable table, then the user has two options to update it:
- completing directly by hand on the rhandsontable table or
- searching the data from a database using a number input and an
actionButton
My code here:
library(shiny)
library(rhandsontable)
library(data.table)
data_pop <- data.table(`Number` = c(1, 1, 2, 2, 3, 3),
`Country` = c("Andorra", "Angola", "Anguilla", "Antigua and Barbuda", "Argentina", "Armenia"),
`Population` = c(123, 456 , 789, 246 , 369 , 159))
ui <- navbarPage(title = tags$b(tags$span("APP")), id = "nav", fluid = TRUE,
tabPanel(tags$b(tags$span("Model")),
sidebarLayout(
sidebarPanel(
numericInput("number", label = "Number", value = 0, min = 0, max = Inf),
actionButton("load", "Load", align = 'right'),
),
mainPanel(
rHandsontableOutput("out_table"),
)
)
)
)
server <- function(input, output) {
default <- data.table(`Country` = "",
`Column` = as.integer(0),
stringsAsFactors = FALSE)
values <- reactiveValues()
observe({
if (!is.null(input$table)) {
table <- hot_to_r(input$out_table_country)
} else {
if (is.null(values[["table"]])) {
default <- eventReactive(input$load, {
data_pop[`Number` == input$number, .(`Country`, `Population`)]
})
table <- default
}
else {
table <- values[["table"]]
}
}
values[["table"]] <- table
})
output$out_table <- renderRHandsontable({
table <- values[["table"]]
if (!is.null(table))
rhandsontable(table, stretchH = "all") %>%
hot_col(col = "Country", type = "autocomplete", source = data_pop$Country, strict = T) %>%
hot_col(col = "Column", format = "0,0")
})
}
shinyApp(ui = ui, server = server)
The problem appears when I try to replace the default
table with the with the reactive one, I get:
Error in as.vector: cannot coerce type 'closure' to vector of type 'list'
I don't know if it's related to the hot_col
format on columns...
I've tried a lot of solutions but nothing works yet, any help would be very much appreciated!
Thanks a lot,
Fabian