闪亮的应用程序错误无法将“环境”类型强制转换为“字符”类型的向量

问题描述

我正在尝试运行这个闪亮的应用程序,它接收一些数据,然后处理它以在名为“out”的变量中给出一些比例表。然后使用使用 purrr 包映射的函数绘制这些表。尽管在我尝试定义函数的部分中,该应用程序给了我以下错误:-

cannot coerce type 'environment' to vector of type 'character'
library(dplyr)
library(tidyr)
library(purrr)
library(ggplot2)

ui<-shinyUI(fluidPage(
  titlePanel(title = h4("proportion graphs",align="center")),sidebarLayout( sidebarPanel( ),mainPanel(
                                                                                # create a uIoUtput
                                                                                uIoUtput("plots")
                                                                              )
  )
  
))


server<- shinyServer(
  
  function(input,output) {
    #1 Dataset l
    l<- reactive({
      f<- list(`0` = structure(list(X70 = "D",X71 = "C",X72 = "C",X73 = "A",X74 = "B",X75 = "C",X76 = "D",X77 = NA_character_,X78 = "B",X79 = "D",X80 = "C",Q = 1),row.names = 32L,class = "data.frame"),`1` = structure(list(X70 = c("D","B","D","D"),X71 = c("B","C",NA,"A","C"),X72 = c("A",NA),X73 = c("B",X74 = c("B",X75 = c("C",X76 = c("D","B"),X77 = c("D",X78 = c("B",X79 = c("C",X80 = c("B","A"),Q = c(2,2,1,4,3,1)),row.names = c(8L,10L,12L,17L,25L,27L,28L,33L,35L,38L,45L),`2` = structure(list(X70 = c("D",X71 = c("A",X72 = c("D",X74 = c("D",X75 = c("B",X76 = c("A",X77 = c("B",X78 = c("C",X79 = c("A",X80 = c(NA,row.names = c(4L,5L,6L,11L,15L,16L,21L,22L,26L,37L,39L,43L),`3` = structure(list(X70 = c("A",X72 = c("B",X73 = c(NA,X74 = c(NA,X75 = c(NA,X76 = c(NA,X77 = c(NA,X78 = c(NA,X79 = c(NA,2)),row.names = c(2L,13L,14L,18L,19L,20L,29L,30L,34L,36L,41L,44L),`4` = structure(list(X70 = c("D",X74 = c("C",X79 = c("D",X80 = c("C",4)),row.names = c(1L,3L,7L,9L,23L,24L,31L,40L,42L,46L,47L,48L),class = "data.frame")) })
    
    
    #2 Vector u
    u <- reactive({
      u <- c("D","A")
    })
    
    #3 reactive expression to process data
    out <- reactive({
      l <- l()
      u <- u()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
      
      lapply(l,function(dat) 
        asplit(as.data.frame(t(sapply(dat,function(x) 
          proportions(table(factor(unlist(x),levels = u)))))),1) ) %>%
        transpose %>%
        map(bind_rows,.id = 'grp')
    })
    
    #4 render plots 
    output$plots <- renderUI({
      
      plots <- function(x){
        d = x %>% 
          as.data.frame() %>%
          tidyr::pivot_longer(!grp) %>%
          dplyr::group_by(name) %>% 
          dplyr::mutate(n = 1:n())
        
        ggplot(data = d) + 
          geom_path(aes(x = grp,y = value,group = factor(name),color = factor(name)),size = 0.7) +
          geom_point(aes(x = grp,size = 2) +
          geom_text(data = d %>% filter(n == max(n)),aes(x = grp,label = name,nudge_x = 0.2) + 
          labs(x = "Group",y = "P",title = "") + 
          theme_bw() +
          theme(legend.position = "none")
      }
      plot_objects <- purrr::map(out(),plots)
      
      
    })
    
  } )

shinyApp(ui,server)

解决方法

renderUI 函数需要在 renderPlot 图上调用 ggplot。这是 output$plots 的附加内容。

output$plots <-
    renderUI({
        plots <- function(x) {
            d = x %>%
                as.data.frame() %>%
                tidyr::pivot_longer(!grp) %>%
                dplyr::group_by(name) %>%
                dplyr::mutate(n = 1:n())
            
            gg <- ggplot(data = d) + # put ggplot into a variable
                geom_path(aes(
                    x = grp,y = value,group = factor(name),color = factor(name)
                ),size = 0.7) +
                geom_point(aes(
                    x = grp,size = 2) +
                geom_text(
                    data = d %>% filter(n == max(n)),aes(
                        x = grp,label = name,color = factor(name)
                    ),nudge_x = 0.2
                ) +
                labs(x = "Group",y = "P",title = "") +
                theme_bw() +
                theme(legend.position = "none")
            renderPlot(gg)  # call renderPlot on the ggplot variable
        }