英文:
data manipulation after file (excel) input in shiny
问题
I have an Excel dataset that I want to manipulate after uploading it. Let's say in this case I would like to add a newcolumn <- 3
, how can I get around with this? many thanks in advance.
ui.r
library(shiny)
ui <- fluidPage(titlePanel("daily data manipulation"),
sidebarLayout(sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx"))
),
mainPanel(tableOutput(outputId = "mytable")
)))
server.r
server <- function(input, output) {
data <- reactive({
inFile <- input$file1
if(!is.null(inFile)) {
readxl::read_excel(inFile$datapath)
}
})
dat <- data()
output$mytable <- renderTable({
dat$newcolumn <- 3
})
}
英文:
I have an Excel dataset that I want to manipulate after uploading it. Let's say in this case I would like to add a newcolumn <- 3
, how can I get around with this? many thanks in advance.
ui.r
library(shiny)
ui <- fluidPage(titlePanel("daily data manipulation"),
sidebarLayout(sidebarPanel(
fileInput('file1', 'Choose xlsx file',
accept = c(".xlsx"))
),
mainPanel(tableOutput(outputId = "mytable")
)))
server.r
server <- function(input, output) {
data <- reactive({
inFile <- input$file1
if(!is.null(inFile)) {
readxl::read_excel(inFile$datapath)
}
})
dat <- data()
output$mytable <- renderTable({
dat$newcolumn <- 3
})
}
答案1
得分: 1
Reactive expressions can only be consumed in a reactive context, so dat <- data()
should be moved inside renderTable
.
此外,在添加新列之前检查反应性值是否为null可以确保在用户上传Excel文件之前不会呈现新列。
英文:
Reactive expressions can only be consumed in a reactive context, so dat <- data()
should be moved inside renderTable
.
server <- function(input, output, session) {
data <- reactive({
inFile <- input$file1
if(!is.null(inFile)) {
readxl::read_excel(inFile$datapath)
}
})
output$mytable <- renderTable({
dat <- data()
if(!is.null(dat))
{
dat$newcolumn <- 3
dat
}
})
}
Also, checking if the reactive value is null before adding the new column ensures that until the user uploads an excel file the new column will not be rendered.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论