从文件输入更新闪亮表

时间:2019-09-25 12:14:37

标签: r shiny r-markdown shinydashboard flexdashboard

我正在尝试创建一个闪亮的/ flexdashboard,可以通过上载新文件来刷新表格。

下面的代码是一个简单的仪表板,带有一个表(使用formattable),该表首先加载了数据集。 用户可以使用文件上传小部件来上传新的数据集-我想了解上传完成后如何刷新表。是否应该在new_df_tbl事件中完成?

该示例是独立的,可以直接在Rstudio中使用。

---
title: "Basic"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
runtime: shiny
---

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

```{r}
df <- data.frame(
  x = rnorm(100),
  y = rnorm(100)
)
```

```{r}
df_tbl <- reactive({
    df %>% select(x, y) # do some stuff here but for simplicity we do nothing
})
```



Sidebar {.sidebar}
=======================================================================

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

br()

actionButton(inputId = "apply", label = "Apply", icon = icon("play"))
```

```{r}
rv <- reactiveValues(data = NULL)
rv <- df

observeEvent(eventExpr = input$apply, 
  handlerExpr = {
      req(input$file1)
      file <- read_csv(input$file1$datapath)
      rv$data <- file
  }, ignoreNULL = FALSE)
```


Main
=======================================================================

Column
-----------------------------------------------------------------------

### Table

```{r}
renderFormattable({
  formattable(df_tbl())
})
```

1 个答案:

答案 0 :(得分:0)

更新了代码。

rv <- reactiveValues(data = NULL)
rv$data <- df

observeEvent(eventExpr = input$apply, 
  handlerExpr = {
      req(input$file1)
      file <- read_csv(input$file1$datapath)
      rv$data <- file
  }, ignoreNULL = FALSE)

在调用表的任何地方,都使用rv$data。用户加载文件时,这应该会更新您的表。