简体   繁体   中英

Shiny Send plain string from server to ui

i'm facing an issue that probably is easy too solve but is bugging me.

I have a global.R script where I use JS to compute the screen height

jscode_for_screen_Height <- '$(document).on("shiny:connected", function(e) {
                             var jsHeight = screen.height; 
                             Shiny.onInputChange("GetScreenHeight",jsHeight); });'

This variable is used in several parts in my server.R and it works properly. Eg

output$viewDataCTgov <- DT::renderDataTable({
DT::datatable(data_to_render_CTgov(),
options = list(scrollX = TRUE,scrollY = paste(input$GetScreenHeight,"px",sep=""), 
scrollCollapse=TRUE,pageLength =  100,searchHighlight = TRUE), escape = FALSE)
})

Now I'm creating a leaflet map and in ui.R I have

 leafletOutput("mymap", height = XXX )

leafletoutput accepts a string as height parameter (eg "400")

Here the problems:

1) I can't use input$GetScreenHeight because i'm on the ui.R and input is not in the scope of the script.

2) I can't figure out how to pass a plain string to ui.R from the server.R

What I tried so far is to use in server.R

output$ScreenHeightvalue <- paste0(input$GetScreenHeight)

And in ui.R

leafletOutput("mymap", height =(textOutput("ScreenHeightvalue")))

but doesn't work due to the fact that textOutput("ScreenHeightvalue") is not a plain string.

How can I pass paste0(input$GetScreenHeight) from the server.R to the ui.R as plain string?

I don't know how to pass a plain string from server to ui, but you can define your lealfetOutput in the server by using a renderUI

library(shiny)
library(leaflet)

ui <- fluidPage(
  uiOutput("test")
)

server <- function(input, output) {
  output$myMap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      setView(-93.65, 42.0285, zoom = 17)
  })

  output$test <- renderUI({
    tagList(
      leafletOutput("myMap", height = paste0(input$GetScreenHeight))
    )
  })
}
shinyApp(ui, server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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