在Flexdashboard中上传文件

时间:2019-01-09 15:02:36

标签: r input flexdashboard

我正在尝试在Flexdashboard中添加输入文件,但我有些困惑。以前,我创建了一个用于生成图的函数,然后使用该函数调用函数renderPlot以便在仪表板上获取该图。

R flexdashboard and shiny interactive plot

但是,现在,如果我创建了一个读取文件的函数,我将不知道如何调用它。

如何遵循相同的想法,创建一个函数,该函数读取仪表板中带有输入文件的文件,然后在仪表板中执行分析。

我已经生成了此代码

    ---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```

Column {.sidebar}
-----------------------------------------------------------------------
```{r, echo = FALSE}
fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                accept = c("text/csv",
                         "text/comma-separated-values,text/plain",
                         ".csv"))


```

通过此操作,我获得了带有文件输入的侧栏。但是如何在任何分析中使用上载的文件并在仪表板中显示结果?

enter image description here

    ---
title: "Untitled"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
---

```{r setup, include=FALSE}
library(flexdashboard)
library(shiny)
```

Column {.sidebar}
-----------------------------------------------------------------------
```{r, echo = FALSE}
fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                accept = c("text/csv",
                         "text/comma-separated-values,text/plain",
                         ".csv"))
df_reac <- reactive({
  read_xlsx(file1$datapath)
})

```

Column{data-width=300}
-----------------------------------------------------------------------
```{r, echo = FALSE}
renderTable(df_reac)


```

1 个答案:

答案 0 :(得分:0)

这是一个老问题,但这段代码可以让你在 Flexdashboard 上上传文件:

    ---
    title: "Untitled"
    output: 
      flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    runtime: shiny
    ---

    ```{r setup, include=FALSE}
    library(flexdashboard)
    ```

    ```{r}
    dataset <- eventReactive(input$file1,{
      dataset <- read.csv(input$file1$datapath)
    })
    ```

    # Column {.sidebar}

    ```{r, echo = FALSE}
    fileInput("file1", "Choose CSV File",
                    multiple = TRUE,
                    accept = c("text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv"))
    ```

    # Data 

    ## Row

    ### Table 1 - Show the data recently acquired

    ```{r}
    renderTable({
      dataset <- dataset()
      dataset
    })
    ```

主要问题是创建一个响应式函数,因为在上传文件之前您不知道数据集。反应值为 input$file1