首页 > 解决方案 > 无法使用 Plotly 和 Shiny 显示误差线

问题描述

我正在尝试用 Shiny 和 plotly 在散点图上显示误差线。这是我的 server.R 文件中的代码:

data = reactiveVal()

observe({
    results <- data.frame() # actually getting the data from here

    # formatting output
    final.results <- cbind(
        "id" = paste(results$a,
                     results$b,
                     results$c,
                     sep = '-'),
        "sigma" = sprintf("%.5g", results$s),
        "c-e" = sprintf("%.3g",results$calc - results$exp)
    )
    
    data(final.results)
})

output$plot <- renderPlotly(
    as.data.frame(data()[,c("id", "c-e", "sigma")]) %>% plot_ly(
        x = ~`c-e`,
        y = ~id,
        height = 800,
        type = 'scatter', 
        mode = 'markers', 
        marker = list(color = "#90AFD9"),
        error_x = list(array = ~sigma, color = "#000000", type = "data")
    )
)

情节还可以,只是没有显示误差线,我的错误是什么?

编辑:澄清函数的起源及其data()返回值是什么。

标签: rshinyplotlyr-plotly

解决方案


sprintf()函数返回一个字符串,而不是一个数字,这就是它不将sigma值显示为错误栏的原因。如果要保留 5 位小数,请改用该round()函数:

"sigma" = round(results$s, digits = 5)

推荐阅读