【问题标题】:Resetting shiny app after pressing submit button按下提交按钮后重置闪亮的应用程序
【发布时间】:2019-02-02 12:34:30
【问题描述】:

我有一个功能强大的闪亮应用程序,其逻辑描述如下:

应用逻辑:

用户使用 selectInput()“标签”选择其中一个测试。这是主要操作,然后他可以修改其名称,例如 Test 1 到 Test A。然后用户可以通过 numericInput() "Items in Test" 在 Test 中添加项目。这些是总项目。正如您将看到的,“测试中的项目”的数量与所选测试的 hot3 表中的“可用”列相同。通过“选择项目”,他可以选择要在 hot5 表中显示的特定项目。然后用户可以点击 hot5 表来选择一个特定的项目,选择项目(或行)的数量会显示在 hot3 表中的“Sel”列下,用于这个特定的测试。 “选择的项目”仅显示在“选择项目”中选择的项目数量。请注意,发生在表上的每次修改都不依赖于其他小部件。例如,这意味着不需要更改标签名称。

问题:

请查看随附的屏幕截图。我将测试 2 的标签更改为测试 B,添加了 4 个项目,但没有使用 -click on the row - 功能选择它们。然后我按下提交按钮,我意识到在此之后,“标签”的设置变回测试 1,但测试 B/测试 2 的所有设置都保留在我的应用程序中。这看起来好像我对测试 1 有相同的设置。我希望在按下提交按钮或选择另一个测试(“标签”)后,显示的设置是您在“标签”下选择的测试。所以如果按下它会进行某种重置,以便它对应于该测试的实际设置。

应用:

library(shiny)
library(DT)
library(rhandsontable)
#library(tidyverse)

ui <- navbarPage(
  "Application",
  tabPanel("Booklets",
           sidebarLayout(
             sidebarPanel(
               uiOutput("tex2"),
               rHandsontableOutput("hot3")
             ),
             mainPanel(
               fluidRow(
                 wellPanel(
                   fluidRow(
                     column(4,
                            DT::dataTableOutput("hot5")
                     ),
                     column(4,
                            fluidRow(
                              uiOutput("book3"),
                              uiOutput("book6")

                            ),
                            fluidRow(
                              uiOutput("book1"),
                              uiOutput("book10"),
                              uiOutput("book11")
                            ),
                            fluidRow(actionButton("submit","submit"))
                     )
                   ))
               )
             )
           )
  )
)
#server
server <- function(input, output, session) {

  rv<-reactiveValues()

  output$tex2<-renderUI({
    numericInput("text2", "#tests", value = 1, min=1)
  })

  output$book1<-renderUI({
    numericInput("bk1",
                 "Items in test",
                 value = 1,
                 min = 1)
  })

  output$book3<-renderUI({

    selectInput("bk3",
                "Label",
                choices=(paste("Test",1:input$text2)))

  })


  output$book6<-renderUI({
    textInput("bk6", "Change to",
              value=NULL
    )
  })


  output$book10<-renderUI({
    # changed from selectize
    selectizeInput(
      "bk10", "Select Items", choices =1:10000,multiple =T,selected = 1,
      options = list(maxItems = input$bk1))#changed from
  })
  output$book11<-renderUI({
    textInput("bk11", "Items chosen",
              value = nrow(rt5())
    )
  })

  #rt4<-reactive({
  observe({
    req(input$text2)

    rv$rt4 = data.frame(
      SNo = rep(TRUE, input$text2),
      Test=paste(1:input$text2),
      Label=paste("Test",1:input$text2),
      Avail=1L,
      Sel =as.integer(rep.int(0,input$text2)),
      stringsAsFactors = FALSE)
  })

  observeEvent(input$submit,{

    # rt4 <- reactive({
    if (is.null( rv$rt4))
      return(NULL)

    if(!is.null(input$bk6) && input$bk6!=""){
      rv$rt4[ rv$rt4$Label==input$bk3, "Avail"] <- input$bk1
      rv$rt4[ rv$rt4$Label==(input$bk3), "Sel"] <- length(input$hot5_rows_selected)

      rv$rt4[ rv$rt4$Label==input$bk3, "Label"] <- input$bk6
    }
    else
    {
      rv$rt4[ rv$rt4$Label==input$bk3, "Avail"] <- input$bk1
      rv$rt4[ rv$rt4$Label==(input$bk3), "Sel"] <- length(input$hot5_rows_selected)

      #rv$rt4[ rv$rt4$Label==input$bk3, "Label"] <- input$bk6

    }
  })

  observeEvent(input$submit,{

    updateSelectInput(session,"bk3","Label", choices=rv$rt4$Label)
  }
  )


  rt55<-reactive({
    DF=data.frame(
      Id=  input$bk10,
      Label=paste("Item",input$bk10),
      Pf=0,
      stringsAsFactors = FALSE
    )
  })

  rt5<-reactive({
    DF=data.frame(
      Id=  input$bk10,
      Label=paste("Item",input$bk10),
      Pf=0,
      stringsAsFactors = FALSE
    )
    cbind(id=rowSelected(), DF)
  })

  rowSelected <- reactive({
    x <- numeric(nrow(rt55()))
    x[input$hot5_rows_selected] <- 1
    x
  })

  output$hot5 <- renderDT(datatable(rt5()[,-1],
                                    selection = list(mode = "multiple",
                                                     selected = (1:nrow(rt5()[,-1]))[as.logical(rowSelected())],
                                                     target = "row"),rownames = F)
  )

  output$hot3 <-renderRHandsontable({
    req(input$text2)
    rhandsontable(rv$rt4)
  })
}
shinyApp(ui,server)

