【问题标题】:Shiny delete table row闪亮的删除表行
【发布时间】:2017-06-20 20:42:32
【问题描述】:

我想使用操作按钮删除表格的最后一行。我试图关注这个帖子Shiny: dynamically add/ remove textInput rows based on index 但我不知道如何将这个想法应用于我的特定案例。

一个最小的可重现示例

library(shiny)

ui <- fluidPage(
  sidebarPanel(numericInput("c1","Example", NA),
           actionButton("update", "Update Table"),
           br(),  br(),
           actionButton("reset", "Clear")
  ),

  mainPanel( tableOutput("example")
  )
)

server <- function(input, output, session) {

  # stores the current data frame, called by values() and set by 
  values(new_data_table)
  values <- reactiveVal(data.frame(A=1, B=2, C=3))

  # update values table on button click
  observeEvent(input$update,{

    old_values <- values()

    A_new <- input$c1
    B_new <- A_new + 2
    C_new <- A_new + B_new

    new_values <- data.frame(A=A_new, B=B_new, C=C_new)

    # attach the new line to the old data frame here:
    new_df <- rbind(old_values, new_values)

    #store the result in values variable
    values(new_df)

    #reset the numeric input to NA  
    updateNumericInput(session, "c1", "Example", NA)

  })
 #delete last row
   deleteEntry <- observeEvent(input$reset,{
                 #.... 
 })

  #Print the content of values$df
  output$example <- renderTable({  return(values())  })

}

shinyApp(ui = ui, server = server)

其实我不知道如何调用我的交互式数据框的最后一行。我尝试过类似 values()

编辑

按照下面的建议,我修改了 deleteEntry 函数,现在它可以工作了。

 ##delete last row
   deleteEntry <- observeEvent(input$reset,{
                 values( values()[-nrow(values()),])
 })

【问题讨论】:

  • values( values()[nrow(values())-1,]) 呢?
  • values(values()[-nrow(values()),])
  • 谢谢@HubertL,解决方案是values(values()[-nrow(values()),])。我已经编辑了代码,包括您的建议。问候。
  • 现在可以用了吗?
  • 是的,它按预期工作。

标签: r shiny


【解决方案1】:

要将 data.frame 的最后一行删除为 reactiveVal ,请使用以下语法:

values(values()[-nrow(values()),])

【讨论】:

  • reactiveValues 怎么样?我可以使用相同的语法吗?
  • my &lt;- reactiveValues(values=data.frame(A=1, B=2, C=3)) 一起使用my$values &lt;- my$values[-nrow(my$values),] 可能更具可读性。
猜你喜欢
  • 1970-01-01
  • 2016-01-28
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-11
  • 2021-09-23
相关资源
最近更新 更多