简体   繁体   中英

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:

  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)

Now when a user selects Action 1 I want to display stories[,1] similarly for others like Action 5 displays 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

Below is the server.R code:

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

Below is the ui.r for your reference as well:

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

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.

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:

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]
          })
          })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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