gpt4 book ai didi

r - 如何使用 DT 包编辑和保存对 Shiny 数据表所做的更改

转载 作者:行者123 更新时间:2023-12-03 19:43:54 24 4
gpt4 key购买 nike

我想编辑表格并将更改保存到下面的示例中。这是我到目前为止所拥有的。

library(shiny)
library(DT)

Admit<-c("Admitted","Rejected","Admitted", "Rejected", "Admitted", "Rejected", "Admitted",
"Rejected","Admitted", "Rejected", "Admitted","Rejected","Admitted", "Rejected","Admitted","Rejected", "Admitted", "Rejected",
"Admitted","Rejected", "Admitted" ,"Rejected","Admitted", "Rejected")
Gender<-c("Male","Male","Female","Female", "Male", "Male", "Female", "Female", "Male","Male","Female","Female",
"Male","Male","Female","Female","Male", "Male", "Female", "Female","Male","Male","Female","Female")
Dept<-c( "A","A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F")
Freq<-c("512", "313", "89", "19", "353", "207", "17", "8", "120", "205", "202", "391", "138", "279", "131", "244", "53", "138",
"94", "299", "22", "351", "24", "317")

banking.df<-data.frame(Admit,Gender,Dept, Freq)


# Define UI ----
ui <- fluidPage(
titlePanel("UC Berkley Admissions"),

mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("Sample Bank", DT::dataTableOutput("banking.df"),
br(),
actionButton("saveBtn","Save")))))

# Define server logic ----
server <- function(input, output) {
d1 = banking.df
d1$Date = Sys.time() + seq_len(nrow(d1))

output$banking.df=render_dt(d1,'cell')

#edit a single cell
proxy5=dataTableProxy('banking.df')
observeEvent(input$banking.df_cell_edit,{
info=input$banking.df_cell_edit
str(info)
d1<<-editData(d1,info)
replaceData(proxy5,d1,resetPaging = FALSE)
})

#observeEvent(input$saveBtn,write.csv(input$banking.df),
# file="NewData.csv",row.names=FALSE)
}



# Run the app ----
shinyApp(ui = ui, server = server)

我能够编辑表格,它将存储更改。但是,每当我尝试对特定列进行新的编辑时,它都不会保存更改并将其保留为空白。例如,如果我在部门输入“H”。不允许更改的列。我怎样才能解决这个问题?
另外,如何使用“保存按钮”来覆盖banking.df?
谢谢你。

最佳答案

我尝试了您的代码并修改了一些元素。有几点需要明确:

  • 您可以使用 DT 属性 editable=TRUE。
  • 要下载为csv或excel,您可以使用DT默认的CSV,Excel按钮。它可以存储编辑过的数据。
  • 如果你想使用单独的按钮下载,你可以简单地使用 write.csv() 。

  • 代码:
    用户界面
    library(shiny)
    library(DT)

    fluidPage(
    titlePanel("UC Berkley Admissions"),

    mainPanel(
    tabsetPanel(
    id = 'dataset',
    tabPanel("Sample Bank",

    DT::dataTableOutput("banking.df_data"),
    br(),
    actionButton("viewBtn","View"),
    br(),
    actionButton("saveBtn","Save"),
    br(),
    DT::dataTableOutput("updated.df")
    ))))

    服务器端
    Admit<-c("Admitted","Rejected","Admitted", "Rejected", "Admitted", "Rejected", "Admitted",
    "Rejected","Admitted", "Rejected", "Admitted","Rejected","Admitted", "Rejected","Admitted","Rejected", "Admitted", "Rejected",
    "Admitted","Rejected", "Admitted" ,"Rejected","Admitted", "Rejected")
    Gender<-c("Male","Male","Female","Female", "Male", "Male", "Female", "Female", "Male","Male","Female","Female",
    "Male","Male","Female","Female","Male", "Male", "Female", "Female","Male","Male","Female","Female")
    Dept<-c( "A","A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "D", "D", "D", "D", "E", "E", "E", "E", "F", "F", "F", "F")
    Freq<-c("512", "313", "89", "19", "353", "207", "17", "8", "120", "205", "202", "391", "138", "279", "131", "244", "53", "138",
    "94", "299", "22", "351", "24", "317")

    banking.df<-data.frame(Admit,Gender,Dept, Freq,stringsAsFactors = FALSE)
    d1 = banking.df
    d1$Date = Sys.time() + seq_len(nrow(d1))

    function(input, output) {


    output$banking.df_data<-renderDataTable(
    d1,selection = 'none', editable = TRUE,
    rownames = TRUE,
    extensions = 'Buttons',

    options = list(
    paging = TRUE,
    searching = TRUE,
    fixedColumns = TRUE,
    autoWidth = TRUE,
    ordering = TRUE,
    dom = 'Bfrtip',
    buttons = c('csv', 'excel')
    ),

    class = "display"
    )


    observeEvent(input$banking.df_data_cell_edit, {
    d1[input$banking.df_data_cell_edit$row,input$banking.df_data_cell_edit$col] <<- input$banking.df_data_cell_edit$value
    })

    view_fun<-eventReactive(input$viewBtn,{
    if(is.null(input$saveBtn)||input$saveBtn==0)
    {
    returnValue()
    }
    else
    {
    DT::datatable(d1,selection = 'none')
    }

    })


    observeEvent(input$saveBtn,{
    write.csv(d1,'test.csv')
    })

    output$updated.df<-renderDataTable({
    view_fun()
    }
    )
    }

    希望这可以帮助。
    enter image description here

    关于r - 如何使用 DT 包编辑和保存对 Shiny 数据表所做的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58812512/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com