尝试从闪亮的应用程序中从导入的csv文件下载pdf表时出现错误 [英] Getting error when trying to download pdf table from imported csv file in shiny app

查看:73
本文介绍了尝试从闪亮的应用程序中从导入的csv文件下载pdf表时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的闪亮应用程序,我想从其中导入一个csv文件后下载一个pdf表.我怀疑我在使用时不正确地使用了参数:

Hi I have a simple shiny app from which i wish to download a pdf table after importing a csv file into it. I suspect that i use the parameters incorrectly as i take :

错误:文件"必须是字符串或连接

Error : 'file' must be a character string or connection

#ui.r
library(shiny)
library(rmarkdown)

fluidPage(sidebarLayout(
  sidebarPanel(
    fileInput("file1", "Input CSV-File"),
    downloadButton(
      outputId = "downloader",
      label = "Download PDF"
    )
  ),
  mainPanel(tableOutput("table"))
))
#server.r
function(input, output) {

  thedata <- reactive({
    inFile <- req(input$file1)
    read.csv(inFile$datapath)
  })
  output$table <- renderTable({
    thedata()
  })
  output$downloader <- downloadHandler(
    filename = "Student-report.pdf",
    content = function(file){
      out = rmarkdown::render("kable.Rmd")
      file.rename(out, file)
    }
  )
}
#kable.rmd
---

output: pdf_document
params:
  table: 'NULL'
  file: 'NULL'
---

```{r echo = FALSE, message = FALSE, include = FALSE}

library(knitr)
```

```{r nice-tab, tidy = FALSE, echo = FALSE, message = FALSE}
read.csv(file = params$file1)

```
```{r}
params[["table"]]
```

推荐答案

问题出在Rmd文件中使用文件.并且因为您已经在响应函数中获得了数据框,所以不必单独传递文件.请查看更新的代码:

The issue was with using file in your Rmd file. And because you've got the dataframe in a reactive function, you don't have to pass the file separately. Please see the updated code:

#ui.r
library(shiny)
library(rmarkdown)

ui <- fluidPage(sidebarLayout(
  sidebarPanel(
    fileInput("file1", "Input CSV-File"),
    downloadButton(
      outputId = "downloader",
      label = "Download PDF"
    )
  ),
  mainPanel(tableOutput("table"))
))
#server.r
server <- function(input, output) {

  thedata <- reactive({
    inFile <- req(input$file1)
    read.csv(inFile$datapath)
  })
  output$table <- renderTable({
    thedata()
  })
  output$downloader <- downloadHandler(
    filename = "Student-report.pdf",
    content = function(file){
      out <- rmarkdown::render("kable.Rmd", pdf_document())
      file.rename(out, file)
    }
  )
}

shinyApp(ui,server)

Kable.Rmd

---
output: pdf_document
---

```{r echo = FALSE, message = FALSE, include = FALSE}

library(knitr)
```


```{r}
kable(thedata())
```

这篇关于尝试从闪亮的应用程序中从导入的csv文件下载pdf表时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