1

I'm building an app that allows user to pass the value from selectizeInput or checkboxInput to form a dataframe. I've searched a while and found these sources that similar to what I expect:

  1. handsontable

It is from here: https://github.com/jrowen/rhandsontable. Mine is quite similar to this exampe:

shiny::runGitHub("jrowen/rhandsontable",
                 subdir = "inst/examples/rhandsontable_portfolio")

But I want to use shiny widgets to pass values to the dataframe. It should be able to add/remove rows as following example:

  1. shinyIncubator

code here:

library("shiny")
library('devtools')
install_github('shiny-incubator', 'rstudio')
library("shinyIncubator")

# initialize data with colnames
df <- data.frame(matrix(c("0","0"), 1, 2))
colnames(df) <- c("Input1", "Input2")
    server = shinyServer(
      function(input, output) {
        # table of outputs
        output$table.output <- renderTable(
          { res <- matrix(apply(input$data,1,prod))
          res <- do.call(cbind, list(input$data, res))
          colnames(res) <- c("Input 1","Input 2","Product")
          res
          }
          , include.rownames = FALSE
          , include.colnames = TRUE
          , align = "cccc"
          , digits = 2
          , sanitize.text.function = function(x) x      
        )  
      }
    )

ui = shinyUI(
  pageWithSidebar(

    headerPanel('Simple matrixInput example')

,
sidebarPanel(

  # customize display settings    
  tags$head(
    tags$style(type='text/css'
               , "table.data { width: 300px; }"
               , ".well {width: 80%; background-color: NULL; border: 0px solid rgb(255, 255, 255); box-shadow: 0px 0px 0px rgb(255, 255, 255) inset;}"
               , ".tableinput .hide {display: table-header-group; color: black; align-items: center; text-align: center; align-self: center;}"
               , ".tableinput-container {width: 100%; text-align: center;}"
               , ".tableinput-buttons {margin: 10px;}"
               , ".data {background-color: rgb(255,255,255);}"
               , ".table th, .table td {text-align: center;}"

    )
  )
  ,
  wellPanel(
    h4("Input Table")
    ,      
    matrixInput(inputId = 'data', label = 'Add/Remove Rows', data = df)
    , 
    helpText("This table accepts user input into each cell. The number of rows may be controlled by pressing the +/- buttons.")
  )
)
,
mainPanel(
  wellPanel(
    wellPanel(
      h4("Output Table")
      ,
      tableOutput(outputId = 'table.output')
      ,
      helpText("This table displays the input matrix together with the product of the rows of the input matrix")
    )
  )
    )
  )
)
runApp(list(ui = ui, server = server))

The value should be entered by user from shiny widgets such as selectizeInput, checkboxInput or textInput and passed to the dataframe once the user click my actionButton. What I want is pretty similar to the combination of the above functions but I don't know how to do. Any suggestions?

Many thanks in advance.

4
  • The simplest way is to directly use rhandsontable (you can edit, add and remove rows). Building it yourself is possible as your described using a combination of textInput and actionButton (in the server code, use observeEvent to observe click on the button, and then get values from the textInput and build your data frame) Commented Mar 31, 2016 at 20:46
  • I can pass value through shiny widgets now but still don't know how to add/remove rows. In addition, I need to fix the previous row after I add a new one. That is, if I press submit I will have my first row with default values which may be changed with the value of selectizeInput or textInput because now I only have one row. Then, after I submit a new row with new values, the value of the first row should be fixed, i.e., not gonna be changed with input because the second row will. Commented Apr 1, 2016 at 0:46
  • You need to have three different buttons: "insert", "update", and "delete". For the latter two, you also need an additional input to specify which row to update or delete. Commented Apr 1, 2016 at 2:11
  • @warmoverflow thanks. I think I find the solution:) Commented Apr 1, 2016 at 2:17

1 Answer 1

6

Though I ended up using none of the two packages, this worked fine:

library(shiny)

server = shinyServer(function(input, output, session){

  values <- reactiveValues()

values$DT <- data.frame(Name = NA,
                        status = NA,
                        compare = NA,
                        stringsAsFactors = FALSE)

newEntry <- observeEvent(input$addrow, {
    newLine <- c(input$textIn, input$boxIn, input$selectIn)
    values$DT <- rbind(values$DT, newLine)
})

newEntry <- observeEvent(input$revrow, {
  deleteLine <- values$DT[-nrow(values$DT), ]
  values$DT <- deleteLine
})

  output$table <- renderTable({
    values$DT
  })

})

ui = shinyUI(navbarPage(
  "Backtest System", inverse = TRUE, id = "navbar",
  tabPanel("Strategy",
           sidebarLayout(
             sidebarPanel(
               h4("Indicator"),
               textInput("textIn", "Text", "try"),
               checkboxInput("boxIn", "Box", TRUE),
               selectizeInput("selectIn", "Select", 
                              choices = c(">" = ">",
                                          ">=" = ">=",
                                          "<" = "<",
                                          "<=" = "<=")),
               actionButton("addrow", "Add Row"),
               actionButton("revrow", "Remove Row")
             ),
             mainPanel(
               tableOutput("table")
             )
           )
  )
  )
)
runApp(list(ui = ui, server = server))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.