【问题标题】:Getting error when trying to download pdf table from imported csv file in shiny app尝试从闪亮应用中导入的 csv 文件下载 pdf 表时出错
【发布时间】:2018-07-24 13:53:44
【问题描述】:

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

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

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

【问题讨论】:

    标签: pdf shiny knitr r-markdown


    【解决方案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())
    ```
    

    【讨论】:

    • 嗨,虽然你的回答似乎是对的,但我在尝试上传一些 csv 时遇到了这个错误。
    • "C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS kable.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --输出 kable.pdf --template "C:\Users\makis23\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex" --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in" !未定义的控制序列。 l.93 \rowcolors
    • pandoc.exe:生成 PDF 时出错警告:运行命令 '"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS kable.utf8.md --to latex --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash --output kable.pdf --template "C:\Users\makis23\Documents\R\win-library\3.4\rmarkdown\rmd\latex\default-1.17.0.2.tex " --highlight-style tango --latex-engine pdflatex --variable graphics=yes --variable "geometry:margin=1in"' 状态为 43 警告:错误:pandoc 文档转换失败,错误 43
    • 您收到此错误是因为您没有渲染 pdf 所需的乳胶包。您要么必须安装 MiTex 之类的应用程序,要么必须将其呈现为 html
    • Html 不是一个选项,所以我想我必须安装 MikTex
    猜你喜欢
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-13
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多