简体   繁体   English

在 shiny 应用程序中找不到反应性 dataframe 的列名

[英]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.我在下面有 shiny 仪表板,我想在其中使用我的pickerInput()中的一个变量并创建一个 plot。 The issue is that my dataset is a reactive object and when I try to use table() I get object 'name' not found .问题是我的数据集是一个反应性 object ,当我尝试使用table()时,我得到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 .问题是您的反应性 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. 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.这意味着反应只会在其他一些代码需要它时运行,但是您的!!sym(input$DB)已经运行,我认为在找到需要的非标准评估 dataframe 和运行反应之间存在延迟。 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其次,由于您的pe是固定的 df,因此无需使用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)) 
        )

    })

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM