简体   繁体   中英

Editing and saving datatable with dropdown in shiny

I am working on a shiny app that creates a datatable to be edited and saved as a dataframe object globaly.

The problem i am facing is: the dataframe as a dropdowm menu option - the name column. When the dataframe is saved, the output is the as.character output. Example below:

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)

for x in the global environment: under the column name I get: <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>

Building on the code found in this post by @GyD

Any assistance is appreciated.

A dataframe was created inside the server with selectInput fed as data in name column, that caused that the pure html appeared as text in the final object (x). I moved the selectInput to the ui so that a name can be chosen for that column, although I didn't know if the selectInput is to select the actual column name but that is easily corrected.

I added an input to select the number of rows and an action button to avoid loosing all data if the inputs where edited again.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM