Shiny: Allowing input of custom formulas in Tables

I am trying to set up a table, where in every row there are variable names, and the user should be able to define a formula based on them.

Below is my attempt with orderInput.

Issues:

A) How to use a reset button? When I try to define the formula for the next row, I cannot delete the old one.

B) when I use " as_source = T" in the foo orderInput, the orderInputupdate does not work any more.

Any other solutions, apart from orderInput, are more than welcome.

Many great thanks!

library(DT)
library(shiny)
library(shinyjqui)
library(dplyr)

some_vars = head(mtcars, 8) %>% select(am, gear, carb) %>% mutate(
  am = paste0("v", am),
  gear = paste0("x", gear),
  carb = paste0("y", carb),
  formula = NA
)

ui <- basicPage(
  h2("build a formula"),
  orderInput(
    "ops",
    "Operators",
    c("~", "+", "*"),
    as_source = T,
    connect = "formula",
    item_class = "default"
  ),
  orderInput(
    "foo",
    "foo",
    items = month.abb[1:3],
    connect = "formula",   
    item_class = 'info'
  ),
  orderInput(
    "formula",
    "Formula",
    items = NULL,
    as_source = F,
    connect = NULL,
    placeholder = "(build your formula)"
  ),
  verbatimTextOutput("myFormula"),
  actionButton("forml", "define the formula for the selected row"),
  actionButton("get_forml", "Get the formula for the selected row"),
  
  
  h2("The variables table"),
  DT::dataTableOutput("mytable")
)

server <- function(input, output, session) {
  
  df=reactiveVal(some_vars)
  
  output$mytable = DT::renderDataTable({
    DT::datatable(df(),
                  selection = "single")
  })
  
  observeEvent(input$forml, {
   
    updateOrderInput(session,
                     "foo",
                     items =
                       unname(as.character(some_vars[input$mytable_rows_selected, 1:3])),
                     item_class = "success")
    
    
  })
  
  output$myFormula <- renderPrint({
    paste0(input$formula, collapse = " ")
  })
  
  observeEvent(input$get_forml, {
               temp=isolate(df())
              temp[input$mytable_rows_selected,"formula"]= paste0(input$formula, collapse = " ")
              
  df(temp)
})
}

shinyApp(ui, server)

Hello, it seems like you have omitted server code ?

Oops! My bad!
I updated my original post to include also the "server".
Many thanks for your attention.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.