2016-03-03 17 views
5

मैं कई विकल्प वस्तुओं के लिए एक मूल्यांकन उपकरण के रूप में चमक का उपयोग करने की कोशिश कर रहा हूं। ऐसे में, कुछ मामलों में मैं एक छवि के रूप में एक छवि चाहते हैं। इसके बजाय, कच्चे HTML दिखाया गया है। क्या यह चमकदार में किया जा सकता है?क्या आपके पास चमकदार में रेडियोबटन पसंद के रूप में एक छवि हो सकती है?

library(shiny) 

choices <- c('\\(e^{i \\pi} + 1 = 0 \\)' = 'equation', 
      '<img src="Rlogo.png">' = 'logo') 

ui <- shinyUI(fluidPage(
    withMathJax(), 
    img(src='Rlogo.png'), 
    fluidRow(column(width=12, 
     radioButtons('test', 'Radio buttons with MathJax choices', 
        choices = choices, inline = TRUE), 
     br(), 
     h3(textOutput('selected')) 
    )) 
)) 

server <- shinyServer(function(input, output) { 
    output$selected <- renderText({ 
     paste0('You selected the ', input$test) 
    }) 
}) 

shinyApp(ui = ui, server = server) 

आप www निर्देशिका में आर लोगो डाल करने के लिए जहाँ आप इस app.r स्क्रिप्ट जगह होगा। http://i1.wp.com/www.r-bloggers.com/wp-content/uploads/2016/02/Rlogo.png?resize=300%2C263

उत्तर

3

img क्योंकि नाम span में आयोजित और का उपयोग कर tags$span तो सभी HTML बच रहा है उत्पन्न कर रहे हैं रेडियो बटन में प्रदर्शित नहीं किया जाता: यहाँ लोगो के लिए एक सीधा संबंध है।

आप केवल एक बार ही करना है, तो आप, radioButtons('test', 'Radio buttons with MathJax choices', choices = choices, inline = TRUE) के उत्पादन में कॉपी कर सकते हैं एक tags$div में रखें, और जोड़ने छवि:

 fluidRow(column(width=12, 
         tags$div(HTML('<div id="test" class="form-group shiny-input-radiogroup shiny-input-container shiny-input-container-inline"> 
    <label class="control-label" for="test">Radio buttons with MathJax choices</label> 
             <div class="shiny-options-group"> 
             <label class="radio-inline"> 
             <input type="radio" name="test" value="equation" checked="checked"/> 
             <span>\\(e^{i \\pi} + 1 = 0 \\)</span> 
             </label> 
             <label class="radio-inline"> 
             <input type="radio" name="test" value="logo"/> 
             <span><img src="http://i1.wp.com/www.r-bloggers.com/wp-content/uploads/2016/02/Rlogo.png?resize=300%2C263"/></span> 
             </label> 
             </div> 
             </div> ')), 
         br(), 
         h3(textOutput('selected')) 
     )) 

आप इस कई बार करने की जरूरत है, तो आप कर सकते हैं एक radioButtons_withHTML समारोह को परिभाषित:

radioButtons_withHTML <- function (inputId, label, choices, selected = NULL, inline = FALSE, 
      width = NULL) 
{ 
     choices <- shiny:::choicesWithNames(choices) 
     selected <- if (is.null(selected)) 
       choices[[1]] 
     else { 
       shiny:::validateSelected(selected, choices, inputId) 
     } 
     if (length(selected) > 1) 
       stop("The 'selected' argument must be of length 1") 
     options <- generateOptions_withHTML(inputId, choices, selected, inline, 
            type = "radio") 
     divClass <- "form-group shiny-input-radiogroup shiny-input-container" 
     if (inline) 
       divClass <- paste(divClass, "shiny-input-container-inline") 
     tags$div(id = inputId, style = if (!is.null(width)) 
       paste0("width: ", validateCssUnit(width), ";"), class = divClass, 
       shiny:::controlLabel(inputId, label), options) 
} 

generateOptions_withHTML <- function (inputId, choices, selected, inline, type = "checkbox") 
{ 
     options <- mapply(choices, names(choices), FUN = function(value, 
                    name) { 
       inputTag <- tags$input(type = type, name = inputId, value = value) 
       if (value %in% selected) 
         inputTag$attribs$checked <- "checked" 
       if (inline) { 
         tags$label(class = paste0(type, "-inline"), inputTag, 
            tags$span(HTML(name))) 
       } 
       else { 
         tags$div(class = type, tags$label(inputTag, tags$span(HTML(name)))) 
       } 
     }, SIMPLIFY = FALSE, USE.NAMES = FALSE) 
     div(class = "shiny-options-group", options) 
} 

मूल एक साथ अंतर कॉल generateOptions_withHTML बटन के नाम बनाने के लिए है, और मैंआसपास HTML() समारोह जोड़ा बचने से रोकने के लिए tags$span में 210। आप इन कार्यों को किसी अन्य फ़ाइल में डाल सकते हैं और source का उपयोग कर सकते हैं।

फिर आप radioButtons बनाने के लिए radioButtons_withHTML('test', 'Radio buttons with MathJax choices',choices = choices, inline = TRUE) का उपयोग कर सकते हैं।

+0

धन्यवाद! मुझे ठीक इसी की आवश्यकता थी। अच्छा होगा अगर Rstudio ने इसे मुख्य चमकदार रिलीज में शामिल किया। – jbryer

संबंधित मुद्दे

 संबंधित मुद्दे