2016-07-27 98 views
1

我是新的r閃亮,我試圖獲取選定的單選按鈕值作爲變量,然後連接它與其他東西。這裏是我的代碼:r閃亮 - 獲取單選按鈕值作爲變量

ui.R

library(shiny) 
shinyUI(fluidPage(
    titlePanel("This is test app"), 

    sidebarLayout(
    sidebarPanel(
     radioButtons("rd", 
        label="Select window size:", 
        choices=list("100","200","500","1000"), 
        selected="100") 
    ), 
    mainPanel(
     //Something 
    ) 
) 
)) 

server.R

library(shiny) 

shinyServer(function(input, output) { 


    ncount <- reactive({input$rd}) 
    print(ncount) 
    my_var <- paste(ncount,"100",sep="_") 

}) 

現在,當我打印ncount它打印出 「NCOUNT」,而不是存儲在變量中的值。有什麼,我在這裏失蹤。

感謝

回答

6

UI

library(shiny) 
shinyUI(fluidPage(
    titlePanel("This is test app"), 

    sidebarLayout(
    sidebarPanel(
     radioButtons("rd", 
        label = "Select window size:", 
        choices = list("100" = 100,"200" = 200,"500" = 500,"1000" = 1000), 
        selected = 100) 
    ), 
    mainPanel(
     verbatimTextOutput("ncount_2") 
    ) 
) 
)) 

服務器

library(shiny) 

shinyServer(function(input, output) { 


# The current application doesnt need reactive 

    output$ncount_2 <- renderPrint({ 
    ncount <- input$rd 
    paste(ncount,"100",sep="_") 
    }) 

    # However, if you need reactive for your actual data, comment the above part 
    # and use this instead 


    # ncount <- reactive({input$rd}) 
    # 
    # output$ncount_2 <- renderPrint({ 
    # paste(ncount(),"100",sep="_") 
    # }) 



}) 
+0

我沒有足夠的聲譽了投票你的答案,但感謝詳細的解釋。 – dagg3r