【问题标题】:How to import Excel sheet into shiny and update with a button如何将 Excel 工作表导入闪亮并使用按钮更新
【发布时间】:2020-05-02 17:57:34
【问题描述】:

我有一个闪亮的应用程序,它允许用户上传 Excel 工作表,然后将其显示在数据表中。然后我想在表上执行一个函数并显示新数据。

据我了解,流程应该是 1. 允许在 ui.R 端使用fileInput 导入数据

sidebarMenu(
        menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
        fileInput("csvs",
                  label="Upload CSVs here",
                  multiple = FALSE)

2。将数据存储为反应值

  data <- reactive({
    inFile <- input$csvs
    if (is.null(inFile)) { return(NULL) }    
    dataFile <- read_excel(inFile$datapath,sheet=1)
    dataFile<-data.frame(EndoPaste(dataFile)[1],stringsAsFactors=FALSE)
  })

RV <- reactiveValues(data = data.frame())

3.在数据表中显示数据

  output$endotable = DT::renderDT({
    RV$data<-data()
  },options = list(scrollX = TRUE))

4.允许用户针对数据运行函数

  observeEvent(input$textPrep,{

#My custom function
    mywordsOGD<-input$caption
    mywordsOGD<-unlist(strsplit(mywordsOGD,","))
    RV$data<-textPrep(RV$data[,1],mywordsOGD,NegEx="TRUE")

  },ignoreInit = TRUE)

5.在dataTable中显示新数据

当我使用 browser() 运行观察事件时,RV$data 已正确更新,唯一的问题是数据表未显示新值。

我确信这是显而易见的;如何在数据表中显示新值?

【问题讨论】:

  • "CSVs" 和 readxl::read_excel 不兼容:前者是文本文件,后者需要 XLS 或 XLSX 文件。
  • 嗨@r2evans,这是我的命名问题。 excel 文件实际上可以很好地加载到数据表中。他们只是没有按照我想要的方式更新
  • 这个问题有几个地方使它不完整,你能让它更具重现性吗?例如,我 inferring readxl (for read_excel) 但不确定其他非基础包,EndoPaste 来自哪里,它有什么作用?如果您可以使这个问题完全可重现(使其成为一个完整但最小的闪亮应用程序),您可能会更快地得到答案。
  • @r2evans 是对的。我已经给出了答案,但我不确定,因为问题不完整。我的答案很可能不适合...提供您的功能和用户界面(input$textPrep 是什么?)。

标签: r shiny


【解决方案1】:

类似这样的:

Data1 <- reactiveVal(NULL)
Data2 <- reactiveVal(NULL)

observe({
  inFile <- input$csvs
  if (!is.null(inFile)) {   
    dataFile <- read_excel(inFile$datapath, sheet=1)
    dat <- data.frame(EndoPaste(dataFile)[1], stringsAsFactors=FALSE)
    Data1(dat)
    Data2(dat)
  }
})

output$endotable = DT::renderDT({
  Data2()
},options = list(scrollX = TRUE))

observeEvent(input$textPrep,{
  mywordsOGD <- input$caption
  mywordsOGD <- unlist(strsplit(mywordsOGD, ","))
  Data2(textPrep(Data1()[,1], mywordsOGD, NegEx="TRUE"))
},ignoreInit = TRUE)

【讨论】:

  • 谢谢。我认为我不明白的部分是我可以使用观察事件来简单地更新数据,然后单独使用它。现在工作
猜你喜欢
  • 2021-07-11
  • 2021-12-14
  • 1970-01-01
  • 2021-03-07
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 1970-01-01
  • 2015-12-12
相关资源
最近更新 更多