【问题标题】:Shiny submitButton not updating my result闪亮的 submitButton 没有更新我的结果
【发布时间】:2016-08-29 17:20:42
【问题描述】:

我正在使用 Shiny 使我的 Rcode 具有交互性。在第一次运行时,我得到了一个初步结果My initial Shiny output。我遇到的问题是在更改输入后应该更新我的结果的 submitButton 不起作用。我搜索了stackoverflow,找不到解决方案。如果您能提供帮助,我会很高兴。

我的 ui.R 代码是:

      library(shiny)
      source("travelcbr1.R")

      shinyUI(fluidPage(

        # Application title.
        titlePanel("TRAVEL RECOMMENDATION"),


        sidebarLayout(
          sidebarPanel(
            selectInput("HolidayType", "Choose an holiday type:", 
                        choices = c("Bathing", "Active", "Education", "Recreation", "Wandering", "Language", "Skiing", "City")),

            selectInput("Transportation", "Choose a means of Transportation:", 
                        choices = c("Plane", "Train", "Coach", "Car")),

            selectInput("Accomodation", "Choose an Accomodation:", 
                        choices = c("One Star", "Two Stars", "Three Stars", "Four Stars", "Five Stars", "Holiday Flat")),

            selectInput("Duration", "Duration (days):", 
                        choices = c("less than 4", "4 - 6", "7 - 9", "10 - 12", "13 - 15", "16 - 20", "more than 20" )),

            numericInput("Price", "Price:", 500),
            # selectInput("Price", "Price ($):",
            #             choices = c("less than 500", "500 - 1000", "1000 - 1500", "1500 - 2000", "2000 - 2500", "2500 - 3000", "3500 - 4000", "4000 - 4500", "500+" )),

            selectInput("Season", "Season:", 
                        choices = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")),

            selectInput("NumberOfPersons", "Number Of Persons:", choices = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12 and above")),

            helpText("Click submit button to view your six best destinations"),

            submitButton("Update")
          ),


          mainPanel(

              tableOutput("view")
              # dataTableOutput("view")
          )
        )
      ))

我的 server.R 代码是这样的:

    library(shiny)

    source("travelcbr1.R")

    shinyServer(function(input, output) {

        output$view <- renderTable({

          Accomodation <- reactive({switch(input$Accomodation, 
                                           "Holiday Flat" = 6,
                                           "Five Stars" = 5,
                                           "Four Stars" = 4,
                                           "Three Stars" = 3,
                                           "Two Stars" = 2,
                                           "One Star" = 1)})

          Price <- reactive({input$Price})


          Transportation <- reactive({switch(input$Transportation, 
                                             "Plane" = 4,
                                             "Train" = 3,
                                             "Coach" = 2,
                                             "Car" = 1)})

          HolidayType <- reactive({switch(input$HolidayType, 
                                          "Active" = 8,
                                          "Skiing" = 7,
                                          "Bathing" = 6,
                                          "Wandering" = 5,
                                          "Recreation" = 4,
                                          "City" = 3,
                                          "Language"= 2,
                                          "Education" = 1)})

          Season <- reactive({switch(input$Season, "December" = 1, "January" = 1, "February" = 1,
                                     "March" = 2, "April" = 2, "May" = 2,
                                     "June" = 3, "July" = 3, "August" = 3,
                                     "September" = 4, "October" = 4, "November" = 4)})

          Duration <- reactive({switch(input$Duration, 
                                       "less than 4" = 1,
                                       "4 - 6" = 2,
                                       "7 - 9" = 3,
                                       "10 - 12" = 4,
                                       "13 - 15" = 5,
                                       "16 - 20" = 6,
                                       "more than 20" = 7
          )})


          NumberOfPersons <- reactive({switch(input$NumberOfPersons, 
                                              "12" = 12,
                                              "11" = 11,
                                              "10" = 10,
                                              "9" = 9,
                                              "8" = 8,
                                              "7" = 7,
                                              "6" = 6,
                                              "5" = 5,
                                              "4" = 4,
                                              "3" = 3,
                                              "2" = 2,
                                              "1" = 1
          )})

                  travelCBR(Accomodation(), 
                  NumberOfPersons(), 
                  Transportation(), 
                  HolidayType(), 
                  Season(), Duration(), Price())
        })



    })

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    我通常也尽量避免使用提交按钮。这是使用actionButton 的示例。使用eventReactive,您可以隔离表达式中的所有反应连接,并且只使用input$Update 作为触发器。

    顺便说一句,renderXXX 是一个响应式,因此您不必在 renderXXX 表达式中使用 reactive

    用户界面

    library(shiny)
    #source("travelcbr1.R")
    
    shinyUI(fluidPage(
    
      # Application title.
      titlePanel("TRAVEL RECOMMENDATION"),
    
    
      sidebarLayout(
        sidebarPanel(
          selectInput("HolidayType", "Choose an holiday type:", 
                      choices = c("Bathing", "Active", "Education", "Recreation", "Wandering", "Language", "Skiing", "City")),
    
          selectInput("Transportation", "Choose a means of Transportation:", 
                      choices = c("Plane", "Train", "Coach", "Car")),
    
          selectInput("Accomodation", "Choose an Accomodation:", 
                      choices = c("One Star", "Two Stars", "Three Stars", "Four Stars", "Five Stars", "Holiday Flat")),
    
          selectInput("Duration", "Duration (days):", 
                      choices = c("less than 4", "4 - 6", "7 - 9", "10 - 12", "13 - 15", "16 - 20", "more than 20" )),
    
          numericInput("Price", "Price:", 500),
          # selectInput("Price", "Price ($):",
          #             choices = c("less than 500", "500 - 1000", "1000 - 1500", "1500 - 2000", "2000 - 2500", "2500 - 3000", "3500 - 4000", "4000 - 4500", "500+" )),
    
          selectInput("Season", "Season:", 
                      choices = c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")),
    
          selectInput("NumberOfPersons", "Number Of Persons:", choices = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12 and above")),
    
          helpText("Click submit button to view your six best destinations"),
    
          actionButton("Update", "Update")
        ),
    
    
        mainPanel(
          verbatimTextOutput("view")
          # dataTableOutput("view")
        )
      )
    ))
    

    服务器

    library(shiny)
    
    #source("travelcbr1.R")
    
    shinyServer(function(input, output) {
    
      View <- eventReactive(input$Update, {
    
        Accomodation <- switch(input$Accomodation, 
                                         "Holiday Flat" = 6,
                                         "Five Stars" = 5,
                                         "Four Stars" = 4,
                                         "Three Stars" = 3,
                                         "Two Stars" = 2,
                                         "One Star" = 1)
    
        Price <- input$Price
    
    
        Transportation <- switch(input$Transportation, 
                                           "Plane" = 4,
                                           "Train" = 3,
                                           "Coach" = 2,
                                           "Car" = 1)
    
        HolidayType <- switch(input$HolidayType, 
                                        "Active" = 8,
                                        "Skiing" = 7,
                                        "Bathing" = 6,
                                        "Wandering" = 5,
                                        "Recreation" = 4,
                                        "City" = 3,
                                        "Language"= 2,
                                        "Education" = 1)
    
        Season <- switch(input$Season, "December" = 1, "January" = 1, "February" = 1,
                                   "March" = 2, "April" = 2, "May" = 2,
                                   "June" = 3, "July" = 3, "August" = 3,
                                   "September" = 4, "October" = 4, "November" = 4)
    
        Duration <- switch(input$Duration, 
                                     "less than 4" = 1,
                                     "4 - 6" = 2,
                                     "7 - 9" = 3,
                                     "10 - 12" = 4,
                                     "13 - 15" = 5,
                                     "16 - 20" = 6,
                                     "more than 20" = 7)
    
        NumberOfPersons <- switch(input$NumberOfPersons, 
                                            "12" = 12,
                                            "11" = 11,
                                            "10" = 10,
                                            "9" = 9,
                                            "8" = 8,
                                            "7" = 7,
                                            "6" = 6,
                                            "5" = 5,
                                            "4" = 4,
                                            "3" = 3,
                                            "2" = 2,
                                            "1" = 1)
    
        list(Accomodation = Accomodation, 
          NumberOfPersons = NumberOfPersons, 
          Transportation = Transportation, 
          HolidayType = HolidayType, 
          Season = Season, Duration = Duration, Price = Price)
    
        }, ignoreNULL = FALSE)
    
      output$view <- renderPrint(View())
    
    })
    

    【讨论】:

    • 谢谢。这个想法的工作原理是列表函数中的对象更新,但是当我用我的函数替换列表函数时,即 travelCBR(Accomodation = Accomodation, NumberOfPersons = NumberOfPersons, Transportation = Transportation, HolidayType = HolidayType, Season = Season, Duration = Duration , Price = Price),输出仍然没有更新。实际上,我希望参数值的变化应该更新结果。我还认为用我的函数替换服务代码中的列表函数应该会自动工作。
    • 啊,我想我知道了。尝试在没有参数默认值的情况下定义函数。所以,而不是fun(a = a) 只是fun(a)
    • 谢谢。我已将函数定义为没有参数的默认值(即travelCBR(Accomodation,NumberOfPersons,Transportation,HolidayType,Season,Duration,Price),但结果仍然没有更新。当我尝试运行基本rcode时,通过更改函数中的参数值,结果会发生变化。然后我尝试从闪亮手动更改函数中参数的值,结果没有改变。这是否意味着基本 rcode 没有反应性?我很抱歉. 我是 Shiny 的新手;我发现它的 GUI 很好。
    • 不,错误一定是别的。请问可以发一下功能吗?或者一个更好的,一个不能按预期工作的函数的最小示例?
    • 谢谢。函数就是这样:travelCBR(Accomodation, NumberOfPersons, Transportation, HolidayType, Season, Duration, Price)。这个函数是从闪亮的 server.R 调用的。我的基本 R 代码使用 source("travelcbr1.R") 部署到闪亮。 travelcbr1.R 文件中的整个 r 代码包含在 travelCBR() 函数中。当我通过改变 travelCBR() 中的参数值在不同时间运行基本 R 代码时,结果也如我预期的那样变化。但是当我用闪亮的方式调用 travelCBR() 并使用闪亮的输入控制台改变参数值时,结果仍然是静态的。
    【解决方案2】:

    考虑使用actionButton 而不是submitButton 并使用

    隔离反应性
    shinyServer(function(input,output) {
       observer({
          if(input$goButton != 0) {
             value$transportation <- <something>
          }
        })
        output$view <- renderTable({
           values$transportation
        })
      }
    

    `

    【讨论】:

    • 嗨,我使用了 actionButton 而不是 submitButton 以及 if(input$goButton != 0),但结果仍然没有更新。我想要的是结果应该在更改输入后更新。换句话说,我希望每次单击操作按钮后都会有新的结果,尤其是在输入更改后。
    • 我已经编辑了我的答案给你更多的指点。希望对你有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多