簡體   English   中英

在r閃亮應用中未指定selectizeInput中的默認值時出現錯誤消息

[英]error message when no default value in selectizeInput specified in r shiny app

這是一個簡單的應用程序來說明我的問題。 當我使用selectizeInput而不指定默認值時,主面板只會顯示一條錯誤消息。 如果沒有選擇,我需要一個空圖。 如何修改我的代碼?

library(shiny)
library(datasets)


ui <- fluidPage(

    sidebarLayout(
        sidebarPanel(
            selectizeInput('var', 'choose variable', 
                           choices = names(mtcars),
                           options = list(
                               placeholder = 'Please select an option below',
                               onInitialize = I('function() { this.setValue(""); }'))
                           )

        ),
        mainPanel(
            plotOutput('hist')
        )
    )

)


server <- function(input, output) {

    output$hist <- renderPlot ({
        hist (eval(parse(text=(paste0('mtcars$', input$var)))))
    })
}

shinyApp(ui, server)

如果在selectizeInput未選擇任何內容,則輸入的值為"" ,因此您在字符串mtcars$上調用eval(parse()) ,這是錯誤的語法。 您不應使用eval(parse()) ,而應在嘗試繪制直方圖之前(例如在服務器中eval(parse())驗證輸入值:

output$hist <- renderPlot ({
  validate(need(input$var, 'Choose a variable!'))
  hist(x = mtcars[[input$var]])
})

如果您不想顯示消息,則可以在調用hist之前執行req(input$var)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM