英文:
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 <- (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
}
})
}
答案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 = "title2",...
and in server
: starting_id <- 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 = '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
}
})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论