[英]Delete specific row in r shiny
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: 为此,我首先使用以下方法初始化
shinyServer
函数内的数据框:
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
: 接下来,我有一个按钮和相应的代码来向
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. 为此,我有一个删除按钮(
input$delete.button
)和一个numericInput()
来选择行。 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. 我尝试了不同的尝试,包括
eventReactive()
但到目前为止没有任何工作。
Edit: This is the relevant part of my ui.r: 编辑:这是我的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
最好的,法比安
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 问候
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.