本文介绍了R Shiny:如何在数据框中嵌入sliderInputs/selectInputs和radioButtons?(错误:无法将类别“" shiny.tag"强制为data.frame)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在矩阵中嵌入不同类型的输入.它适用于textInput()和numericInput(),但我找不到selectInput(),sliderInput()和radioButton()的方法.

I need to embed different types of Inputs in a matrix. It works fine for textInput() and numericInput(), but I can't find a way for selectInput(), sliderInput() and radioButton().

我可以使用

paste0("<input id='num", 1:2, "' class='shiny-bound-input' type='text' value=''>")

但是,我找不到在HTML中指定其他输入类型的正确方法.例如,对于滑块,我已经尝试过

However I did not find the right way to specify the other types of inputs in HTML. For the sliders for example, I've tried

paste0("<input id='Sl_", 1:2, "' class='jslider shiny-bound-input' type = 'range' value='20' min='0' max='100'>") 

但是结果不是闪亮的滑块.

but the result is not a shiny-type slider.

所以我尝试使用Shiny命令:

So I tried to use the Shiny commands:

lapply(1:2, function(i) sliderInput(inputId = paste0("sl", i), label = "", min = 0, max =100, value = 50)). 

我得到的错误是:无法将类"shiny.tag"强制转换为data.frame.所以我推断我应该在HTML中指定它们->有人知道该怎么做吗?

The Error that I get is: cannot coerce class ""shiny.tag"" to a data.frame. So I deduct that I should specify them in HTML -> does somebody know how to do this ?

这是代码(您可以在下面看到图片):

Here is the code (and you can see the picture here below):

ui = pageWithSidebar(
headerPanel("TEST"),
  sidebarPanel(
    helpText('Some types of input can be included in a matrix and some do not')
    ),
  mainPanel(
    uiOutput('matrix_text'),
    uiOutput('matrix_num'),
    uiOutput('matrix_slider'),
    uiOutput('matrix_select'),
    uiOutput('matrix_radioButtons')
  )
)

server = function(input,output){

  output$matrix_text <- renderTable({

    matrix_input <- paste0("<input id='num", 1:2, "' class='shiny-bound-input' type='text' value=''>")
    matrix <- data.frame(matrix_input)

    row.names(matrix) <- c("r 1","r 2")
    colnames(matrix) <- c('C 1')
    matrix
  },sanitize.text.function = function(x) x)


 output$matrix_num <- renderTable({

   matrix_input <- paste0("<input id='num", 1:2, "' class='shiny-bound-input' type='number' value=''>")
   matrix <- data.frame(matrix_input)

   row.names(matrix) <- c("r 1","r 2")
   colnames(matrix) <- c('C 1')
   matrix
 },sanitize.text.function = function(x) x)


output$matrix_slider <- renderTable({

  matrix_input <- lapply(1:2, function(i) sliderInput(inputId = paste0("sl", i), label = "", min = 0, max =100, value = 50))  

  matrix <- data.frame(matrix_input)

},sanitize.text.function = function(x) x)

output$matrix_select <- renderTable({

  matrix_input <- lapply(1:2, function(i) selectInput(inputId = paste0("select", i), label = "", choices = c("Cylinders" = "cyl","Transmission" = "am")))  

  matrix <- data.frame(matrix_input)

},sanitize.text.function = function(x) x)

output$matrix_radioButtons <- renderTable({

  matrix_input <- lapply(1:2, function(i) radioButtons(inputId = paste0("radio", i), label = "",choices= c("Cylinders" = "cyl","Transmission" = "am"), selected = NULL))  

  matrix <- data.frame(matrix_input)

},sanitize.text.function = function(x) x)


}    

runApp(list(ui = ui, server = server))  

任何建议/建议将不胜感激,并在此先感谢干杯

Any advice/suggestion would be highly appreciated and thanks in advanceCheers

推荐答案

我已经找到了解决方案.对于ex的radioButtons,代码为:

I've found the solution. For radioButtons for ex, the code would be:

paste0("<div class='control-group shiny-input-radiogroup shiny-bound-input' id='radio",1:2,"'>
    <label class='control-label' for=='radio",1:2,"'>Individual</label>
    <label class='radio'>
    <input id=='radio1",1:2,"' name='radio",1:2,"' type='radio' value='cyl'>
    <span>Cylinders</span>
    </label>
    <label class='radio'>
    <input id=='radio2",1:2,"' name='radio",1:2,"' type='radio' value='am'>
    <span>Transmission</span>
    </label>
    </div>")

可以通过右键单击带有一些单选按钮的网页并选择检查元素"来获得.

It is obtainable by right-clicking on a webpage with some radioButtons and selecting 'Inspect element'.

希望这对其他人有帮助...

Hope this will help others...

这篇关于R Shiny:如何在数据框中嵌入sliderInputs/selectInputs和radioButtons?(错误:无法将类别“" shiny.tag"强制为data.frame)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:09