R Shiny 반응 시스템을로드 한 후 'navbarPage'및 'tabPanel'구성 요소의 요소를 변경하는 방법은 무엇입니까?

Andrii

R Shiny 반응 시스템을로드 한 후 'navbarPage'및 'tabPanel'구성 요소를 변경하는 방법을 연구하고 있습니다. 다음은 코드입니다.

library(shiny)

# How to change these elements after loading R Shiny reactive system
str_title <- "Title"
str_window_title <- "Window title"
str_cars <- "Cars"
str_iris <- "Iris"

# UI
ui <- fluidPage(
  navbarPage(
    title = str_title, 
    windowTitle = str_window_title,
    tabPanel(title = str_cars, fluidPage(fluidRow(dataTableOutput("dt_mtcars")))),
    tabPanel(title = str_iris, fluidPage(fluidRow(dataTableOutput("dt_iris"))))
  ))

# SERVER
server <- function(input, output) {
  output$dt_mtcars <- renderDataTable(datatable(mtcars))
  output$dt_iris <- renderDataTable(datatable(iris))
}

# RUN APP
shinyApp(ui = ui, server = server)

문제는 Shiny 앱을로드 한 후 'navbarPage'구성 요소의 'title', 'window_title'및 'tabPanel'구성 요소의 'title'값을 변경하는 방법입니다. 예를 들어, 이러한 이름에 접두사 'New'를 추가하고 값은 'New Title', 'New Window title', 'New Cars', 'New Iris'입니다.

아이디어를 공유해 주셔서 감사합니다!

gdevaux

에 대한 해결책을 찾을 수 windowTitle없었지만 3 개의 다른 요소에 대해 a textOutput및 반응 값을 사용하여 요소를 변경할 수 있습니다. 다음은 작업 버튼을 클릭 한 후 요소 이름을 변경하는 예입니다.

편집 : 이 답변을 기반으로 windowTitle도 변경하는 방법을 찾았습니다.

library(shiny)
library(DT)

# UI
ui <- fluidPage(
  actionButton("btn", "Change components' names"),
  #javascript code to change window title
  tags$script(HTML('Shiny.addCustomMessageHandler("changetitle", function(x) {document.title=x});')),
  navbarPage(
    title = textOutput("str_title"), 
    windowTitle = "Window title",
    tabPanel(title = textOutput("str_cars"), fluidPage(fluidRow(dataTableOutput("dt_mtcars")))),
    tabPanel(title = textOutput("str_iris"), fluidPage(fluidRow(dataTableOutput("dt_iris"))))
  ))

# SERVER
server <- function(input, output, session) {
  # initialize names
  rv <- reactiveValues(str_title = "Title",
                       str_window_title = "Window title",
                       str_cars ="Cars",
                       str_iris = "Iris")
  
  output$dt_mtcars <- renderDataTable(datatable(mtcars))
  output$dt_iris <- renderDataTable(datatable(iris))
  
  output$str_title <- renderText({
    rv$str_title
  })
  output$str_window_title <- renderText({
    rv$str_window_title
  })
  output$str_cars <- renderText({
    rv$str_cars
  })
  output$str_iris <- renderText({
    rv$str_iris
  })
  
  #change names when button is clicked
  observeEvent(input$btn,{
    print("Change names")
    rv$str_title <- paste0(rv$str_title,"+")
    rv$str_window_title <- paste0(rv$str_window_title,"+")
    rv$str_cars <- paste0(rv$str_cars,"+")
    rv$str_iris <- paste0(rv$str_iris,"+")
    
    session$sendCustomMessage("changetitle", rv$str_window_title )
  })
  

}

# RUN APP
shinyApp(ui = ui, server = server)

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관