【问题标题】:How to override (upon actionButton click) a reactive object in R Shiny Flexdashboard如何在 R Shiny Flexdashboard 中覆盖(在单击 actionButton 时)反应性对象
【发布时间】:2020-01-13 00:58:17
【问题描述】:

我正在尝试制作一个 R Shiny Flexdashboard,它允许用户使用本地 Excel 文件下载的 Googlesheet 作为数据源。

我在这里创建了一个示例,但我使用本地 csv 文件来代替 Googlesheets 下载组件(在示例中使用 googlesheets 会很复杂)。

https://gist.github.com/jtag04/67ae6b2c39e4f68f90e06bb1ce2ceb98

上面的脚本有效(将其保存为 *.rmd 文件以供其运行 - 它是一个 Flexdashboard)。

但是,我的挑战是我希望 *csv 上传(按下 actionButton 时)覆盖 Excel 文件对象(如果存在)。

即不是将 csv 保存到 step_one_2(如示例中所示),而是覆盖 step_one 对象(如果存在)。

这可能吗? 非常感谢。

【问题讨论】:

    标签: r shiny flexdashboard


    【解决方案1】:

    实现这样的事情的一种方法(如果我理解正确的话)是使用radioButtons 让用户选择输入类型。然后根据用户的选择呈现不同的输入元素,并有一个响应式来适当地处理两种不同的输入文件类型。最后,我们从 excel 或 googlesheets 文件呈现单个表格。

    代码:

    library(shiny)
    library(tidyverse)
    
    # let the user choose the type of input file
    radioButtons(
      inputId = "what_source",
      label = "Choose source file",
      choices = c("googlesheets", "excel"),
      inline = FALSE
      )
    
    # render file input widgets conditioned on user choice
    conditionalPanel(condition = 'input.what_source == "googlesheets"',
                     fileInput("google_file", "Upload from csv", accept = ".csv")
                     )
    
    conditionalPanel(condition = 'input.what_source == "excel"',
                     fileInput("excel_file", "Upload from excel", accept = ".xlsx")
                     )
    
    # deal with the file selected by the user
    what_file <- reactive({
      if (input$what_source == "googlesheets") {
        x <- readr::read_csv(input$google_file$datapath) %>% mutate(source = "csv")
      } else {
        x <- readxl::read_excel(input$excel_file$datapath) %>% mutate(source = "Excel")
      return(x)
      }
    })
    
    # then in a different element downstream add the render function:
    renderDataTable(what_file())
    

    更新:随意取csv,但用checkbox改成excel输入:

    服务器逻辑:

    # fetch updated csv data
    actionButton("fetch", "Fetch new data")
    checkboxInput("excel", label = "Get excel file", value = FALSE)
    
    fetch_new <- eventReactive(input$fetch, {
      readr::read_csv("df.csv")
    })
    
    conditionalPanel(condition = 'input.excel == true',
                     fileInput("excel_file", 
                               "Upload from excel", 
                                accept = ".xlsx")
                     )
    
    what_file <- reactive({
      if (!input$excel) {
        x <- fetch_new()
      } else {
        x <- readxl::read_excel(input$excel_file$datapath)
      return(x)
      }
    })
    
    

    【讨论】:

    • 感谢@teofil,我认为您的观点肯定会有所帮助。关键部分是我需要能够反复“重新点击”“googlesheets”(实际上是 csv)按钮......以便我每次都下载新数据。
    • 在这种情况下,我可能会让csv 获取默认行为,并且仅在用户需要时才切换到 excel 输入。您可以在用户每次单击“update_csv”时刷新数据,并且仅在用户单击复选框时呈现文件输入。
    • 嘿@teofil,我认为(感谢您的评论)我有一个可行的解决方案......这对你来说怎么样? gist.github.com/jtag04/cced196b01d781d5c35ae5d0c7ce60a1
    • 是的!就是这样!欢呼声。只是把我的脑袋绕在所有这些 eventReactive 和 observeReactive 函数上
    猜你喜欢
    • 2018-06-11
    • 2020-10-22
    • 2023-02-12
    • 2021-02-19
    • 2020-07-27
    • 2017-06-27
    • 2013-06-21
    • 2020-07-31
    • 2021-10-27
    相关资源
    最近更新 更多