【问题讨论】:

  • " 我希望能够在按下提交按钮或选择另一个测试后显示您在“标签”下选择的测试的设置。当您说“另一个测试”时,您指的是到 input$text2 或 input$bk3 (因为两者都称为测试)。
  • 有用的评论。我指的是 input$bk3 ('Label')。

标签: r shiny


【解决方案1】:

试试这个。测试时我碰巧在 ui 中定义了 bk6,但是您可以使用 renderUI 公式,它仍然可以工作。此外,您可以将两个 observeEvent 块合并为一个。

library(shiny)
library(DT)
library(rhandsontable)
#library(tidyverse)

ui <- navbarPage(
  "Application",
  tabPanel("Booklets",
           sidebarLayout(
             sidebarPanel(
               uiOutput("tex2"),
               rHandsontableOutput("hot3")
             ),
             mainPanel(
               fluidRow(
                 wellPanel(
                   fluidRow(
                     column(4,
                            DT::dataTableOutput("hot5")
                     ),
                     column(4,
                            fluidRow(
                              uiOutput("book3"),
                             textInput("bk6", "Change to",value="")

                            ),
                            fluidRow(
                              uiOutput("book1"),
                              uiOutput("book10"),
                              uiOutput("book11")
                            ),
                            fluidRow(actionButton("submit","submit"))
                     )
                   ))
               )
             )
           )
  )
)
#server
server <- function(input, output, session) {

  rv<-reactiveValues()

  output$tex2<-renderUI({
    numericInput("text2", "#tests", value = 1, min=1)
  })

  output$book1<-renderUI({
    numericInput("bk1",
                 "Items in test",
                 value = 1,
                 min = 1)
  })

  output$book3<-renderUI({

    selectInput("bk3",
                "Label",
                choices=(paste("Test",1:input$text2)),
                selected = rv$selected)

  })
  observeEvent(input$submit,{

    if(!is.null(input$bk6) && input$bk6!=""){
      rv$selected <- input$bk6
    }
    else
      rv$selected <- input$bk3
     }
  )
  # output$book6<-renderUI({
  #   textInput("bk6", "Change to",
  #             value=""
  #   )
  # })


  output$book10<-renderUI({
    # changed from selectize
    selectizeInput(
      "bk10", "Select Items", choices =1:10000,multiple =T,selected = 1,
      options = list(maxItems = input$bk1))#changed from
  })
  output$book11<-renderUI({
    textInput("bk11", "Items chosen",
              value = nrow(rt5())
    )
  })

  #rt4<-reactive({
  observe({
    req(input$text2)

    rv$rt4 = data.frame(
      SNo = rep(TRUE, input$text2),
      Test=paste(1:input$text2),
      Label=paste("Test",1:input$text2),
      Avail=1L,
      Sel =as.integer(rep.int(0,input$text2)),
      stringsAsFactors = FALSE)
  })

  observeEvent(input$submit,{

    # rt4 <- reactive({
    if (is.null( rv$rt4))
      return(NULL)

    if(!is.null(input$bk6) && input$bk6!=""){
      rv$rt4[ rv$rt4$Label==input$bk3, "Avail"] <- input$bk1
      rv$rt4[ rv$rt4$Label==(input$bk3), "Sel"] <- length(input$hot5_rows_selected)

      rv$rt4[ rv$rt4$Label==input$bk3, "Label"] <- input$bk6
    }
    else
    {
      rv$rt4[ rv$rt4$Label==input$bk3, "Avail"] <- input$bk1
      rv$rt4[ rv$rt4$Label==(input$bk3), "Sel"] <- length(input$hot5_rows_selected)

      #rv$rt4[ rv$rt4$Label==input$bk3, "Label"] <- input$bk6

    }
  })

  observeEvent(input$submit,{

    updateSelectInput(session,"bk3","Label", choices=rv$rt4$Label,
                      selected = rv$selected)
    updateTextInput(session, "bk6", value = "")
    print(rv$selected)

  }
  )


  rt55<-reactive({
    DF=data.frame(
      Id=  input$bk10,
      Label=paste("Item",input$bk10),
      Pf=0,
      stringsAsFactors = FALSE
    )
  })

  rt5<-reactive({
    DF=data.frame(
      Id=  input$bk10,
      Label=paste("Item",input$bk10),
      Pf=0,
      stringsAsFactors = FALSE
    )
    cbind(id=rowSelected(), DF)
  })

  rowSelected <- reactive({
    x <- numeric(nrow(rt55()))
    x[input$hot5_rows_selected] <- 1
    x
  })

  output$hot5 <- renderDT(datatable(rt5()[,-1],
                                    selection = list(mode = "multiple",
                                                     selected = (1:nrow(rt5()[,-1]))[as.logical(rowSelected())],
                                                     target = "row"),rownames = F)
  )

  output$hot3 <-renderRHandsontable({
    req(input$text2)
    rhandsontable(rv$rt4)
  })
}
shinyApp(ui,server)

【讨论】:

  • 我发现了一些功能问题并为其创建了一个新 Q。
  • 嗨,您能帮我解决与您构建的应用程序相关的问题吗?谢谢
猜你喜欢
  • 2021-12-05
  • 2014-08-07
  • 2014-10-04
  • 2013-03-22
  • 2020-08-12
  • 1970-01-01
  • 1970-01-01
  • 2020-10-03
  • 1970-01-01
相关资源
最近更新 更多