Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 通过Plotly在Shiny应用程序中绘制缺少的数据图表_R_Shiny_Plotly - Fatal编程技术网

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

R 通过Plotly在Shiny应用程序中绘制缺少的数据图表,r,shiny,plotly,R,Shiny,Plotly,下午好 我试图从ggplot2的经济学数据集中绘制一个简单的时间序列。应用程序加载并显示具有正确轴的图表,但不包括任何绘图数据。任何帮助都将不胜感激。最好的,乔 library(shiny) library(plotly) library(tidyverse) df <- economics datalst = colnames(df) ui <- pageWithSidebar( headerPanel("test"), sidebarPanel( selec

下午好

我试图从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)
库(闪亮)
图书馆(绘本)
图书馆(tidyverse)

df这里的诀窍是避免非标准求值,因为
input$x
input$y
求值到字符串,而在您的示例中使用
plotly
则需要在~”之后使用裸列名。您可以使用以下方法解决此问题,例如:


server plot\u ly(x=dataset()[[input$x]],y=dataset()[[input$y]],type='scatter',mode='lines')@ibusett,效果很好!非常感谢。很高兴它起到了作用,我现在也把它作为一个答案发布了,因为我认为它可以让其他人受益。