使用闪亮的下拉菜单编辑和保存数据表



我正在开发一个闪亮的应用程序,该应用程序创建一个要编辑的数据表,并全局保存为数据帧对象。

我面临的问题是:数据帧作为下拉菜单选项-name列。保存数据帧时,输出为as.character输出。以下示例:

library(shiny)
ui <- fluidPage(
DT::dataTableOutput("shinytest")
)
server <- function(input, output) {
df <- data.frame(Number = NA, 
Pages  = NA,
Name  = as.character(selectInput(paste0("sel", 1), "", choices = c("x","y"), width = "100px"))
)

rv <- reactiveVal(df)

output$shinytest <- DT::renderDataTable ({
DT::datatable(rv(), editable = TRUE, escape = FALSE, selection = 'none'
)

})

observeEvent(input$shinytest_cell_edit, {
info <- input$shinytest_cell_edit
newdf <- rv()
newdf[info$row, info$col] <- info$value
rv(newdf)
x <<- rv()
})
}
shinyApp(ui, server)

对于全局环境中的x:在列名下:<div class="form-group shiny-input-container" style="width:100px;">n <label class="control-label" id="sel1-label" for="sel1"></label>n <div>n <select id="sel1"><option value="x" selected>x</option>n<option value="y">y</option></select>n <script type="application/json" data-for="sel1" data-nonempty="">{"plugins":["selectize-plugin-a11y"]}</script>n </div>n</div>

基于@GyD 在这篇文章中发现的代码

如有任何协助,我们将不胜感激。

服务器内部创建了一个数据帧,selectInput作为名称列中的数据提供,这导致纯html在最终对象(x(中显示为文本。我将selectInput移到了ui中,这样就可以为该列选择一个名称,尽管我不知道selectInput是否要选择实际的列名,但这很容易纠正。

我添加了一个选择行数的输入和一个操作按钮,以避免在再次编辑输入时丢失所有数据。

library(shiny)
library(DT)
library(tidyverse)

ui <- fluidPage(
selectInput(inputId = 'selection1', "", choices = c("x","y"), selected = 'x', width = "100px"),
numericInput('number_of_rows', 'How many rows?',value = 5),
actionButton('update', 'Refresh Table'),
DTOutput("shinytest")
)
server <- function(input, output, session) {



df <- eventReactive(input$update, {tibble(Number = '', 
Pages  = '',
Name  = as.character(input$selection1),.rows = input$number_of_rows
)})

rv <- reactive(df())

output$shinytest <- renderDT({
datatable(rv(), editable = TRUE, escape = FALSE, selection = 'none')
})

observe({
newdf <<- df()
})

observeEvent(input$shinytest_cell_edit, {

info <- input$shinytest_cell_edit
newdf[info$row, info$col] <<- info$value

})
}
shinyApp(ui, server)

最新更新