Search code examples
rshinyshiny-servershinyapps

Names of a numeric vector with HTML


Explaining the code first:

I'm programming an app in RStudio with Shiny, and I have an observer that makes a lot of loops to find some results in a vector and, I don't know why, the vector is generated as a character vector, so I convert it to a numeric vector.

And I also take one position out of him, because at this point, it's not useful anymore.

I also tried to name the positions of the vector with some symbols using HTML and with some common characters or strings.

Then, I use "t()" so the vector can be shown horizontally.

In the beginning of the code below, I defined a function that generates a matrix with 6 columns: the columns 2, 3, 4, 5 and 6 contain values of normal distribuition with the parameter definid as inputs to the function. The firt column contains the count of values between LI and LS, that are aldo inputs of the fucntion.

After, in the code, I call the function and use the values stored in two positions of the vector as the inputs LI and LS.

# Function to generate normal data with the first column containing the count of values between LI and LS
generate_normal_data <- function(n, mi, sigma, LI, LS) {
  # Generate matrix of normal samples
  normal_matrix <- matrix(rnorm(n * 100, mean = mi, sd = sigma), ncol = n)
  
  # Initialize the final matrix with NA values
  result_matrix <- matrix(NA, nrow = 100, ncol = n + 1)
  
  # Fill the first column with the count of values between LI and LS in each row
  for (i in 1:100) {
    result_matrix[i, 1] <- sum(normal_matrix[i, 1:n] > LI & normal_matrix[i, 1:n] < LS)
  }
  
  # Fill the remaining columns with normal samples
  result_matrix[, 2:(n + 1)] <- normal_matrix
  
  return(result_matrix)
}






ui <- fluidPage(
  
  
  theme = bs_theme(version = 4, bootswatch = "sandstone"),
  
  titlePanel("TITLE"),
  
  sidebarLayout(
    sidebarPanel(
      numericInput("n", "n"),
      numericInput("m", "m"),
      numericInput("sigma", "sigma"),
      numericInput("S2", "S2"),
      numericInput("alpha", "alpha"),
      numericInput("lambda", "lambda"),
      numericInput("beta", "beta"),
      actionButton("runButton", "Run")
    ),
    
    mainPanel(
      tabsetPanel(
        tabPanel("Results and chart", 
                 tableOutput("results"),
                 plotOutput("chart")),
        tabPanel(...)),
        
      )
    )
  )
)

server <- function(input, output) {

  res <- reactiveValues(ults = NULL)

  observeEvent(input$runButton, {

 
  #Code with loops that generates a vector


  
    Vector <- c(input$n, input$mi, input$sigma, Vector)
    
    Vector <- sprintf("%.5f", Vector)
    
    data4 <- Vector[-13]
    
    data4 <- as.numeric(data4)

    names(data4) <- c("n", HTML("&mu;<sub>0</sub>"), HTML("&sigma;<sub>0</sub>"), HTML("W<sub>y</sub>"), HTML("CL<sub>y</sub>"), "LDI", "LDS", HTML("%S<sup>2</sup>"), "LCL", "UCL", HTML("&alpha;"), HTML("&lambda;"), HTML("&beta;"))

    data4 <- t(data4)

    res$ults <- data4

  }, once = TRUE)



  output$results <- renderTable({
    
    data.frame(req(res$ults))
    
  }, rownames = FALSE, sanitize.text.function = function(x) x)



   output$resultados <- renderTable({
    
    data.frame(req(res$ults))
    
  }, rownames = FALSE, sanitize.text.function = function(x) x)

  

   options(DT.options = list(pageLength = 5))
  
  
  output$x1 = render_dt(
    
    generate_normal_data(input$n, input$mi, input$sigma, req(res$ults[[6]]), req(res$ults[[7]])),'row'
    
    )
  
}

shinyApp(ui, server)

Explaining the problem:

The problem is that I tried to name the positions in the vector, but the HTML function simply doesn't work to call the symbols... it shows what I writed literally.

Before discovering that the vector was being generated as a character vector, the HTML function seemed to work... after I converted it to a numeric vector, if I put the code below before coverting the vector to numeric, the names simply disappear after I convert it. And if I put this code after converting it, it shows what I writed literally, without the symbols that I want.

names(data4) <- c("n", HTML("&mu;<sub>0</sub>"), HTML("&sigma;<sub>0</sub>"), HTML("W<sub>y</sub>"), HTML("CL<sub>y</sub>"), "LDI", "LDS", HTML("%S<sup>2</sup>"), "LCL", "UCL", HTML("&alpha;"), HTML("&lambda;"), HTML("&beta;"))

Solution

  • Just to update...

    I actually solved it creating a new reactive object and assigning the character vector to it before I convert the vector into numeric. After I converted it to a numeric vector, I assigned it to another reactive object.

    So now I have one character vector only for rendering and the numeric vector to use its values for other purposes.

    The conclusion seems to be: a character vector accepts HTML for naming its positions, but a numeric vector doesn't.