【问题标题】:In Shiny, how to assign two functions to a new one as a reactive function在 Shiny 中,如何将两个函数分配给一个新函数作为反应函数
【发布时间】:2017-11-22 16:42:23
【问题描述】:

我已经编写了 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)

【问题讨论】:

  • 我没有收到任何错误,当我按下添加另一行时,rbind 工作正常。能否请您详细说明一下这个问题?
  • @amrrs 感谢您的回复。如果我想添加多行,它没有添加。它仅适用于前两组。它应该通过插入 x、y、a、b 的值来添加尽可能多的行。我正计划构建一个闪亮的应用程序,它将添加您输入的每组值作为 x,y,a,b。
  • 谢谢。我已经更新了多个 rbind 的答案。请检查。

标签: r shiny


【解决方案1】:

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

    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)

【讨论】:

  • 谢谢@amrrs。我几乎就在那里,除了每次单击添加另一组数据时都会出现第一组数据的重复。我期望一次只添加行。您的建议将不胜感激。
  • 请检查更新后的代码,其中z() 输出分配给fz()rbind 中删除
【解决方案2】:

避免重复的新代码如下:

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 2013-04-13
    相关资源
    最近更新 更多