Cannot find column name of a reactive dataframe in a shiny app

天涯浪子 提交于 2020-06-17 12:56:31

问题


I have the shiny dashboard below in which I want to use a variable from my pickerInput() and create a plot. The issue is that my dataset is a reactive object and when I try to use table() I get object 'name' not found. If it would not be reactive it would work but it has to be in my real app.

library(shiny)
library(shinydashboard)
library(shinyWidgets)
library(ggplot2)
library(plotly)
ui <- dashboardPage(
    header = dashboardHeader(title = "My dashboard"),
    sidebar = dashboardSidebar(
        uiOutput("dbs")

    ),
    body = dashboardBody(
        plotlyOutput("fn")
    )
)

server <- function(input, output, session) {
    pe<-reactive({
        sts<-c("Rev","Rev")
        sID<-c("123","124")
        snID<-c("23","34")
        name<-c("s","d")
        data.frame(sts,sID,snID,name)
    })


    output$dbs<-renderUI({

            pickerInput("DB", "Select Database/s", 
                        choices = c("name","snID"), 
                        multiple = F,options = list(`actions-box` = TRUE),
                        selected = "name")

    })
    output$fn<-renderPlotly({

            #2.2 MAKING A TABLE for public.exists
        tbl<-table(pe()[[input$DB]], pe()$sts)
            ggplotly(
                ggplot(as.data.frame(tbl), aes(!!sym(input$DB), Freq, fill = sts)) 
            )

    })

}

shinyApp(ui, server)

回答1:


The problem is your reactive df pe. In shiny logic, when the app runs renderPlotly your non-standard evaluation of !!sym(input$DB) is evaluated and it tries to get the object name, and then it searches for the dataframe, because ractive uses lazy loading in shiny. That means the reactive will only run when some other code requires it, but your !!sym(input$DB) has already run and I think there is a delay between finding non-standard evaluation required dataframe and run the reactive. So error happens.

You have two solutions, first, change your !! to string evaluation:

        ggplotly(
            ggplot(as.data.frame(tbl), aes_(input$DB, 'Freq', fill = 'sts')) 
        )

Second, since your pe is a fixed df, no need to use reactive

server <- function(input, output, session) {
    pe<-{
        sts<-c("Rev","Rev")
        sID<-c("123","124")
        snID<-c("23","34")
        name<-c("s","d")
        data.frame(sts,sID,snID,name)
    }


    output$dbs<-renderUI({

        pickerInput("DB", "Select Database/s", 
                    choices = c("name","snID"), 
                    multiple = F,options = list(`actions-box` = TRUE),
                    selected = "name")

    })
    output$fn<-renderPlotly({
        #2.2 MAKING A TABLE for public.exists
        tbl<-table(pe[[input$DB]], pe$sts)

        ggplotly(
            ggplot(as.data.frame(tbl), aes(!!sym(input$DB), Freq, fill = sts)) 
        )

    })

}


来源:https://stackoverflow.com/questions/62071152/cannot-find-column-name-of-a-reactive-dataframe-in-a-shiny-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!