首页 > 解决方案 > 如何调用在服务器函数中创建的数据以在 UI 中显示值?R 闪亮

问题描述

我遇到了这样的困难,因为我在服务器函数中构建了一个数据集,用于绘制树形图,不仅在主面板中显示图形,而且在侧边栏面板中显示有关数据的信息。有人可以告诉我如何使这个反应式能够像我在下面的代码中尝试的那样使用,如果 UI 工作得很好?我之前尝试过在里面制作 dtd1 reactive,然后在情节中调用它,dtd1()但它仍然无法正常工作。

############################ GLOBAL #########################################

#1. App
if("shiny" %in% rownames(installed.packages()) == FALSE){ install.packages("shiny") }
library(shiny)

#2. Easier data handling
if("dplyr" %in% rownames(installed.packages()) == FALSE){ install.packages("dplyr") }
library(dplyr)

#3. Interactive graphs
if("plotly" %in% rownames(installed.packages()) == FALSE){ install.packages("plotly") }
library(plotly)


############################ UI #########################################
ui <- fluidPage( 
  # Set bullet size
  tags$style(type='text/css', "#select {font-size: 16px !important} "), 
  # 32px - h1() size || 24px - h2() size || 18.72px - h3() size || 16px - h4() size || 13.28px - h5() size
  navbarPage("Analysis", 
             tabPanel("Home",
                      sidebarPanel(

                        h5(tags$li( tags$head(tags$style("
                        #container * {   display: inline;  }")),
                        div(id="container", textOutput("patent_scape_tag1")))
                        )
             ),
             mainPanel(
               plotlyOutput("treemap")
               )
             )
  ))


############################ SERVER #########################################

server <- function(input, output, session) { 
  dtd1 <- NULL
  output$treemap <- renderPlotly({ 
    dtd1 <<- structure(list(V1 = structure(c(9L, 8L, 4L, 7L, 2L, 6L, 1L, 3L, 
                                            5L, 10L, 13L, 11L, 12L), .Label = c("Apple", "Avocado", "Banana", 
                                                                                "Carrot", "Mango", "Mushroom", "Onion", "Orange", "Pineapple", 
                                                                                "Strawberry", "Sweet-lemon", "Watermelon", "Wildberry"), class = "factor"), 
                           V2 = structure(c(4L, 3L, 9L, 11L, 12L, 2L, 1L, 6L, 10L, 5L, 
                                            7L, 8L, 1L), .Label = c("23", "24", "36", "42", "43", "46", 
                                                                    "48", "52", "56", "61", "82", "94"), class = "factor")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                 -13L))
    p <- plot_ly(
      dtd1,
      labels = ~ V1,
      parents = NA,
      values = ~ V2,
      type = 'treemap',
      hovertemplate = "Ingredient: %{label}<br>Count: %{value}<extra></extra>"
    )

    p
  })

  output$patent_scape_tag1 <- renderText({ 
    paste0("Topic ",
           as.character(dtd1$V1[which.max(dtd1$V2)]), 
           " reached the highest number!")
  })
} 

shinyApp(ui, server)

标签: rshinyshiny-reactivityshinyapps

解决方案


推荐阅读