【问题标题】:Creating Radio Buttons that Update based on an Input in R Shiny创建基于 R Shiny 中的输入更新的单选按钮
【发布时间】:2017-06-17 01:17:46
【问题描述】:

我希望在闪亮的应用程序中创建一组单选按钮,这些单选按钮将根据用户输入进行更新。基本上,用户会选择一个问题,相应的答案选择将是单选按钮的更新选项。我无法让按钮更新。谁能指出我正确的方向?

非常抱歉,第一次使用这个网站。

到目前为止,我有:

questions <- read.csv("~/Answers.csv")
library(shiny)


ui <- fluidPage(

  selectInput("numberchoice",label = "Choose an image", choices = c(1:6), 
 selected = 1)
,
imageOutput("image")
,
radioButtons("answerchoice","",choiceNames = c("A","B","C","D","E"), 
choiceValues = questions[3,2:6])

)

server <- function(input,output,session) {
answers <- read.csv("~Answers.csv")
output$image <- renderImage(list(src=
paste("~",
input$numberchoice,".png", sep = "")
,contentType = "image/png", alt = "Face"),deleteFile = FALSE)
updateRadioButtons(session,"answerchoice",choices = 
questions[input$numberchoice,2:5]


}

shinyApp(ui = ui, server = server)`

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您唯一需要做的就是对更改执行observe input$numberchoice 并在更改发生后更新radioButtons:

    # observe input$numberchoice and update answer radioButtons accordingly
    observeEvent(input$numberchoice, {
        updateRadioButtons(session,"answerchoice",choices = questions[input$numberchoice,2:5])
    })
    

    或者,如果你想避免observeEvent,你可以把整个答案输出放在服务器上,这样每次input$numberchoice改变时它都会更新:

    用户界面:

    uiOutput("answerchoice")
    

    服务器:

    output$answerchoice <- renderUI({
        radioButtons("answerchoice", "", choiceNames = c("A","B","C","D","E"), 
                     choiceValues = questions[input$numberchoice,2:5])
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-11
      • 2019-01-29
      • 2021-11-01
      • 2018-03-24
      • 2014-10-27
      • 2018-03-14
      • 2016-10-26
      • 2015-10-03
      相关资源
      最近更新 更多