【问题标题】:R Shiny: Perform a series of functions on reactive inputR Shiny:对反应输入执行一系列功能
【发布时间】:2017-02-16 01:33:11
【问题描述】:

我想在 data.frame 上执行一系列任务,这些任务将由用户通过 actionButton 加载。这是一个示例,我想在用户选择的 data.frame 的第三列中删除具有 NA 的行。我得到错误:

“警告:observeEventHandler 出错:找不到对象‘df’

堆栈跟踪(最内层优先):

65:observeEventHandler [(文件位置).R#56]"

为什么observeEvent不能识别变量f?

Server.R

library(shiny)

 shinyServer <- function(input, output) {
    filedata <- reactive({
        infile <- input$Samples
        if (is.null(infile)) {
            # User has not uploaded a file yet
            return(NULL)
        }
        df <- read.table(infile$datapath,sep="\t",skip =0, header = TRUE,na.strings = "NA",stringsAsFactors=FALSE)
        })

    observeEvent(input$Click, {
            selectA <- df[complete.cases(f[,3]),]
    }
}

ui.R

library(shiny)

 shinyUI( <- fluidPage(
    tabPanel("Inputs",
         wellPanel(fileInput(inputId = "Samples", label = "Import File"), 
                   actionButton(inputId = "Click", label = "Samples")),
         h2('Results'),
         dataTableOutput("Results")))

【问题讨论】:

  • dffiledata 反应对象的局部变量。你要在这里做什么?你希望这组减少的行最终去哪里?看来您还没有完全了解 Shiny 使用的正确反应式数据模型。 Shiny developer conference page上有有用的介绍
  • 最终我想在用户上传的文件上运行一个函数,并在表格、绘图、空间等中显示结果。我的函数作为脚本正常工作,但我想要它作为闪亮界面的一部分运行。
  • 当您编写 R 脚本时,您可能会编写函数,但它基本上是声明性代码。 Shiny 使用反应式范式。代码看起来会很不一样。我建议您从基本教程或我链接到的视频开始。这里真的没有一个可以用格式回答的明确问题。

标签: r shiny


【解决方案1】:

也许我没有很好地表达我的问题。这是我一直在寻找的答案。

Server.R

library(shiny)

shinyServer <- function(input, output) {
    filedata <- reactive({
     infile <- input$Samples
     if (is.null(infile)) {
        # User has not uploaded a file yet
        return(NULL)
    }
    read.table(infile$datapath,sep="\t",skip =0, header = TRUE,na.strings = "NA",stringsAsFactors=FALSE)
    })

    observeEvent(input$Click, {
     df <- filedata()
     selectA <- df[complete.cases(df[,3]),]
    }
}

【讨论】:

    猜你喜欢
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2016-08-25
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 2019-06-30
    相关资源
    最近更新 更多