Raghavan vmvs
Raghavan vmvs

Reputation: 1265

generate right click and select custom menu R shiny JS automatically

I have created the following App using R Shiny

  library(shiny)
  library(rhandsontable)
  library(shinyjs)

ui <- fluidPage(  
sidebarLayout(sidebarPanel = "Inputparameter",
            numericInput(inputId = "Noi", label = "Row Count", value = 7, 0, max = 1000)),mainPanel (useShinyjs(),rHandsontableOutput(outputId = 'Adjusttable', width ='100%', height 
= '100%'),dataTableOutput(outputId = "Init_Tbl")))

server <- function(input, output, session) {    
DF <-reactive({
DF_Out<-data.frame(ID = 1:5,'Column2' = 0, Start = "D",FM="",stringsAsFactors = FALSE)
return(DF_Out)})

output$Adjusttable<-renderRHandsontable({  
input_Val<-input$Noi  
js_func<-paste("function (key, options) {this.alter('insert_row',[0],", 
input_Val,");this.render();}")
 ###
namestate<-paste("Add",input_Val, "rows at the bottom") 
output_Adjusttable<- DF() %>% head(5) %>% rhandsontable(width = 280, height = 677,stretchH = 
"all") %>%hot_context_menu(customOpts = list(insert_row = list(name = namestate,callback = 
htmlwidgets::JS(js_func))))
return(output_Adjusttable)}, quoted = FALSE )} 
shinyApp(ui, server)

the js_func line generates a right click option that adds extra rows. The number of extra rows is determined by the numericinput row count. Is it possible to automatically add the extra rows by the numericinput without the right click.

Upvotes: 2

Views: 104

Answers (1)

thothal
thothal

Reputation: 20389

This example, will add as many empty rows to the output as given by input$Noi. The idea is that you create an empty data.frame which has input$Noi rows and rbind it to your original data.frame:

library(shiny)
library(rhandsontable)

ui <- fluidPage(  
   sidebarLayout(sidebarPanel = "Inputparameter",
                 numericInput("Noi", "Row Count", 7, 0, 1000)),
   mainPanel(rHandsontableOutput("Adjusttable", "100%", "100%"),
             dataTableOutput("Init_Tbl"))
)
server <- function(input, output, session) {    
   DF <- reactive({
      DF_Out <- data.frame(ID = 1:5,
                           Column2 = 0, 
                           Start = "D",
                           FM = "")
      df_fill <- data.frame(ID = NA_integer_,
                            Column2 = NA_real_,
                            Start = NA_character_,
                            FM = NA_character_)[rep(1L, input$Noi), ]
      res <- rbind(DF_Out,
                   df_fill)
      rownames(res) <- NULL
      res
   })
   
   output$Adjusttable <- renderRHandsontable({  
      DF() %>% 
         rhandsontable(width = 280, 
                       height = 677,
                       stretchH = "all")
   })
} 
shinyApp(ui, server)

Upvotes: 1

Related Questions