Reputation: 719
I am trying to make an app where the user can add columns and rows to a table they can use to set rules to sort their data set stored in another object.
I built a reprex to isolate the problem. The UI successfully adds rows and columns to the table on clicking the add button. The additional rows work fine. However, the cells in the new columns can not be changed. You can enter text but not switch the cell. I am still learning the reactivity rules of Shiny, and suspect the issue is there. I tried adopting
dynamically add rows to rhandsontable in shiny and R
but with no avail.
library(shiny)
library(rhandsontable)
library(tidyverse)
ui <- fluidPage(
actionButton("addRow",
"Add"),
numericInput("rows",
"Number of Rows",
value = 0,
min = 0),
numericInput("colAnd",
"Number of And Cols",
value = 0,
min = 0),
numericInput("colBut",
"Number of But Cols",
value = 0,
min = 0),
rHandsontableOutput("hot")
)
server <- function(input, output) {
df <- reactive({
tibble(Topic = "Null",
And = "Null",
But = "Null")
})
df2 <- eventReactive(input$addRow,{
add_row_custom<- function (df, n, add = "null")
{
new <- data.frame(matrix(add, nrow = n, ncol = ncol(df)))
colnames(new) <- colnames(df)
rbind(df, new)
}
final <- hot_to_r(input$hot)
final %>%
add_row_custom(., input$rows ) %>%
cbind(., data.frame(matrix("null",
nrow = nrow(.),
ncol = input$colAnd + input$colBut)
)
)
})
df4 <- reactive({
if(input$addRow == 0){
df()
}else{
df2()}
})
output$hot <- renderRHandsontable(rhandsontable(df4()) %>%
hot_table(highlightRow = T,
highlightCol = T))
}
# Run the application
shinyApp(ui = ui, server = server)
Upvotes: 1
Views: 437
Reputation: 719
I walked away for 30 minutes after ramming my head into this for a few hours and went to the rhandsontable documentation.
Long story short, add the argument useType = F under the rhandsontable function.
Upvotes: 0