简体   繁体   English

更新文本多个操作按钮-R闪亮

[英]Update Text Multiple Action Buttons - R Shiny

Here is the scenario: 这是场景:

I have a data frame named stories generated by a function called say abc() of the following type: 我有一个名为stories的数据框,该数据框是由以下类型的称为say abc()的函数生成的:

  X1 X2 X3 X4 X5
1 a  e  i  m  q
2 b  f  j  n  r
3 c  g  k  o  s
4 d  h  l  p  t

Now I have given in the ui.r 5 action buttons say (Action 1,..., Action 5) 现在我在ui.r中给出了5个操作按钮,说(操作1,...,操作5)

Now when a user selects Action 1 I want to display stories[,1] similarly for others like Action 5 displays stories[,5] 现在,当用户选择动作1时,我想为其他人类似地显示stories[,1] ,例如动作5显示stories[,5]

To achieve this I have written an If statement unfortunately this only yields values based ONLY on what is last in my last if condition 为了实现这一点,不幸的是,我编写了一个If语句,它仅基于我的最后一个if条件中的最后一个产生值

Below is the server.R code: 下面是server.R代码:

shinyServer(
  function(input,output,session){
            abc()
    terms<-reactive({
      if(input$analyse5){
      isolate({
        withProgress({
          setProgress(message = "Analyzing")
          stories[,5]
        })
      })}
      if(input$analyse4){
      isolate({
        withProgress({
          setProgress(message = "Analyzing")
          stories[,4]
        })
      })}
    if(input$analyse3){
      isolate({
        withProgress({
          setProgress(message = "Analyzing")
          stories[,3]
        })
      })}
    if(input$analyse2){
      isolate({
        withProgress({
          setProgress(message = "Analyzing")
          stories[,2]
        })
      })}
    if(input$analyse1){
      isolate({
        withProgress({
          setProgress(message = "Analyzing")
          stories[,1]
        })
      })}
    })

    output$text1<-renderUI({
      HTML(paste(h2(terms()[1]),terms()[2],terms()[3],terms()[4],sep='<br/><br/>'))
    })
  }
)

Firstly only when I press the first action button it triggers [a,b,c,d] If any other action button is pressed first it yields nothing, similarly after the first Action Button is pressed and then other action Buttons it doesnot show respective columns in stories ie say Action 2 does not give stories[,2]=[e,f,g,h] instead it remains as [a,b,c,d] ideally which means the buttons are not responding 首先,只有当我按下第一个动作按钮时,它才会触发[a,b,c,d]如果首先按下任何其他动作按钮,则不会产生任何效果,类似地,在按下第一个动作按钮然后再按下其他动作按钮后,它不会显示相应的列在故事中,例如说动作2不给出stories[,2]=[e,f,g,h]而是理想地保持为[a,b,c,d] ,这意味着按钮没有响应

Below is the ui.r for your reference as well: 以下也是ui.r供您参考:

shinyUI(
  fluidPage((
    titlePanel('Some Headline')),
    sidebarLayout(
      sidebarPanel(
        actionButton("analyse1","Action 1"),
        actionButton("analyse2","Action 2"),
        actionButton("analyse3","Action 3"),
        actionButton("analyse4","Action 4"),
        actionButton("analyse5","Action 5")
        ),
   mainPanel(
        h4(htmlOutput("text1"))
      ))))

Edit: Tried using observeEvent 编辑:使用observeEvent尝试observeEvent

section of modified code: 修改后的代码部分:

terms<-reactive({
      observeEvent((input$analyse5),{
      isolate({
        withProgress({
          setProgress(message = "Loading 5th Article...")
          stories[,5]
        })
      })})
      observeEvent((input$analyse4),{
      isolate({
        withProgress({
          setProgress(message = "Loading 4th Article...")
          stories[,4]
        })
      })})

This is improved the situation: earlier I could see that all the action buttons were executing simultaneously (The Progress message of every scenario triggering) now i see only the respective button progress message on a click of the button, but am not able to get the output, it still remains that of the first one. 这可以改善这种情况:之前我可以看到所有操作按钮都同时执行(每个场景触发的“进度”消息),现在单击按钮时我只能看到相应的按钮进度消息,但无法获取输出,它仍然是第一个输出。

Please Help With Some Code Examples. 请帮助一些代码示例。

Update: When I add print(stories[,5]) or print(stories[,4]), it prints on the console for the click of the respective button but not on the Shiny App. 更新:当我添加print(stories [,5])或print(stories [,4])时,单击相应按钮即可在控制台上进行打印,但不能在Shiny App上单击。

Here's a full working app. 这是一个完整的工作应用程序。 I just got in to a computer, so I wasn't able to submit it sooner. 我刚进入计算机,所以无法尽快提交。 I submit it in case you still find it useful. 如果您仍然觉得有用,我会提交。

library(shiny)

stories <- matrix(letters[1:20], byrow = FALSE, nrow = 4)

shinyApp(
  ui = 
    shinyUI(
      fluidPage(
        titlePanel('Some Headline'),
      sidebarLayout(
        sidebarPanel(
          actionButton("analyse1","Action 1"),
          actionButton("analyse2","Action 2"),
          actionButton("analyse3","Action 3"),
          actionButton("analyse4","Action 4"),
          actionButton("analyse5","Action 5")
        ),
        mainPanel(
          h4(htmlOutput("text1"))
        )
      )
    )
  ),

  server = 
    shinyServer(function(input, output, session){
      Terms <- reactiveValues(
        Selection = 1
      )

      # using an observe block lets us listen for multiple events
      # without having to rewrite the code five times
      observe({
        lapply(sprintf("analyse%s", 1:5),
               function(x)
               {
                 observeEvent(input[[x]],
                              Terms$Selection <- as.numeric(sub("analyse", "", x)))
               })
      })

      output$text1<-renderUI({
        HTML(paste(stories[, Terms$Selection],collapse='<br/><br/>'))
      })

    })
)

With hints from @Benjamin I was able to solve this: 通过@Benjamin的提示,我得以解决此问题:

Here are the changes that worked (sampled code): 以下是有效的更改(示例代码):

terms<-reactiveValues(dta = NULL)

observeEvent(input$analyse1,{
            withProgress({
              setProgress(message = "Loading 1st Article...")
            terms$dta<-stories[,1]
          })
          })

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

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