Reputation: 406
I am currently programming a shiny app in which the user can add and delete lines to a data frame.
To do so, I first initialize the data frame inside the shinyServer
function using:
values <- reactiveValues()
values$df <- data.frame(v1 = NA,
v2 = NA,
v3 = NA,
v4 = NA,
v5 = NA,
v6 = NA,
v7 = NA,
v8 = NA
)
Next, I have a button and the corresponding code to add lines to the reactiveValue
:
newEntry <- observe({
if(input$add.button > 0) {
new.value <- isolate(input$v1 * input$v2 * input$v3 * input$v4)
newRow <- isolate(c(input$v1, input$v2, input$v3, input$v4,
input$v5, input$v6, input$v7, new.value))
isolate(values$df <- rbind(values$df, newRow))
}
})
So far, this code works perfect for me. Finally, I want to allow the user to remove the last row, or specific rows from the data frame. To do so, I have a delete button (input$delete.button
) and a numericInput()
to select the row. The logic I wanted to implement should be: if no specific row is selected, delete the last row, else delete the selected row. The code I tried so for is:
deleteEntry <- observe({
if(input$delete.button > 0) {
if(is.na(input$row.selection)){
values$df <- isolate(values$df[-nrow(values$df), ])
} else {
what does here?
}
}
})
So the first statement, deleting the last row works, but deleting a specific row always fails. I tried different attempts including eventReactive()
but nothing works so far.
Edit: This is the relevant part of my ui.r:
# Add button
actionButton(inputId = "add.button", label = "Add", icon = icon("plus")),
# Delete button
actionButton(inputId = "delete.button", label = "Delete", icon = icon("minus")),
# Row selection
numericInput(inputId = "row.selection", label = "Select row to be deleted", min = 1, max = 100, value = ""),
Any ideas to to implement this logic? Best, Fabian
Upvotes: 3
Views: 3189
Reputation: 5677
Just delete the specific element using
values$df <- isolate(values$df[-input$row.selection, ])
using some parts of your code I created this simple working example to show how to delete the spesific row.
runApp(list(
ui = shinyUI(
fluidPage(
sidebarLayout(
sidebarPanel(
# Add button
actionButton(inputId = "add.button", label = "Add", icon = icon("plus")),
# Delete button
actionButton(inputId = "delete.button", label = "Delete", icon = icon("minus")),
# Row selection
numericInput(inputId = "row.selection", label = "Select row to be deleted", min = 1, max = 100, value = "")
),
mainPanel(
verbatimTextOutput('text')
)
)
)
),
server = function(input, output, session) {
values <- reactiveValues()
values$df <- data.frame(v1 = NA,
v2 = NA,
v3 = NA,
v4 = NA,
v5 = NA,
v6 = NA,
v7 = NA,
v8 = NA
)
newEntry <- observe({
cat("newEntry\n")
if(input$add.button > 0) {
new.value <- isolate(input$v1 * input$v2 * input$v3 * input$v4)
newRow <- isolate(c(input$v1, input$v2, input$v3, input$v4,
input$v5, input$v6, input$v7, new.value))
isolate(values$df <- rbind(values$df,sample(1:8)))
}
})
deleteEntry <- observe({
cat("deleteEntry\n")
if(input$delete.button > 0) {
if(is.na(isolate(input$row.selection))){
values$df <- isolate(values$df[-nrow(values$df), ])
} else {
values$df <- isolate(values$df[-input$row.selection, ])
}
}
})
output$text = renderPrint({
values$df
})
}
)
If this is not what you want, please modify the code and edit your questions.
As an advice for future questions, please try always to provide a working minimal example that shows the problem or the desired feature. It will always simplifies the understanding of the problem and also provide a simple way to test if the solution works as you expected.
Regards
Upvotes: 2