代码之家  ›  专栏  ›  技术社区  ›  Rachel

R Shiny validate数据有效时返回错误

  •  0
  • Rachel  · 技术社区  · 6 年前

    我有一个闪亮的应用程序,在没有数据的情况下会产生错误,我正在尝试使用validate来生成更好的错误消息。我已经让它对没有数据的变量起作用,但是在有数据的地方,我得到了一个错误,应该出现一个图(或表)。

    使用以下代码,我得到以下错误。

    else if (input$geo=="Alaska, 2015") {
        data <- switch(
            validate(need(input$var != "Sexual Orientation", "Data Unavailable")),
            input$var, 
            "Housing Status" = hmlweightak,
            "Sex" = sexweightak,
            "Race/Ethnicity" = raceethweightak,
            "Sexual Orientation" = "",
            "Bullied at School in the last 12 Months" = bsweightak,
            "Missed School due to Safety Concerns" = usweightak,
            "Deliberately hurt by an intimate partner" = pvweightak,
            "Forced to Perform Sexual Acts by Intimate Partner in the last 12 Months" = saweightak,
            "Binge Drank in the last 30 days" = bdweightak,
            "First Tried Alcohol by Age 12" = faweightak,
            "First Tried Marijuana by Age 12" = fmweightak,
            "Suffered from Depression in the last 12 Months" = dweightak,
            "Had Suicidal Thoughts in the last 12 Months" = stweightak,
            "Attempted Suicide in the last 12 Months" = asweightak,
            "Required Medical Attention After Suicide Attempt in the last 12 Months" = smweightak,
            "Ever Used Illegal Drugs" = sdweightak,
            "Used Prescription Drugs Without a Prescription" = pdweightak,
            "Sexually Active by Age 13" = fiweightak,
            "Drank or Used Drugs Before Last Sexual Intercourse" = ddliweightak,
            "Breakfast in the Last 7 Days" = "",
            "Average Hours of Sleep per Night" = hsweightak,
            "Used a Condom During Last Sexual Intercourse" = clweightak,
            "Asthma" = aweightak
        )
    }
    

    enter image description here

    当我包括 get() 之前 input$var ,之前我得到了一个不同的错误,它基本上列出了所有的变量,但现在我似乎无法重现。这只是代码的一小部分,所以请让我知道更多的代码或截图是否会有帮助,但我希望这是一个简单的东西,因为它似乎只工作了一半。提前感谢!

    1 回复  |  直到 6 年前
        1
  •  0
  •   Maximilian Peters    6 年前

    问题在于 switch validate 陈述

    假设根据您的输入$dropDownValue,您希望返回一个或另一个数据集。如果 iris return datasets::iris ,则需要以下语句。

    switch(input$dropDownValue,
           iris = datasets::iris,
           cars = datasets::cars)
    

    验证 如果第一条语句的计算结果为,则停止函数的执行并添加验证消息 FALSE

    你的情况应该是

    validate(need(input$var != "Sexual Orientation", "Data Unavailable"))
    data <- switch(input$var, 
                   "Housing Status" = hmlweightak,
                   "Sex" = sexweightak,
                   [...]
                   )
    

    有关使用的示例,请参见下面的代码 验证 转换

    library(shiny)
    library(datasets)
    
    ui <- fluidPage(
       selectInput("dropDownValue", "Data", c("no data", "iris", "cars")),
        mainPanel(
          plotOutput("plot")
        )
    )
    
    server <- function(input, output) {
      data <- reactive({
        validate(
          need((input$dropDownValue == "iris" || input$dropDownValue == "cars"), "Please select a data set")
        )
        switch(input$dropDownValue,
               iris = datasets::iris,
               cars = datasets::cars)
      })
    
      output$plot <- renderPlot({
        hist(data()[, 1])
      })
    }
    shinyApp(ui, server)