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

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

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 &lt;- navbarPage(
  title = &#39;Benvenuti&#39;,
  tabPanel(&#39;read.me&#39;, column(3,
                             
                             h3(&quot;Daily Data Manipulation&quot;),
                             
                             h3(&quot;title1&quot;),
                             tags$a(&quot;Upload data.xlsx&quot;)
  )),
  
  tabPanel(title = &quot;title2&quot;, 
           sidebarPanel( width = 2, fileInput(&#39;file1&#39;, &#39;Choose xlsx file&#39;,
                                              accept = c(&quot;.xlsx&quot;)),
                         numericInput(&quot;ID&quot;, &quot;Enter the starting ID number:&quot;,
                                      value = 1, min = -Inf, max = Inf),
                         downloadButton(&quot;Download.mytable&quot;, &quot;Download mytable&quot;)
           ), 
           
           mainPanel(tableOutput(outputId = &quot;mytable&quot;))
  ) ) 



server &lt;- function(input, output) {
  
  data.Consenso &lt;- reactive({
  inFile &lt;- input$file1
  if(!is.null(inFile)) {
    readxl::read_excel(inFile$datapath)
    #read.csv(inFile$datapath, header = TRUE, stringsAsFactors = FALSE)    
  }
})


output$mytable &lt;- renderTable({
  
  dat &lt;- data.Consenso()
  if(!is.null(dat))
  {
    dat$Tipo &lt;- 3
    
    dat
  }
}) 

mytable2 &lt;- reactive({mytable})

output$Download.mytable  &lt;- downloadHandler(
  
  filename = function() { 
    paste(&quot;data-&quot;, Sys.Date(), &quot;.csv&quot;, sep=&quot;&quot;)
  },
  
  content = function(file) {
    
    write.csv(mytable2(), file)
    
  })


}

shinyApp(ui = ui, server = server)

Error

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

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

答案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 &lt;- reactive({
  req(data.Consenso())
  something &lt;- data.Consenso() |&gt;
    subset(somevar &gt; 5) |&gt;
    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.

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:

确定