Reputation: 8404
I have the shiny app below which by default displayes a dataset via table. Then in the sidebar there are 5 widgets which correspond to the 5 columns of the table. Then there is one actionbutton.
Add: If the user presses Add
then he adds a new row to the table with the selected widget values.
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
Input<-structure(list(`Security Type` = c("Stock", "Stock", "Load Fund"
), Ticker = c("XOM", "NFLX", "AMCPX"), `Purchase Date` = structure(c(16070,
17084, 17084), class = "Date"), `Sale Date` = structure(c(18627,
NA, 18545), class = "Date"), `Amount Invested` = c("$10,000",
"$8,000", "$10,000")), class = c("spec_tbl_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -3L))
shinyApp(
ui = tags$body(class="skin-blue sidebar-mini control-sidebar-open",dashboardPage(
options = list(sidebarExpandOnHover = TRUE),
header = dashboardHeader(title = "Investment Advisor Monitoring - Insider Trading",titleWidth = 450),
sidebar = dashboardSidebar(minified = F, collapsed = F,
selectInput("sectype", "Security Type",
c(unique(Input$`Security Type`))),
selectInput("sectick", "Ticker",
c(unique(Input$Ticker))),
dateInput("PurDate", "Purchase Date", value = as.Date("2013-12-31")),
dateInput("selDate", "Sale Date", value = as.Date("2019-01-31")),
selectInput("aminv", "Amount Invested",
c(unique(Input$`Amount Invested`))),
actionButton("add","Add")
#actionButton("edit","Edit"),
#actionButton("del","Delete")
),
body = dashboardBody(
h3('Results'),
tabsetPanel(id = "tabs",
tabPanel("InsiderTraining",
dataTableOutput("TBL1")
)
)
),
controlbar = dashboardControlbar(width = 300
),
title = "DashboardPage"
)),
server = function(input, output) {
output$TBL1<-renderDataTable(
datatable(Input) )
}
)
Upvotes: 0
Views: 394
Reputation: 10627
You can create the reactive value called data
which gets updated whenever the button was clicked. Then you can use shiny::isolate
to freeze the current value of the input into the table:
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(DT)
Input <- structure(list(`Security Type` = c("Stock", "Stock", "Load Fund"), Ticker = c("XOM", "NFLX", "AMCPX"), `Purchase Date` = structure(c(
16070,
17084, 17084
), class = "Date"), `Sale Date` = structure(c(
18627,
NA, 18545
), class = "Date"), `Amount Invested` = c(
"$10,000",
"$8,000", "$10,000"
)), class = c(
"spec_tbl_df", "tbl_df", "tbl",
"data.frame"
), row.names = c(NA, -3L))
shinyApp(
ui = tags$body(class = "skin-blue sidebar-mini control-sidebar-open", dashboardPage(
options = list(sidebarExpandOnHover = TRUE),
header = dashboardHeader(title = "Investment Advisor Monitoring - Insider Trading", titleWidth = 450),
sidebar = dashboardSidebar(
minified = F, collapsed = F,
selectInput(
"sectype", "Security Type",
c(unique(Input$`Security Type`))
),
selectInput(
"sectick", "Ticker",
c(unique(Input$Ticker))
),
dateInput("PurDate", "Purchase Date", value = as.Date("2013-12-31")),
dateInput("selDate", "Sale Date", value = as.Date("2019-01-31")),
selectInput(
"aminv", "Amount Invested",
c(unique(Input$`Amount Invested`))
),
actionButton("add", "Add")
),
body = dashboardBody(
h3("Results"),
tabsetPanel(
id = "tabs",
tabPanel(
"InsiderTraining",
dataTableOutput("TBL1")
)
)
),
controlbar = dashboardControlbar(width = 300),
title = "DashboardPage"
)),
server = function(input, output) {
# Init with some example data
data <- reactiveVal(Input)
observeEvent(
input$add,
{
# start with current data
data() %>%
add_row(
`Security Type` = isolate(input$sectype),
Ticker = isolate(input$sectick),
`Purchase Date` = isolate(input$PurDate),
`Sale Date` = isolate(input$selDate),
`Amount Invested` = isolate(input$aminv)
) %>%
# update data value
data()
}
)
output$TBL1 <- renderDataTable(
datatable(data())
)
}
)
Upvotes: 1