簡體   English   中英

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

[英]Cannot find column name of a reactive dataframe in a shiny app

我在下面有 shiny 儀表板,我想在其中使用我的pickerInput()中的一個變量並創建一個 plot。 問題是我的數據集是一個反應性 object ,當我嘗試使用table()時,我得到object 'name' not found 如果它不是反應式的,它會起作用,但它必須在我的真實應用程序中。

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)

問題是您的反應性 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. 這意味着反應只會在其他一些代碼需要它時運行,但是您的!!sym(input$DB)已經運行,我認為在找到需要的非標准評估 dataframe 和運行反應之間存在延遲。 所以會發生錯誤。

你有兩個解決方案,首先,改變你的!! 字符串評估:

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

其次,由於您的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