运行R闪亮应用程序时如何编辑数据表函数中的列名? [英] How to edit column names in datatable function when running R shiny app?
问题描述
我在 R Shiny 中使用来自 DT 包的数据表函数,我希望我的应用程序的用户可以编辑列名(变量名).有没有办法做到这一点?
I'm using datatable function from DT package in R Shiny and I want that the user of my app can edit the column names (the variable names). Is there any option to do that?
现在我使用文本输入old_var_name"、文本输入new_var_name"和操作按钮update_variable_name".但在这一点上,我当时只能更改变量名称.我希望用户能够随心所欲地更改变量名称.
For now I'm using a text input "old_var_name", a text input "new_var_name" and an actionbutton "update_variable_name". But at this point, I'm only able to change on variable name at the time. I want the user to be able to change as much as variable names he wants.
服务器:
tab <- eventReactive(input$import,{
inFile <- input$file1
if (is.null(inFile))
return(NULL)
tabledata <- read.xlsx(inFile$datapath,startRow=1,sheet = 1)
})
name_temp <- eventReactive(input$var_name,{
if (input$old_name == ""){
colnames(tab())
} else {
c(colnames(tab())[1:(which(colnames(tab()) == input$old_name)-1)],input$new_name,
colnames(tab())[(which(colnames(tab()) == input$old_name)+1):length(colnames(tab()))])
}
})
final_rename <- reactive({
d <- tab()
colnames(d) <- name_temp()
d
})
output$tabledata <- DT::renderDataTable({
if (input$var_name == 0) {
DT::datatable(tab(),editable = T)
} else {
DT::datatable(final_rename(),editable = T)
}
})
用户界面:
tabPanel("Table",h1("Table",align="center") ,
actionButton(inputId = "import", label = "Import data"),br(),br(),
splitLayout(textInput(inputId = "old_name", label = "Old variable name"),
textInput(inputId = "new_name", label = "New variable Name")),
actionButton(inputId = "var_name", label = "Update Variable name"),br(),br(),
DT::dataTableOutput("tabledata"))
是否有任何建议可以实现该目标或我可以使用数据表的任何选项,然后用户将能够更改他想要的所有变量名称?
Is there any suggestion to achieve that or any option with datatable that I can use and then the user will be able to change all variable names he wants?
推荐答案
这是一个带有上下文菜单的解决方案.右键单击列标题以对其进行编辑.完成后按Escape",或者只需将鼠标移出文本输入框即可.此解决方案不干扰排序.
Here is a solution with a context menu. Right-click on a column header to edit it. Press 'Escape' when done, or simply move the mouse outside the text input box. This solution does not interfere with the sorting.
library(shiny)
library(DT)
callback <- c(
"$.contextMenu({",
" selector: '#table th',",
" trigger: 'right',",
" autoHide: true,",
" items: {",
" text: {",
" name: 'Enter column header:',",
" type: 'text',",
" value: ''",
" }",
" },",
" events: {",
" show: function(opt){",
" $.contextMenu.setInputValues(opt, {text: opt.$trigger.text()});",
" },",
" hide: function(opt){",
" var $this = this;",
" var text = $.contextMenu.getInputValues(opt, $this.data()).text;",
" var $th = opt.$trigger;",
" $th.text(text);",
" }",
" }",
"});"
)
ui <- fluidPage(
tags$head(
tags$link(rel = "stylesheet", href = "https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.8.0/jquery.contextMenu.min.css"),
tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/jquery-contextmenu/2.8.0/jquery.contextMenu.min.js")
),
DTOutput("table")
)
server <- function(input, output){
output[["table"]] <- renderDT({
datatable(iris[1:3,], callback = JS(callback))
}, server = FALSE)
}
shinyApp(ui, server)
这篇关于运行R闪亮应用程序时如何编辑数据表函数中的列名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!