R-错误“;没有适用于'的方法;ggplotly';应用于”类的对象;c(';双精度';,';数字';)”;

R-错误“;没有适用于'的方法;ggplotly';应用于”类的对象;c(';双精度';,';数字';)”;,r,plot,shiny,plotly,R,Plot,Shiny,Plotly,我有一个使用plotly graphic的闪亮应用程序的简化示例 # Function, library, data PlotResponseRate <- function(EntryData) { PlotData <- as.data.frame(apply(X = EntryData, MARGIN = 2, function(x) round(length(which(!is.na(x)))/lengt

我有一个使用plotly graphic的闪亮应用程序的简化示例

# Function, library, data
PlotResponseRate <- function(EntryData)
{
  PlotData <- as.data.frame(apply(X = EntryData, MARGIN = 2,
                                  function(x) round(length(which(!is.na(x)))/length(x)*100)))
  colnames(PlotData) <- "TheData"
  PlotData$TheLabel <- factor(str_wrap(colnames(EntryData), width = 30),
                              levels = unique(str_wrap(colnames(EntryData), width = 30)))
  PlotData$TheLabel <- gsub(pattern = "\n", replacement = "<br>", PlotData$TheLabel)


  Graphe <- ggplot(data = PlotData, aes(x = TheLabel, y = TheData)) +
    geom_bar(stat = "identity", fill = "red", width = 0.8) +
    coord_flip() +
    labs(title = "Response rate")
}

library(stringr)
library(ggplot2)
library(plotly)

a <- c(1, 2, 2, 2, NA, 1, 2, 2, 1)
b <- c(2, 1, 2, NA, 2, NA, 1, NA, 1)
df <- data.frame(a, b)

colnames(df) <- c("This Is A Long Answer To A Long Question Label For The First Question",
                  "This Is A Long Answer To A Long Question Label For The Second Question")

# The Shiny app 
Interface <- 
{
  fluidPage(
    sliderInput(inputId = "Num",
                label = "Choose the questions",
                value = c(1:2), min = 1, max = 2, step = 1),
    plotlyOutput("Myplot")
    )
}

Serveur <- function(input, output)
{
  output$Myplot <- renderPlotly({
    Plot1 <- PlotResponseRate(EntryData = df[c(input$Num[1]:input$Num[2])])
    ggplotly(Plot1)
  })
}

shinyApp(ui = Interface, server = Serveur)
或者,当我添加这一行时,我有一个错误“没有适用于'ggplotly'的方法应用于类“c('double','numeric')”的对象”,尽管我做了很多努力,但还是无法调试。有什么想法吗

我确信它在R命令行中运行良好:

根据上面的注释,正确的代码如下所示

Serveur <- function(input, output)
{
  output$Myplot <- renderPlotly({
    Plot1 <- PlotResponseRate(EntryData = df[c(input$Num[1]:input$Num[2])])
    Plot1 <- plotly_build(Plot1)
    Plot1$x$layout$margin$l <- 180
    Plot1 <- ggplotly(Plot1)
  })
}

Serveur根据上面的注释,正确的代码如下

Serveur <- function(input, output)
{
  output$Myplot <- renderPlotly({
    Plot1 <- PlotResponseRate(EntryData = df[c(input$Num[1]:input$Num[2])])
    Plot1 <- plotly_build(Plot1)
    Plot1$x$layout$margin$l <- 180
    Plot1 <- ggplotly(Plot1)
  })
}

Serveur请发布产生错误的代码,此代码适用于meth添加
Plot1$x$layout$margin$l时代码不起作用,这是因为renderPlotly需要一个由ggplotly生成的对象,该对象是代码的结果(表达式的最后一次计算是该表达式的返回值),而如果在后面添加该指令,则整个表达式的结果为180,因为它是一个数字,所以不能呈现为绘图。在调用GGPLOTLY之前添加它我认为您误解了这里的解决方案:<使用code>plotly\u build()
代替
ggplolty()
,然后编辑plotly对象的列表。然后在renderPlotly中调用plotly对象。请发布产生错误的代码,该代码适用于Meth。添加
Plot1$x$layout$margin$l时,该代码不起作用,因为renderPlotly需要由ggplotly生成的对象,这就是代码的结果(表达式的最后一次求值是该表达式的返回值),而如果在后面添加该指令,则整个表达式的结果是180,因为它是一个数字,无法呈现为绘图。请在调用GGPLOTLY之前添加它。我认为您误解了这里的解决方案:。
plotly\u build()
用于代替
ggplolty()
,然后编辑plotly对象的列表。然后在renderPlotly中调用plotly对象。