简体   繁体   English

R Shiny:如何在数据框中嵌入sliderInputs / selectInputs和radioButtons? (错误:无法将类“” shiny.tag””强制转换为data.frame)

[英]R shiny: how to embed sliderInputs/selectInputs and radioButtons in a data frame ? (Error: cannot coerce class “”shiny.tag“” to a data.frame)

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(). 它对于textInput()和numericInput()正常工作,但是我找不到selectInput(),sliderInput()和radioButton()的方法。

I can specify the textInput and the numericInput in HTML, using something like 我可以使用类似以下方式在HTML中指定textInput和numericInput

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

However I did not find the right way to specify the other types of inputs in HTML. 但是,我找不到在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. 但是结果不是闪亮的滑块。

So I tried to use the Shiny commands: 所以我尝试使用Shiny命令:

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

The Error that I get is: cannot coerce class ""shiny.tag"" to a data.frame. 我得到的错误是:无法将类““ shiny.tag”“强制转换为data.frame。 So I deduct that I should specify them in HTML -> does somebody know how to do this ? 所以我推断我应该在HTML中指定它们->有人知道该怎么做吗?

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 advance Cheers 任何建议/建议将不胜感激,并在此先感谢干杯 在此处输入图片说明

I've found the solution. 我找到了解决方案。 For radioButtons for ex, the code would be: 对于ex的radioButtons,代码为:

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... 希望这会帮助其他人...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM