在Shiny中添加一列

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

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

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

server.r

  1. library(shiny)
  2. server <- function(input, output) {
  3. ########################################
  4. ### title1
  5. ########################################
  6. data.Consenso <- reactive({
  7. inFile <- input$file1
  8. if(!is.null(inFile)) {
  9. readxl::read_excel(inFile$datapath)
  10. #read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)
  11. }
  12. })
  13. output$mytable <- renderTable({
  14. dat <- data.Consenso()
  15. if(!is.null(dat))
  16. {
  17. dat$Tipo <- 3
  18. dat$my.ID <- (max(dat$ID) + 1):(max(dat$ID) + nrow(dat))
  19. dat
  20. }
  21. })
  22. }
英文:

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

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

server.r

  1. library(shiny)
  2. server &lt;- function(input, output) {
  3. ########################################
  4. ### title1
  5. ########################################
  6. data.Consenso &lt;- reactive({
  7. inFile &lt;- input$file1
  8. if(!is.null(inFile)) {
  9. readxl::read_excel(inFile$datapath)
  10. #read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)
  11. }
  12. })
  13. output$mytable &lt;- renderTable({
  14. dat &lt;- data.Consenso()
  15. if(!is.null(dat))
  16. {
  17. dat$Tipo &lt;- 3
  18. dat$my.ID &lt;- (max(dat$ID) + 1):(max(dat$ID) + nrow(dat))
  19. dat
  20. }
  21. })
  22. }

答案1

得分: 1

这应该可以工作:

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

ui.r

  1. library(shiny)
  2. navbarPage(
  3. title = 'Benvenuti',
  4. tabPanel('read.me', column(
  5. 3,
  6. h3("Daily Data Manipulation"),
  7. h3("title1"),
  8. tags$a("Upload data.xlsx")
  9. )),
  10. tabPanel(
  11. title = "title2",
  12. sidebarPanel(
  13. width = 2,
  14. fileInput('file1', 'Choose xlsx file', accept = c(".xlsx")),
  15. numericInput(
  16. "ID",
  17. "Enter the starting ID number:",
  18. value = 1,
  19. min = -Inf,
  20. max = Inf
  21. )
  22. ),
  23. mainPanel(tableOutput(outputId = "mytable"))
  24. )
  25. )

server.r

  1. server <- function(input, output) {
  2. data.Consenso <- reactive({
  3. inFile <- input$file1
  4. if (!is.null(inFile)) {
  5. readxl::read_excel(inFile$datapath)
  6. }
  7. })
  8. output$mytable <- renderTable({
  9. dat <- data.Consenso()
  10. if (!is.null(dat)) {
  11. dat$Tipo <- 3
  12. starting_id <- input$ID
  13. dat$my.ID <- starting_id:(starting_id + nrow(dat) - 1)
  14. dat
  15. }
  16. })
  17. }
英文:

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

  1. library(shiny)
  2. navbarPage(
  3. title = &#39;Benvenuti&#39;,
  4. tabPanel(&#39;read.me&#39;, column(
  5. 3,
  6. h3(&quot;Daily Data Manipulation&quot;),
  7. h3(&quot;title1&quot;),
  8. tags$a(&quot;Upload data.xlsx&quot;)
  9. )),
  10. tabPanel(
  11. title = &quot;title2&quot;,
  12. sidebarPanel(
  13. width = 2,
  14. fileInput(&#39;file1&#39;, &#39;Choose xlsx file&#39;, accept = c(&quot;.xlsx&quot;)),
  15. numericInput(
  16. &quot;ID&quot;,
  17. &quot;Enter the starting ID number:&quot;,
  18. value = 1,
  19. min = -Inf,
  20. max = Inf
  21. )
  22. ),
  23. mainPanel(tableOutput(outputId = &quot;mytable&quot;))
  24. )
  25. )

server.r

  1. server &lt;- function(input, output) {
  2. data.Consenso &lt;- reactive({
  3. inFile &lt;- input$file1
  4. if (!is.null(inFile)) {
  5. readxl::read_excel(inFile$datapath)
  6. }
  7. })
  8. output$mytable &lt;- renderTable({
  9. dat &lt;- data.Consenso()
  10. if (!is.null(dat)) {
  11. dat$Tipo &lt;- 3
  12. starting_id &lt;- input$ID
  13. dat$my.ID &lt;- starting_id:(starting_id + nrow(dat) - 1)
  14. dat
  15. }
  16. })
  17. }

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:

确定