I'm trying to create an interactive calculator using R Markdown and Shiny. The calculator will sum car prices, from an imported table, based on whether check boxes are triggered. I've created a basic shell for the calculator, but I'm stuck not knowing how to reference the check boxes.
I want my checkboxes to be in line with my table, and wasn't able to use checkBoxGroupInput to do so. I did, however, find this recent SO thread which suggests I can use cbind to combine (what looks to be) an HTML vector of checkboxes with a datatable:
---
output: html_document
runtime: shiny
---
### Select car(s)
```{r, echo=FALSE}
## import car data
data <- head(mtcars[1:6])
data$id <- seq(1:nrow(data))
## generate random car prices
set.seed(1)
data$price <- round(runif(nrow(data),5000,15000),0)
## create table w/ checkboxes
output$mytable = renderDataTable({
addCheckboxButtons <- paste0('<input id="table1" type="checkbox" name="row', data$id, '" value="op', data$id, '">',"")
#Display table with checkbox buttons
z <- cbind(Pick=addCheckboxButtons, data)
z
})
## display table
dataTableOutput({"mytable"})
```
This code above works fine and produces the table I want. I get stuck, however, as I'm unsure how to now reference my checkbox vector. I know I need to use a reactive statement to do my summation, but I'm not exactly sure how to do reference the check boxes to do so. I imagine the last piece of code will look something like:
### Total bill
```{r, echo=FALSE}
my_sum <- reactive({z$price[z$Pick=="TRUE"]})
renderText(my_sum)
```
The code blows up as the data$Pick
part doesn't work. The error I get is:
Error: argument 1 (type 'closure') cannot be handled by 'cat'
(As I understand it, renderText is passing the first argument to cat
, which does not know what to do with the function.)
Can someone please help me reference the checkboxes I've created?