2017-10-12 64 views
0

我正在做一些事情我很有興趣並且想知道是否有可能讓單選按鈕決定另一個輸入。R Shiny:使單選按鈕影響其他輸入

會複製我的問題,一種玩具,例如由下式給出:

if (interactive()) { 
    ui <- fluidPage(
    radioButtons("dist", "Distribution type:", 
       c("Normal" = "norm", 
        "Uniform" = "unif")), 
    textInput(inputId = "textid", label = "Text input-header", value = "rnorm"), 
    plotOutput("distPlot") 
) 

    server <- function(input, output) { 
    df <- reactive({ 
     switch(input$textid, 
      rnorm = {rnorm(500)}, 
      uni = {runif(500)}, 
      exp = {rexp(500)}, 
    )}) 

    output$distPlot <- renderPlot(hist(df()) 
    ) 
    } 

    shinyApp(ui, server) 
} 

因爲它是現在,它是在決定什麼樣的分佈,用來生成數據的文本框中輸入。我想要的是,當點擊其中一個單選按鈕時,文本會在文本框中更新(例如,如果選中「Uniform」,那麼textunput將更新爲「uni」 - 並因此使用此分佈)。問題是我需要這個盒子,因爲我希望能夠選擇單選按鈕之間的某個選項,所以它不適用於我添加額外的單選按鈕。在這種情況下,額外的選項是,可以在文本輸入中寫入「exp」(此分佈不能從單選按鈕中選擇)。

在這個例子中,這可能看起來有點愚蠢,但在我的情況下,我有2個非常經常使用的時間戳,但應用程序必須允許用戶選擇每個可能的日期。

這是不是有可能?

在此先感謝!

回答

0

像這樣的東西?請注意,我說的req它需要將文本輸入爲"rnorm","uni","exp"

library(shiny) 
if (interactive()) { 
    ui <- fluidPage(
    radioButtons("dist", "Distribution type:",c("Normal" = "rnorm","Uniform" = "uni","Exponential" = "exp")), 
    textInput(inputId = "textid", label = "Text input-header", value = "rnorm"), 
    plotOutput("distPlot") 
) 

    server <- function(input, output,session) { 

    observeEvent(input$dist,{ 
     updateTextInput(session, "textid",label = "Text input-header",value = input$dist) 

    }) 

    df <- eventReactive(input$textid,{ 
     req(input$textid %in%c("rnorm","uni","exp")) 
     switch(input$textid, rnorm = {rnorm(500)},uni = {runif(500)}, exp = {rexp(500)}, 
    )}) 
    output$distPlot <- renderPlot(hist(df())) 
    } 
    shinyApp(ui, server) 
} 
+0

一個太謝謝你了! (沒有指數單選按鈕,這正是我想要的!) 感謝! – Dorthe

+0

@Dorthe https://stackoverflow.com/help/someone-answers –

相關問題