3

我希望能够将反应传递data.table给我的 Rmarkdown 文档,该文档将对表格进行排序并打印顶部(例如 5 个)排序条目。

我一直在遇到“错误:x 必须是 data.frame 或 data.table” setorderv。我怀疑反应是扰乱is.data.table检查。

如何简单地将数据副本传递给 Rmd 文档?并删除反应性包装?

这是一个从反应性和 downloadHandler 库存示例拼凑而成的简化示例。我在 SO上查看了这个,它建议使用观察,但我不知道如何实现这个。谢谢我感谢一些帮助。

RMARKDOWN - SOtestreport.Rmd

---
title: "Top in Segment Report"
output: html_document
params:
  n: 5
  dt: NA
  top.field: NA
---


```{r setup, echo = FALSE}
library(data.table)
library(knitr) # for kable

```
Data report as at: `r format(Sys.time(), "%a %b %d %Y %X")`

```{r, echo = FALSE}
table.str <- paste0("Showing Top ", params$n ," tables for these fields: ", 
             params$top.field)

# is this the best way of having dynamic labels?
```
`r table.str`


```{r, echo = FALSE}
# tried this
# test.dt <- copy(dt)

# normally a function GenerateTopTable with limit sort field etc.
setorderv(dt, params$top.field, -1) # -1 is descending. THIS IS WHERE IT CRASHES
out <- dt[1:params$n,]

```


```{r toptable, echo = FALSE}

kable(out, digits = 3)

```

应用程序.R

# SO test report
# mini example based on https://shiny.rstudio.com/gallery/reactivity.html

library(shiny)
library(datasets)

server <- function(input, output) {

  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })

  output$caption <- renderText({
    input$caption
  })

  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })


  # PROBLEM BIT WHERE I AM ADDING ON GALLERY EXAMPLE
  # handler from https://stackoverflow.com/questions/37347463/generate-report-with-shiny-app-and
  output$topreport <- downloadHandler(
    filename = "topreport.html",
    content = function(file) {
      # Copy the report file to a temporary directory before processing it, in
      # case we don't have write permissions to the current working dir (which
      # can happen when deployed).
      tempReport <- file.path(tempdir(), "SOtestreport.Rmd")
      file.copy("SOtestreport.Rmd", tempReport, overwrite = TRUE)

      # Set up parameters to pass to Rmd document
      #  TRIED THIS TOO
      #  my.dt <- as.data.table(datasetInput())
      my.dt <- datasetInput()
      my.params <- list(n = input$obs, 
                        dt = my.dt,
                        top.field = names(my.dt)[1])

      rmarkdown::render(tempReport, output_file = file,
                        params = my.params,
                        envir = new.env(parent = globalenv())
      )
    }
  )
4

1 回答 1

5

我修复了这个例子——dt在 Rmd 中没有以params$!

setorderv(params$dt, params$top.field, -1) # -1 is descending
out <- params$dt[1:params$n,]

我仍在寻找关于这是否是上述正确方法的答案(将 Shiny 中的参数传递给 RMarkdown 应用程序)。

我还想向其他人指出,这些答案也很有用:

我还利用复制数据集来破坏反应性,即

my.dt <- copy(params$dt)
#do this before passing into render params list

RStudio 中的另一个技巧是不要使用内部查看器窗格,而是在浏览器中外部运行。这解决了临时文件保存位置的问题。您可以在下图中的“运行应用程序”下选择始终在外部运行:

运行ExternalinRStudio

于 2017-03-08T14:44:46.940 回答