在Shiny中添加一列

huangapple go评论66阅读模式
英文:

adding a column in shiny

问题

I would like to add an ID column to a data set using Shiny. And this ID column can be as long as the dataset dat$my.ID <- (max(dat$ID) + 1):(max(dat$ID) + nrow(dat)) and should not limit the user who is entering a starting ID number. many thanks in advance.

ui.r

navbarPage(
  title = 'Benvenuti',
  tabPanel('read.me', column(3,
                             
                             h3("Daily Data Manipulation"),
                          
                             h3("title1"),
                             tags$a("Upload data.xlsx")
  )),
  
  tabPanel(title = "title2", 
           sidebarPanel( width = 2, fileInput('file1', 'Choose xlsx file',
                                              accept = c(".xlsx")),
                         numericInput("ID", "enter the id number to increment", THERE SHOULD BE NO LIMIT IN MIN AND MAX )
           ), 
           
           mainPanel(tableOutput(outputId = "mytable"))
  ) )

server.r

library(shiny)

server <- function(input, output) {
  
  ########################################
  ### title1
  ########################################  
  
  data.Consenso <- reactive({
    inFile <- input$file1
    if(!is.null(inFile)) {
      readxl::read_excel(inFile$datapath)
      #read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)    
    }
  })
  
  
  output$mytable <- renderTable({
    
    dat <- data.Consenso()
    if(!is.null(dat))
    {
      dat$Tipo <- 3
      dat$my.ID <- (max(dat$ID) + 1):(max(dat$ID) + nrow(dat)) 
      dat
    }
  }) 
  
  
  
}
英文:

I would like to add an ID column to a data set using Shiny. And this ID column can be as long as the dataset dat$my.ID &lt;- (max(dat$ID) + 1):(max(dat$ID) + nrow(dat)) and should not limit the user who is entering a starting ID number. many thanks in advance.

ui.r

 navbarPage(
  title = &#39;Benvenuti&#39;,
  tabPanel(&#39;read.me&#39;, column(3,
                             
                             h3(&quot;Daily Data Manipulation&quot;),
                          
                             h3(&quot;title1&quot;),
                             tags$a(&quot;Upload data.xlsx&quot;)
  )),
  
  tabPanel(title = &quot;title2&quot;, 
           sidebarPanel( width = 2, fileInput(&#39;file1&#39;, &#39;Choose xlsx file&#39;,
                                              accept = c(&quot;.xlsx&quot;)),
                         numericInput(&quot;ID&quot;, &quot;enter the id number to increment&quot;, THERE SHOULD BE NO LIMIT IN MIN AND MAX )
           ), 
           
           mainPanel(tableOutput(outputId = &quot;mytable&quot;))
  ) ) 

server.r

library(shiny)

server &lt;- function(input, output) {
  
  ########################################
  ### title1
  ########################################  
  
  data.Consenso &lt;- reactive({
    inFile &lt;- input$file1
    if(!is.null(inFile)) {
      readxl::read_excel(inFile$datapath)
      #read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)    
    }
  })
  
  
  output$mytable &lt;- renderTable({
    
    dat &lt;- data.Consenso()
    if(!is.null(dat))
    {
      dat$Tipo &lt;- 3
      dat$my.ID &lt;- (max(dat$ID) + 1):(max(dat$ID) + nrow(dat)) 
      dat
    }
  }) 
  
  
  
}

答案1

得分: 1

这应该可以工作:

ui 中进行了更改:tabPanel(title = "title2",...
以及在 server 中:starting_id <- input$ID 从用户获取输入并创建一个以用户输入为起点,逐步增加1的ID序列。

ui.r

library(shiny)

navbarPage(
  title = 'Benvenuti',
  tabPanel('read.me', column(
    3,
    
    h3("Daily Data Manipulation"),
    
    h3("title1"),
    tags$a("Upload data.xlsx")
  )),
  
  tabPanel(
    title = "title2",
    sidebarPanel(
      width = 2,
      fileInput('file1', 'Choose xlsx file', accept = c(".xlsx")),
      numericInput(
        "ID",
        "Enter the starting ID number:",
        value = 1,
        min = -Inf,
        max = Inf
      )
    ),
    mainPanel(tableOutput(outputId = "mytable"))
  )
) 

server.r

server <- function(input, output) {
  data.Consenso <- reactive({
    inFile <- input$file1
    if (!is.null(inFile)) {
      readxl::read_excel(inFile$datapath)
    }
  })
  
  output$mytable <- renderTable({
    dat <- data.Consenso()
    if (!is.null(dat)) {
      dat$Tipo <- 3
      starting_id <- input$ID
      dat$my.ID <- starting_id:(starting_id + nrow(dat) - 1)
      dat
    }
  })
}
英文:

This should work:

Made changes in ui: tabPanel(title = &quot;title2&quot;,...
and in server: starting_id &lt;- input$ID takes the input from the user and makes a sequence of IDs starting from the user input and incrementing by 1.

ui.r

library(shiny)

navbarPage(
  title = &#39;Benvenuti&#39;,
  tabPanel(&#39;read.me&#39;, column(
    3,
    
    h3(&quot;Daily Data Manipulation&quot;),
    
    h3(&quot;title1&quot;),
    tags$a(&quot;Upload data.xlsx&quot;)
  )),
  
  tabPanel(
    title = &quot;title2&quot;,
    sidebarPanel(
      width = 2,
      fileInput(&#39;file1&#39;, &#39;Choose xlsx file&#39;, accept = c(&quot;.xlsx&quot;)),
      numericInput(
        &quot;ID&quot;,
        &quot;Enter the starting ID number:&quot;,
        value = 1,
        min = -Inf,
        max = Inf
      )
    ),
    mainPanel(tableOutput(outputId = &quot;mytable&quot;))
  )
) 

server.r

server &lt;- function(input, output) {
  data.Consenso &lt;- reactive({
    inFile &lt;- input$file1
    if (!is.null(inFile)) {
      readxl::read_excel(inFile$datapath)
    }
  })
  
  output$mytable &lt;- renderTable({
    dat &lt;- data.Consenso()
    if (!is.null(dat)) {
      dat$Tipo &lt;- 3
      starting_id &lt;- input$ID
      dat$my.ID &lt;- starting_id:(starting_id + nrow(dat) - 1)
      dat
    }
  })
}

huangapple
  • 本文由 发表于 2023年5月21日 16:01:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定