【问题标题】:How do we duplicate existed attribute values with different attribute name in shinyapp?我们如何在 shinyapp 中复制具有不同属性名称的现有属性值?
【发布时间】:2021-01-13 18:49:34
【问题描述】:

如果有人可以帮助解决以下要求,那就太好了。

    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) 

需求详情:-

希望在名为“Category_provider”的新列下连接“Category/Provider”属性值的值,不幸的是它在 UI 表中显示属性名称而不是值。为了达到要求,我的代码中的修正是什么。

【问题讨论】:

    标签: r shiny shiny-server


    【解决方案1】:

    试试这个,

    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) 
    

    【讨论】:

      猜你喜欢
      • 2020-09-24
      • 2021-05-08
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多