尝试从闪亮的应用程序中导入的csv文件下载pdf表时出错

时间:2018-02-14 04:19:41

标签: pdf shiny knitr r-markdown

您好我有一个简单的闪亮应用程序,我希望在将csv文件导入其中后下载pdf表。我怀疑我采用的参数不正确:

  

错误:'file'必须是字符串或连接

#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"]]
```

1 个答案:

答案 0 :(得分:1)

问题在于使用Rmd文件中的文件。并且因为您已经在反应函数中获得了数据帧,所以您不必单独传递该文件。请参阅更新的代码:

#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())
```