缺少通过Plotly在Shiny应用程序中绘制的数据

下午好,

我正在尝试从ggplot2的经济学数据集中绘制一个简单的时间序列。该应用会加载,然后显示具有正确轴的图表,但不包含任何绘图数据。任何帮助将不胜感激。最好,乔

library(shiny)
library(plotly)
library(tidyverse)

df <- economics

datalst = colnames(df)

ui <- pageWithSidebar(
  headerPanel("test"),
  sidebarPanel(
    selectInput(inputId = "x",
                label = "Choose the x axis",
                datalst),
    selectInput(inputId = "y",
                label = "Choose the y axis",
                datalst, datalst[[2]])


  ),
  mainPanel(

    plotlyOutput("plot")

  )
)

server <- function(input, output) {

    dataset  <- reactive({
      df[, c(input$x, input$y)]
    })


    output$plot = renderPlotly({

      plot_ly(dataset(), x = ~input$x, y = ~input$y,type = 'scatter', mode = 'lines')

    })

}  

shinyApp(ui = ui, server = server)
勒布塞特

这里的技巧是避免非标准评估,因为input$xinput$y评估为字符串,而plotly在示例中使用as则需要〜后面的裸列名称。您可以使用以下方法解决此问题:

server <- function(input, output) {

    dataset  <- reactive({
      df[, c(input$x, input$y)]
    })
    output$plot = renderPlotly({
      plot_ly(x = dataset()[[input$x]], y = dataset()[[input$y]],type = 'scatter', mode = 'lines')
    })

}  

进一步的改进可以是将反应性“拆分”为两个,从而使绘图的输入无效,并且仅当相应的输入更改时才重新计算:

server <- function(input, output) {

    dataset_x  <- reactive({
      df[, input$x]
    })

    dataset_y  <- reactive({
      df[, input$y]
    })
    output$plot = renderPlotly({
      plot_ly(x = dataset_x()[[1]], y = dataset_y()[[1]], type = 'scatter', mode = 'lines')
    })

}  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Shiny应用程序中缺少值的updateSliderInput

在闪亮的应用程序中缺少反应性依赖,阻止数据通过输入更新

通过单击“ Shiny”应用程序中的链接来打开其他“ Shiny”应用程序

将用户上传的数据存储在Shiny应用程序中

在 ggplot 中绘制可变数量的系列以用于 Shiny 应用程序

iTunes Connect中缺少应用程序

Xubuntu中缺少应用程序菜单

Shiny R应用程序,允许用户通过套索选择来修改数据框

通过fileInput重置Shiny应用程序(当用户浏览新数据时)

Debian:通过flatpak安装的应用程序,从搜索栏运行,cmd行中缺少

通过使用闪亮的应用程序中的复选框来绘制箱线图

Plotly Dash:如何在多页应用程序中存储输入数据?

R Shiny 中的本地持久数据存储,供部署的应用程序中的多个用户使用

在Firebase中创建新的Web应用程序时,缺少数据库URL

我的shinyapps.io中的Shiny应用程序如何加载本地数据?

将响应式数据集和函数从父应用程序传递到Shiny中的模块

在为反应性数据框创建的R Shiny应用程序中删除标记

如何通过Android和iOS中的SMS消息启动应用程序,以及用于应用程序的加密数据

在虚拟应用程序(IIS)中运行应用程序时缺少程序集引用

R Shiny应用程序用于数据可视化

使用 Shiny 应用程序保存数据时出现的问题

缺少启动应用程序

Angular JS 应用程序无法通过 Wcf 服务从 Sql 数据库中检索数据

如何在plotly-dash应用程序内绘制图形

当我使用上传的数据在我的r Shiny应用程序中绘制ggplot2条形图时,为什么只得到一条实线?

使用Plotly图表向Shiny应用程序添加自定义标题

混合应用程序中的输入缺少焦点光标

Django 在“身份验证”应用程序中缺少迁移

在Yii 2应用程序中缺少Bootstrap 3标注