1
votes

I'm trying to set up an interactive table in a Shiny app using rhandsontable, and I cannot edit the entries. In fact, I have to use the escape key to deselect cells once I have selected them, and the dropdown menu for the op column is missing. Here is the app.

library(rhandsontable)
library(shiny)

ui = fluidPage(rHandsontableOutput("equation"))

server = function(input, output){
  values = reactiveValues(equation = 
    data.frame(A = "A value", op = ">", B = "B value"))

  observe({
    if(!is.null(input$equation))
      values$equation = hot_to_r(input$equation)
  })

  output$equation = renderRHandsontable({
    rhandsontable(values$equation) %>%
      hot_col(col = "op", source = c(">", "<"))
  })
}

shinyApp(ui = ui, server = server)

Other notes:

  1. Eventually, I would like to add and remove rows interactively as well.
  2. I'm using this table to build an equation, so feel free to ignore this table stuff and tell me about a cool equation builder HTML widget.
1
Jeez, you mean there is nothing good for equations? - Mike Wise
Not that I've found. I hope I'm just ignorant. - landau
I have looked too - I usually end up using Microsoft Word equation editor, which sucks, but less so than trying to read my handwriting. Or trying to remember Latex which I always forget 15 minutes after I accomplish something. - Mike Wise
In this case, I'm just trying to input an equation as data to an R package while giving the user some guidance. Displaying it neatly would be cool, but it's not necessary here. - landau
I am not great at rhandsontable but will have a look when I get out of this meeting. :) - Mike Wise

1 Answers

1
votes

Not exactly clear on what you are trying to do here, but I think this solves a couple of problems.

  • You need to declare your levels when you create that op factor variable, and in general you have to be very explicit about the types of your columns in your dataframe for rhandsontable. Makes sense to construct it outside the reactiveValues call for that reason so you can inspect it beforehand.
  • The factor dropdown never drops below the grid, so you have to have enough room to display the factor dropdown. I inserted a few rows and selected the dropdown to illustrate this below.

Here is the adjusted code:

library(rhandsontable)
library(shiny)

ui = fluidPage(rHandsontableOutput("equation"))

server = function(input,output) {

  eqdf = data.frame(A_value = as.numeric(0),op = factor(c(">"),levels=c(">","<")),B_value = as.numeric(0))
  print(eqdf)
  values = reactiveValues(equation=eqdf)

  observe({
     req(input$equation)
     values$equation = hot_to_r(input$equation)
  })

  output$equation = renderRHandsontable({
    rhandsontable(values$equation) 
  })
}
shinyApp(ui = ui,server = server)

And here is what it looks like with a dropdown after I added two rows with a right-click:

enter image description here