Ahmad Tanwir
Ahmad Tanwir

Reputation: 133

In Shiny, how to assign two functions to a new one as a reactive function

I have written R code and tried to create a shiny app which will serve the same purpose with gui.

R code:

text1 <-data.frame(x=2,y=30) 
text1
text2 <- data.frame(a=11,b=10)
text2
# fisrt set of data
z<- cbind.data.frame(text1,text2)
z
# second set of data
nz <-cbind.data.frame(text1,text2)
nz

# combined data. my plan to change the z or nz, and it will reflect everytime, and save as z.
# every time I change any value of x,y,a,and b. it should add a new row
# couldn't able to write the following steps in shiny as reactive form
z <- rbind.data.frame(z,nz)
z

I have tried to prepare a shiny app but couldn't do the step of

z <- rbind.data.frame(z,nz)
z

In shiny. It would be helpful anyone has any idea. z will be overwritten by the row bind of z and nz. The shiny app code is given below:

library(shiny)

ui <- fluidPage(theme = shinytheme("sandstone"),

                # header
                headerPanel("DTI post analysis conversion"),

                sidebarLayout(
                  # sidebar for form
                  sidebarPanel(
                    h3("Information",""),
                    textInput("x", "Value X",""),
                    textInput("y", "Value Y",""),
                    textInput("a", "Value a",""),
                    textInput("b", "Value b",""),
                    actionButton("update", "first data Set"),
                    actionButton("update1", "Add another Set")),

                  # output for viewing
                  mainPanel(

                    DT::dataTableOutput("tableDT"),
                    #DT::dataTableOutput("tableDT1"),
                    DT::dataTableOutput("tableDT2")


                  )   
                )
)

server <- function(input, output, session) {

  text1 <- reactive({
    tx1 <- data.frame(X = input$x, 
                      Y = input$y)
  })


  text2 <- reactive({
    tx2 <- data.frame(A = input$a, 
                      B = input$b)
  })

# create 1st row of data  
z <- eventReactive(input$update,{
  cbind.data.frame(text1(), text2())
})

# create 2nd row of data  
nz <- eventReactive(input$update1,{
  cbind.data.frame(text1(), text2())
})

# everytime I change any value and click "add another data set", it should add a new row
# the problem is it only works for the first time.  
combine <- eventReactive(input$update1,{
  rbind.data.frame(z(), nz())
})

output$tableDT <- DT::renderDataTable(
  z()
)

output$tableDT1 <- DT::renderDataTable(
  nz()
)

output$tableDT2 <- DT::renderDataTable(
  combine()
)

}
shinyApp (ui = ui, server = server)

Upvotes: 1

Views: 736

Answers (2)

Ahmad Tanwir
Ahmad Tanwir

Reputation: 133

The new code which avoid the duplication is given below:

library(shiny)

ui <- fluidPage(theme = shinytheme("sandstone"),

                # header
                headerPanel("DTI post analysis conversion"),

                sidebarLayout(
                  # sidebar for form
                  sidebarPanel(
                    h3("Information",""),
                    textInput("x", "Value X",""),
                    textInput("y", "Value Y",""),
                    textInput("a", "Value a",""),
                    textInput("b", "Value b",""),
                    actionButton("update", "first data Set"),
                    actionButton("update1", "Add another Set")),

                  # output for viewing
                  mainPanel(

                    DT::dataTableOutput("tableDT"),
                    #DT::dataTableOutput("tableDT1"),
                    DT::dataTableOutput("tableDT2")


                  )   
                )
)

f <- data.frame()

server <- function(input, output, session) {

  text1 <- reactive({
    tx1 <- data.frame(X = input$x, 
                      Y = input$y)
  })


  text2 <- reactive({
    tx2 <- data.frame(A = input$a, 
                      B = input$b)
  })

  # create 1st row of data  
  z <- eventReactive(input$update,{
    f <<- cbind.data.frame(text1(), text2())
  })

  # create 2nd row of data  
  nz <- eventReactive(input$update1,{
    cbind.data.frame(text1(), text2())
  })

  # everytime I change any value and click "add another data set", it should add a new row
  # the problem is it only works for the first time.  
  combine <- eventReactive(input$update1,{
    f <<- rbind.data.frame(z(), nz())
  })

  # this step avoid the duplication of add rows
  combine2<- eventReactive(input$update1,{
    f<<- rbind.data.frame(f, nz())

  })

  output$tableDT <- DT::renderDataTable(
    z()
  )

  output$tableDT1 <- DT::renderDataTable(
    nz()
  )

  output$tableDT2 <- DT::renderDataTable(
    combine2()
  )

}
shinyApp (ui = ui, server = server)

Upvotes: 0

amrrs
amrrs

Reputation: 6325

I've tried to assign the combined value to a global object and then use it everytime the button is clicked. Please check if this helps.

    library(shiny)

ui <- fluidPage(theme = shinytheme("sandstone"),

                # header
                headerPanel("DTI post analysis conversion"),

                sidebarLayout(
                  # sidebar for form
                  sidebarPanel(
                    h3("Information",""),
                    textInput("x", "Value X",""),
                    textInput("y", "Value Y",""),
                    textInput("a", "Value a",""),
                    textInput("b", "Value b",""),
                    actionButton("update", "first data Set"),
                    actionButton("update1", "Add another Set")),

                  # output for viewing
                  mainPanel(

                    DT::dataTableOutput("tableDT"),
                    #DT::dataTableOutput("tableDT1"),
                    DT::dataTableOutput("tableDT2")


                  )   
                )
)

f <- data.frame()

server <- function(input, output, session) {

  text1 <- reactive({
    tx1 <- data.frame(X = input$x, 
                      Y = input$y)
  })


  text2 <- reactive({
    tx2 <- data.frame(A = input$a, 
                      B = input$b)
  })

  # create 1st row of data  
  z <- eventReactive(input$update,{
    f <<- cbind.data.frame(text1(), text2())
    f
  })

  # create 2nd row of data  
  nz <- eventReactive(input$update1,{
    cbind.data.frame(text1(), text2())
  })

  # everytime I change any value and click "add another data set", it should add a new row
  # the problem is it only works for the first time.  
  combine <- eventReactive(input$update1,{

    f <<- rbind.data.frame(f, nz())

  })

  output$tableDT <- DT::renderDataTable(
    z()
  )

  output$tableDT1 <- DT::renderDataTable(
    nz()
  )

  output$tableDT2 <- DT::renderDataTable(
    combine()
  )

}
shinyApp (ui = ui, server = server)

Upvotes: 1

Related Questions