【问题标题】:Is there a way to combine reactive expressions in Shiny?有没有办法在 Shiny 中结合反应式表达?
【发布时间】:2016-08-17 05:57:35
【问题描述】:

在使用 R/Shiny 作为 GIS 的背景下,我对反应性非常陌生。..

如果执行两个不同的反应表达式中的任何一个,是否可以有一个反应变量会改变?

基本上,用户可以手动创建数据框或上传数据框。问题是,我将所有反应表达式都绑定到手动创建端( customDF() )并且无法集成上传端。

换句话说,我希望程序在我按下两个按钮(创建数据框和上传)中的任何一个时意识到数据框已更改。我试过让任一表达式重新分配一个静态变量,但它破坏了反应性。

按钮按下方法如下所示:

customDF <- eventReactive(input$buildTable,{

    p <- getPoly()
    a <- attrib()

    for( i in 1:length(activeLayers)){
      word <- activeLayers[i]
      p[[word]] <- with(a, a[[word]][match(p$geoid, a$geoid)])
    }
    p 
  })

 # Method that uses it
 exportResult <- eventReactive(input$export, {        
    j <- customDF()
    writeSpatialShape(j, paste(repoLocation,input$layerName, sep=""))

    "Export successful"
  })

importResult <- eventReactive(input$selectImport, {
    # ?????? 
  })

任何帮助将不胜感激。

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    导入文件并选择文件? 改编自:https://gist.github.com/jcheng5/9496297

    server.r

    #list of files
    files <<- ("create data frame" = "create",
              "upload data frame" = "upload")
    
    #cash results
    getData <- memoise(function(file){
        if (!(file %in% files))
           stop("Unknown File")
    
         data<- as.data.frame(sprintf("path///to///file///location//%s.csv", file))
    
        data
        })
    
    #reactive function
    shinyServer(function(input, output, session) {
      # Define a reactive expression for the document
      reactiveData <- reactive({
        # Change when the "update" button is pressed...
        input$update
        # ...but not for anything else
        isolate({
          withProgress(session, {
            setProgress(message = "Processing data...")
            getData(input$selection)
          })
        })
      })
    
    
    #output as table or whatever
    output$table <- DT::renderDataTable(DT::datatable(
                         data <- as.data.frame(reactiveData())
                    )
    

    ur.r

    shinyUI(fluidPage(
      progressInit(),
    
      # Application title
      headerPanel("data selection"),
    
      # Sidebar with a slider and selection inputs
      sidebarPanel(width = 5,
        selectInput("selection", "Choose a dataset:", 
                    choices = files),
        actionButton("update", "Change"),
    
      ),
    
      # Show data
      mainPanel(
        DT::dataTableOutput("table")
      )
    ))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-08
      相关资源
      最近更新 更多