2017-06-22 63 views
0

我正在研究Shiny中的測驗類型程序,它需要具有用表格答案更新的單選按鈕。我有下面的代碼,但單選按鈕不更新,答案保持2,3,4,5和6,儘管更改問題。任何想法,爲什麼這可能會發生,以及什麼可以解決這個問題?R Shiny單選按鈕不更新使用updateRadioButtons

library(shiny) 


ui <- fluidPage(

    selectInput("numberchoice",label = "Choose an image", choices = c(1:6), selected = 1) 
    , 
    imageOutput("image") 
    , 
    radioButtons("answerchoice", "Answers", choices = c(2:6)) 

) 

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

shinyApp(ui = ui, server = server) 
+0

'selectInput'需要用'UIOutput()'來構建。然後你可以使用'renderUI()'輸入來自UI輸出的輸入,它將在'server'函數中。複雜,我知道。見[這裏](https://shiny.rstudio.com/articles/dynamic-ui.html) – RobertMc

+0

@RobertMc好的,我已經完成了。我現在只是將問題的答案選項作爲「問題」圖表的標題,而不是圖表中的實際值。在我看來,「question [input $ numberchoice,2:6]」正在返回標題(我已經命名爲A到E),而不是正在讀取的csv中的實際答案選項 – Zack

+0

嗯,你能否用一些重現問題的示例數據更新你的問題? – RobertMc

回答

1

嘗試用observeEvent替換eventReactive。以下代碼適用於我。

library(shiny) 

ui <- fluidPage(
    selectInput("numberchoice", label = "Choose an image", choices = 1:6, selected = 1), 
    radioButtons("answerchoice", "Answers", choices = 1:6) 

) 

server <- function(input, output, session) { 
    observeEvent(input$numberchoice,{ 
    updateRadioButtons(session, "answerchoice", 
     choices = letters[1:input$numberchoice])}) 
} 

shinyApp(ui = ui, server = server) 

好像eventReactive沒有引發如此updateRadioButtons是沒有問題的。