r shiny - 获取单选按钮值作为变量

时间:2016-07-27 16:39:46

标签: r radio-button shiny

我是r new的新手,我试图将单选按钮的选定值作为变量,然后将其与其他东西连接起来。这是我的代码:

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”而不是存储在变量中的值。这里有什么我想念的。

由于

1 个答案:

答案 0 :(得分:7)

<强> 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="_")
  # })



})