下载在Shiny中进行数据处理后的fileInput()。

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

download fileinput() after data manipulation in Shiny

问题

以下是您要翻译的代码部分:

  1. 我有一个Shiny App,在一些数据操作之后,我想要下载`mytable`。非常感谢提前帮助。
  2. library(shiny)
  3. ui <- navbarPage(
  4. title = 'Benvenuti',
  5. tabPanel('read.me', column(3,
  6. h3("每日数据操作"),
  7. h3("标题1"),
  8. tags$a("上传数据.xlsx")
  9. )),
  10. tabPanel(title = "标题2",
  11. sidebarPanel( width = 2, fileInput('file1', '选择xlsx文件',
  12. accept = c(".xlsx")),
  13. numericInput("ID", "输入起始ID号码:",
  14. value = 1, min = -Inf, max = Inf),
  15. downloadButton("Download.mytable", "下载mytable")
  16. ),
  17. mainPanel(tableOutput(outputId = "mytable"))
  18. ) )
  19. server <- function(input, output) {
  20. data.Consenso <- reactive({
  21. inFile <- input$file1
  22. if(!is.null(inFile)) {
  23. readxl::read_excel(inFile$datapath)
  24. #read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)
  25. }
  26. })
  27. output$mytable <- renderTable({
  28. dat <- data.Consenso()
  29. if(!is.null(dat))
  30. {
  31. dat$Tipo <- 3
  32. dat
  33. }
  34. })
  35. mytable2 <- reactive({mytable})
  36. output$Download.mytable <- downloadHandler(
  37. filename = function() {
  38. paste("data-", Sys.Date(), ".csv", sep="")
  39. },
  40. content = function(file) {
  41. write.csv(mytable2(), file)
  42. })
  43. }
  44. 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.

  1. library(shiny)
  2. ui &lt;- navbarPage(
  3. title = &#39;Benvenuti&#39;,
  4. tabPanel(&#39;read.me&#39;, column(3,
  5. h3(&quot;Daily Data Manipulation&quot;),
  6. h3(&quot;title1&quot;),
  7. tags$a(&quot;Upload data.xlsx&quot;)
  8. )),
  9. tabPanel(title = &quot;title2&quot;,
  10. sidebarPanel( width = 2, fileInput(&#39;file1&#39;, &#39;Choose xlsx file&#39;,
  11. accept = c(&quot;.xlsx&quot;)),
  12. numericInput(&quot;ID&quot;, &quot;Enter the starting ID number:&quot;,
  13. value = 1, min = -Inf, max = Inf),
  14. downloadButton(&quot;Download.mytable&quot;, &quot;Download mytable&quot;)
  15. ),
  16. mainPanel(tableOutput(outputId = &quot;mytable&quot;))
  17. ) )
  18. server &lt;- function(input, output) {
  19. data.Consenso &lt;- reactive({
  20. inFile &lt;- input$file1
  21. if(!is.null(inFile)) {
  22. readxl::read_excel(inFile$datapath)
  23. #read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)
  24. }
  25. })
  26. output$mytable &lt;- renderTable({
  27. dat &lt;- data.Consenso()
  28. if(!is.null(dat))
  29. {
  30. dat$Tipo &lt;- 3
  31. dat
  32. }
  33. })
  34. mytable2 &lt;- reactive({mytable})
  35. output$Download.mytable &lt;- downloadHandler(
  36. filename = function() {
  37. paste(&quot;data-&quot;, Sys.Date(), &quot;.csv&quot;, sep=&quot;&quot;)
  38. },
  39. content = function(file) {
  40. write.csv(mytable2(), file)
  41. })
  42. }
  43. shinyApp(ui = ui, server = server)

Error

  1. Warning: Error in &lt;reactive:mytable2&gt;: object &#39;mytable&#39; not found
  2. 3: runApp
  3. 2: print.shiny.appobj
  4. 1: &lt;Anonymous&gt;

photo
下载在Shiny中进行数据处理后的fileInput()。

答案1

得分: 1

你使用了 mytable(),但它在任何地方都没有被定义。假设你正在使用原始数据并以某种方式修改数据,你需要像这样的内容:

  1. mytable <- reactive({
  2. req(data.Consenso())
  3. something <- data.Consenso() %>%
  4. subset(somevar > 5) %>%
  5. transform(newvar = somevar - 42)
  6. something
  7. })

我使用了基本的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:

  1. mytable &lt;- reactive({
  2. req(data.Consenso())
  3. something &lt;- data.Consenso() |&gt;
  4. subset(somevar &gt; 5) |&gt;
  5. transform(newvar = somevar - 42)
  6. something
  7. })

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.

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

发表评论

匿名网友

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

确定