R 如何在shinyapp中使用不同的属性名称复制现有属性值?

R 如何在shinyapp中使用不同的属性名称复制现有属性值?,r,shiny,shiny-server,R,Shiny,Shiny Server,这将是伟大的,有人可以帮助以下要求 url <- "https://bbolker.github.io/mpha_2019/gapminder_index.csv" dt <- fread(url) # UI ui <- fluidPage( sidebarLayout( sidebarPanel( textInput("newcolumnname", "Custom

这将是伟大的,有人可以帮助以下要求

    url <-  "https://bbolker.github.io/mpha_2019/gapminder_index.csv"
dt <-   fread(url)

# UI
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
                textInput("newcolumnname", "Custom Attribute Name"),
                selectInput("formula", "Enter Custom Formula", choices = unique(names(dt)), multiple = TRUE),
                actionButton("addnewcolumn", "Add new column")
                ),
    mainPanel(
      DT::DTOutput("data_tbl")
    )
  )
)
#SERVER
server <- function(input, output, session) {
  reactive_dt <- eventReactive(input$addnewcolumn, {
    if (input$newcolumnname != "" &&
        !is.null(input$newcolumnname) && input$addnewcolumn > 0) {
      #newcolval <- dt$input$formula
      newcolval <- dt[,input$formula]
      newcol <- data.table(newcolval)
      names(newcol) <- input$newcolumnname
      dt <<- cbind(dt, newcol)
    }
    dt
  })
  
  output$data_tbl <- DT::renderDT({ head(reactive_dt(),5) })
  }
#Run the Shiny App to Display Webpage
shinyApp(ui = ui, server = server) 
url试试这个

url <-  "https://bbolker.github.io/mpha_2019/gapminder_index.csv"
dt <-   as.data.frame(fread(url))

# UI
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
                textInput("newcolumnname", "Custom Attribute Name"),
                selectInput("formula", "Enter Custom Formula", choices = unique(names(dt)), multiple = TRUE),
                actionButton("addnewcolumn", "Add new column")
            
                ),
    mainPanel(
      DT::DTOutput("data_tbl")

    )
  )
)
#SERVER
server <- function(input, output, session) {
  reactive_dt <- eventReactive(input$addnewcolumn, {
    if (input$newcolumnname != "" &&
        !is.null(input$newcolumnname) && input$addnewcolumn > 0) {

        newcol <- apply(dt[,input$formula] , 1, function(x) paste(x, collapse = "_"))

        cn <-colnames(dt)
        dt <<- data.frame(dt, newcol)
        colnames(dt) <- c(cn,input$newcolumnname)
    }
    dt
  })
  
  output$data_tbl <- DT::renderDT({ head(reactive_dt(),5) })
  }
#Run the Shiny App to Display Webpage


shinyApp(ui = ui, server = server) 
url