Ich habe das folgende vereinfachte Beispiel für eine Shiny App mit plotly Grafik.R Shiny - Fehler "keine anwendbare Methode für" ggplotly "angewendet auf ein Objekt der Klasse" c ('double', 'numeric') "
# 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)
Das Hauptmerkmal, das ich will, ist die Struktur des Diagramms zu ändern. Daher füge ich diese Codezeile in renderPlotly
nach der Konvertierung in einer plotly
Grafik hinzu.
ggplotly(Plot1)
Plot1$x$layout$margin$l <- 180
Oder, wenn ich diese Zeile hinzufügen, ich habe einen Fehler „keine anwendbare Methode für‚ggplotly‘auf ein Objekt der Klasse angewendet„c (‚double‘, ‚numerisch‘)“und trotz der Bemühungen kann ich nicht debuggen . Irgendeine Idee ? Ich
präzise, dass es funktioniert in R-Befehlszeile fein: Handle long labels in plotly
bitte Code schreiben, der den Fehler erzeugt, funktioniert dieser Code gut für mich – HubertL
Der Code funktioniert nicht, wenn Sie 'Plot1 $ layout x $ $ l Marge $
Kumpelka
Das liegt daran, dass renderPlotly ein Objekt benötigt, das von ggplotly erzeugt wird, was das Ergebnis des Codes ist (die letzte Auswertung des Ausdrucks ist der Rückgabewert dieses Ausdrucks), während, wenn Sie diese Anweisung nachher hinzufügen, das Ergebnis des gesamten Ausdrucks ist 180, die nicht als Plotplot gerendert werden kann, weil es eine Zahl ist. Fügen Sie es vor dem Aufruf an ggplotly – HubertL