Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive CSS properties in R Shiny

I would like to selectively control font-scaling with a slidebar. Is this possible?

I've attached an MRE that I believe demonstrates where I'm stuck and what I'm trying to achieve.

if(!require('pacman')) {install.packages("pacman")} # Ensures that pacman is installed for auto-installation
pacman::p_load(shiny, lipsum) # automatically installs and loads packages.

ui <- fluidPage( # Sidebar with a slider input for font size
    sidebarLayout(sidebarPanel(
        sliderInput("font_size", "Font Size:", min = 1, max = 200, value = 70
        )
    ),

    mainPanel(
        wellPanel(
            id = "textPanel",
            style = "overflow-y:scroll; max-height: 50vh; font-size: 70%",
            #style = paste0("overflow-y:scroll; max-height: 50vh; font-size: ",
            # output$text_size,"70%"),
        paste0(lipsum[2:10], collapse = "")))))

# Define server logic 
server <- function(input, output) {
    output$text_size = renderText({input$font_size})}

# Run the application
shinyApp(ui = ui, server = server)
like image 960
RTS Avatar asked Sep 04 '17 23:09

RTS


1 Answers

You can use shinyjs to run some JavaScript code to change any CSS, in this case the font-size.

library(shiny)
library(shinyjs)

ui <- fluidPage( 
  shinyjs::useShinyjs(),
  sidebarLayout(
    sidebarPanel(
      sliderInput("font_size", "Font Size:", min = 1, max = 200, value = 70)
    ),
    mainPanel(
      wellPanel(
        id = "textPanel",
        style = "overflow-y:scroll; max-height: 50vh; font-size: 70%",
      paste0("Hello", collapse = ""))
    )
  )
)

server <- function(input, output) {
  observeEvent(input$font_size, {
    runjs(paste0('$("#textPanel").css("font-size","', input$font_size, '%")'))
  })
}

shinyApp(ui = ui, server = server)
like image 168
Geovany Avatar answered Sep 21 '22 14:09

Geovany