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

匿名 (未验证) 提交于 2019-12-03 01:12:01

问题:

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().

I can specify the textInput and the numericInput in HTML, using something like

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. 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:

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. 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 advance Cheers

回答1:

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...



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!