英文:
download fileinput() after data manipulation in Shiny
问题
以下是您要翻译的代码部分:
我有一个Shiny App,在一些数据操作之后,我想要下载`mytable`。非常感谢提前帮助。
library(shiny)
ui <- navbarPage(
title = 'Benvenuti',
tabPanel('read.me', column(3,
h3("每日数据操作"),
h3("标题1"),
tags$a("上传数据.xlsx")
)),
tabPanel(title = "标题2",
sidebarPanel( width = 2, fileInput('file1', '选择xlsx文件',
accept = c(".xlsx")),
numericInput("ID", "输入起始ID号码:",
value = 1, min = -Inf, max = Inf),
downloadButton("Download.mytable", "下载mytable")
),
mainPanel(tableOutput(outputId = "mytable"))
) )
server <- function(input, output) {
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
}
})
mytable2 <- reactive({mytable})
output$Download.mytable <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(mytable2(), file)
})
}
shinyApp(ui = ui, server = server)
如果您需要进一步的帮助,请告诉我。
英文:
I have a Shiny App and after some data manipulations, I would like to download mytable
. Many thanks in advance.
library(shiny)
ui <- 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),
downloadButton("Download.mytable", "Download mytable")
),
mainPanel(tableOutput(outputId = "mytable"))
) )
server <- function(input, output) {
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
}
})
mytable2 <- reactive({mytable})
output$Download.mytable <- downloadHandler(
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(mytable2(), file)
})
}
shinyApp(ui = ui, server = server)
Error
Warning: Error in <reactive:mytable2>: object 'mytable' not found
3: runApp
2: print.shiny.appobj
1: <Anonymous>
答案1
得分: 1
你使用了 mytable()
,但它在任何地方都没有被定义。假设你正在使用原始数据并以某种方式修改数据,你需要像这样的内容:
mytable <- reactive({
req(data.Consenso())
something <- data.Consenso() %>%
subset(somevar > 5) %>%
transform(newvar = somevar - 42)
something
})
我使用了基本的R管道等,这只是一个演示,_这_就是你在允许用户下载新创建的CSV之前需要进行任何数据操作的地方。
英文:
You use mytable()
but it is not defined anywhere. Assuming you are taking the original data and somehow modifying things, you need something like this:
mytable <- reactive({
req(data.Consenso())
something <- data.Consenso() |>
subset(somevar > 5) |>
transform(newvar = somevar - 42)
something
})
I'm using base-R pipes and such, this is just a demonstration that this is where you would do whatever data manipulation is needed before allowing the user to download a newly-created CSV.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论