简体   繁体   中英

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

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)

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)) 
        )

    })

}

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