简体   繁体   English

闪亮-react()在两个react()之间进行选择

[英]shiny - reactive() to select between two reactive()

I'm making an app in Shiny which allows users to upload data or upload a save file. 我正在使用Shiny制作一个应用程序,该应用程序允许用户上传数据或上传保存文件。

If a user uploads new data then the flow looks like: 如果用户上传新数据,则流程如下所示:

Upload Files >> Select Sheets >> Select Columns >> Sentence Tokenization >> Word Tokenization >> Graphics 上传文件>>选择工作表>>选择列>>句子标记化>>单词标记化>>图形

Originally, I had been using observe() statements with reactiveValues() so at each step, I stored off the results into a reactiveValues(). 最初,我一直将observe()语句与reactorValues()一起使用,因此在每一步中,我都将结果存储到reactiveValues()中。 This enabled the save file upload to work like: 这样可以使保存文件上载的工作方式如下:

Upload Save File >> Set sentences >> Set words >> Graphics 上传保存文件>>设置句子>>设置单词>>图形

I would like to be able to accomplish something similar but using reactive() statements. 我希望能够完成类似的事情,但要使用react()语句。 So, given that I have uploadedFiles = reactive() and uploadedSave = reactive() how do I write a reactive with the following pseudo code: 因此,鉴于我已经上uploadedFiles = reactive()uploadedSave = reactive() ,我如何使用以下伪代码编写反应式:

rawText = reactive({
  if uploadedFiles() flushes reactivity then uploadedFiles()
  else if uploadedSave() flushes reactivity then uploadedSave()
  else NULL
})

I don't have an "error" I'm trying to troubleshoot, just trying to think through the process of using a reactive to act like a 'most recently flushed' gate to allow my data flow to start from two different places and converge to a single one. 我没有要尝试解决的“错误”,只是试图思考使用反应式充当“最近刷新”门的过程,以允许我的数据流从两个不同的地方开始并收敛到一个。

I'm going to show how I do it, but I have a feeling it is not a great method. 我将展示如何做到这一点,但我觉得这不是一个好方法。 I'm interested in hearing what the best method is and what's wrong with mine. 我有兴趣了解什么是最好的方法以及我的问题在哪里。

new_react <- reactiveValues()
new_react$FilesReact <- 0 
new_react$SaveReact <- 0

invalidate <- function(x) x <- x+1

uploadedSave <- eventReactive(new_react$SaveReact,{ 
   # Set sentences >> Set words >> Graphics
})


uploadedFiles <- eventReactive(new_react$FilesReact,{ 
  # Select Sheets >> Select Columns >> Sentence Tokenization >> Word Tokenization >> Graphics
})

## I don't know how you are going to determine whether it's a data file
# or a save file...

if (its_a_save_file) {
  new_react$SaveReact <- invalidate(new_react$SaveReact)
  uploadedSave()
}else if (its_a_data_file) {
  new_react$FilesReact <- invalidate(new_react$FilesReact)
  uploadedFiles()

So basically I just define two reactive values which when invalidated by that simple function, will allow for the reactive functions, uploadedSave() or uploadedFiles() to be called. 因此,基本上我只定义了两个反应性值,当这些反应性值被该简单函数无效时,它们将允许调用反应性函数, uploadedSave()uploadedFiles()

Edit: To me, this just looks like a way to force eventReactive() to work like observeEvent() 编辑:对我来说,这似乎是一种强制eventReactive()类似于observeEvent()

Without more code, it is hard to be specific. 没有更多的代码,很难具体说明。 Here is an app. 这是一个应用程序。 that displays a different value based on what is the last control used. 根据最后使用的控件显示不同的值。 The controls are monitored using observeEvent() and each will put its input into a reactive value. 控件使用observeEvent()进行监视,并且每个控件都会将其input放入响应值。 Then the output just works with whatever is in the reactive value. 然后, output仅适用于无功值中的任何值。 For your case you need to have an observeEvent() for each of the controls you are using to do file and save uploads. 对于您的情况,您需要为要用来做文件和保存上载的每个控件有一个observeEvent() Then they each put their file into a common reactiveValue and that value is used for further processing. 然后,它们各自将文件放入一个公共的reactiveValue并将该值用于进一步处理。

library(shiny)

app<-shinyApp(
  ui=(fluidPage(sliderInput("First", min=1, max=10, label="Slider", value=1),
                actionButton("Second", label="Second Button"),
                textOutput("OutText"))
    ),

  server=function(input,output){

    RV<-reactiveValues(data=NULL)

    observeEvent(input$First,
      RV$Text<-input$First)

    observeEvent(input$Second,
       RV$Text<-"Second")

    output$OutText<-renderText({
      req(RV$Text)
      RV$Text
    })
  }
)

runApp(app)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM