【问题标题】:Update shiny table from a file input从文件输入更新闪亮表
【发布时间】:2020-01-25 15:08:37
【问题描述】:

我正在尝试创建一个闪亮的/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())
})
```

【问题讨论】:

    标签: r shiny r-markdown shinydashboard flexdashboard


    【解决方案1】:

    更新了你的代码。

    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。这应该会在用户加载文件时更新您的表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-28
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 2020-06-23
      • 2021-04-18
      • 2020-12-28
      相关资源
      最近更新 更多