在Shiny中,如何将两个函数分配给新函数作为反应函数

时间:2017-11-21 21:35:01

标签: r shiny

我已经编写了R代码并试图创建一个闪亮的应用程序,它将与gui起到同样的作用。

R代码:

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

我试图准备一个闪亮的应用程序,但无法完成

的步骤
z <- rbind.data.frame(z,nz)
z

闪亮。任何人都有任何想法会有所帮助。 z将被z和nz的行绑定覆盖。闪亮的应用程序代码如下:

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)

2 个答案:

答案 0 :(得分:1)

我尝试将combine d值分配给全局对象,然后在每次单击按钮时使用它。请检查这是否有帮助。

    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)

答案 1 :(得分:0)

避免重复的新代码如下:

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)