首页 > 解决方案 > 反应表:在闪亮中隐藏观察和编辑

问题描述

我正在创建一个响应式闪亮应用程序,我必须在其中编辑数据框并保存它。但在编辑部分,首先我想隐藏除选定行之外的所有其他观察。我想编辑它并再次选择一些观察并编辑它并再次保存等等。

我创建了一个示例数据,在这里我选择了一个国家并仅显示该国家的观察结果。我想编辑价格列,它应该在整体数据中更新,我需要保存它。然后稍后当我选择一个国家时,它会从编辑的数据中获取数据。并编辑它自动保存并继续

rm(list = ls())


library(shiny)
library(shinydashboard)


#--------------------------------

df <- data.frame("country" =c("Russia", "China", "US", "India", "UK" )
                 , "tv_cost" = c(43, 67, 78, 56, 78 )
                 , "fridge_cost" = c(78, 45, 56, 78, 47)
                 , "microwave" = c(44, 23,31,29, 34)
                 )


#Shiny Dashboard

ui <- dashboardPage(skin = "green"
                    , dashboardHeader(title = "Edit Table")
                    , dashboardSidebar(sidebarMenu(menuItem("Edit Data", tabName = "tab_01" )
                                                   , menuItem("Output", tabName = "tab_02")
                                                   , menuItem("Input", tabName = "tab_03")
                                                   ))
                    , dashboardBody(tabItems(tabItem(tabName = "tab_01"
                                                     , selectInput(inputId = "countryid", label = "SELECT COUNTRY" , choices = df$country )
                                                     , DT::dataTableOutput("table1"))
                                             , tabItem(tabName = "tab_02", DT::dataTableOutput("table2"))
                                             , tabItem(tabName = "tab_03", DT::dataTableOutput("table3"))
                                             ))
                    )#dashboardpage ends here

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

  #Creating a blank Data frame to update every change 
  df_02 <- reactive(df_03 <-  NULL)


  #Showing Data table, for editing, "I'm filtering data here but I have to hide other observation "
  df_04 <- reactive(df %>% 
                      filter(country == input$countryid )) 


  output$table1 <-  renderDT({df_04()})

  # saving the edit data

  #Observing edited table 

  #updating it edited frame df_02

  #Rendering overall edited df. 
  output$table2 <- renderDT({df_02()})

  #input file
  output$table3 <- renderDT({df})



}

shinyApp(ui, server)

提前致谢

标签: rshinyreactive-programmingreactivedt

解决方案


推荐阅读