R Shiny:散点图未显示
R Shiny: scatter plot not showing
我正在尝试根据动态滑块渲染 plot_ly 散点图。但是,似乎我错过了一些东西。
当我 运行 应用程序时,我看到的错误如下:
Error: invalid first argument
非常感谢您帮助我了解问题所在。
请在下面找到代码(希望它现在可以重现)。
#load libraries to be used. Pacman ensures library reproducibility
if (!require("pacman")) install.package("pacman")
pacman::p_load(ggplot2,dplyr,tidyr, rpart, shiny, shinydashboard, plotly, DT)
#load Titanic dataset
dataset<-read.csv(".../titanic.csv")
ui <- fluidPage(
dashboardHeader(
title = "My trial",
titleWidth = 300
),
dashboardSidebar(
sidebarMenu(
sidebarMenu(
menuItem("Visualization", tabName = "visualization", icon = icon("dashboard")),
menuItem("Data Table", tabName = "datatable", icon = icon("th"))
)
)
),
dashboardBody(
tabItems(
# Content of first tab
tabItem(tabName = "visualization",
h2("Visualization using Plot_ly"),
fluidRow(
box(plotlyOutput("Trialplot")),
box(
title = "Age range",
status="primary",
solidHeader=TRUE,
collapsible=TRUE,
sliderInput("slider", "Please select the age range to be seen:", 0, max(dataset$Age), c(0,max(dataset$Age)/2), ticks=FALSE)
)
),
box(plotlyOutput("Trialplot"))
),
# Content of second tab
tabItem(tabName = "datatable",
h2("Data table")
)
)
)
)
server = shinyServer(function(input, output,session) {
output$Trialplot<-renderPlotly({
dataset_filtered<-dataset[dataset$Age ==input$slider,]
plot_ly(data = dataset_filtered, x = ~Fare, y = ~get(input$slider) ,
color=~Survived,
colors=pal,
type='scatter',
marker=list(size=10),
hoverinfo='text',
text=~paste("Name:",Name,", Age:",Age,", Fare",Fare)
)%>%
layout(title="Age vs.Fare")})
}
)
shinyApp(ui,server)
提前致谢。
你不应该在你闪亮的应用程序中调用 plotlyOutput("Trialplot")
两次,另外,sliderInput
有两个值,即滑块的最小值和最大值,所以你不能使用这样的 dataset$Age == input$slider
:
我尝试重现您的代码,现在它对我有用,下次尝试 post 您的数据并输出 dput(data)
函数。
library(shiny)
pacman::p_load(ggplot2,dplyr,tidyr, rpart, shiny, shinydashboard, plotly, DT)
#load Titanic dataset
dataset<-read.csv("titanic.csv")
ui <- fluidPage(
dashboardHeader(
title = "My trial",
titleWidth = 300
),
dashboardSidebar(
sidebarMenu(
sidebarMenu(
menuItem("Visualization", tabName = "visualization", icon = icon("dashboard")),
menuItem("Data Table", tabName = "datatable", icon = icon("th"))
)
)
),
dashboardBody(
tabItems(
# Content of first tab
tabItem(tabName = "visualization",
h2("Visualization using Plot_ly"),
fluidRow(
# box(plotlyOutput("Trialplot")),
box(
title = "Age range",
status="primary",
solidHeader=TRUE,
collapsible=TRUE,
sliderInput("slider", "Please select the age range to be seen:", 0, max(dataset$Age,na.rm = T), c(0,max(dataset$Age,na.rm = T)/2), ticks=FALSE)
)
),
box(plotlyOutput("Trialplot"))
),
# Content of second tab
tabItem(tabName = "datatable",
h2("Data table")
)
)
)
)
server = shinyServer(function(input, output,session) {
observe({
print(input$slider)
})
output$Trialplot<-renderPlotly({
dataset_filtered<-dataset[dataset$Age >= input$slider[1] & dataset$Age <= input$slider[2],]
plot_ly(data = dataset_filtered, x = ~Fare, y = ~Age ,
color=~Survived,
colors=NULL,
type='scatter',
marker=list(size=10),
hoverinfo='text',
text=~paste("Name:",Name,", Age:",Age,", Fare",Fare)
)%>%
layout(title="Age vs.Fare")
})
}
)
shinyApp(ui,server)
我正在尝试根据动态滑块渲染 plot_ly 散点图。但是,似乎我错过了一些东西。
当我 运行 应用程序时,我看到的错误如下:
Error: invalid first argument
非常感谢您帮助我了解问题所在。
请在下面找到代码(希望它现在可以重现)。
#load libraries to be used. Pacman ensures library reproducibility
if (!require("pacman")) install.package("pacman")
pacman::p_load(ggplot2,dplyr,tidyr, rpart, shiny, shinydashboard, plotly, DT)
#load Titanic dataset
dataset<-read.csv(".../titanic.csv")
ui <- fluidPage(
dashboardHeader(
title = "My trial",
titleWidth = 300
),
dashboardSidebar(
sidebarMenu(
sidebarMenu(
menuItem("Visualization", tabName = "visualization", icon = icon("dashboard")),
menuItem("Data Table", tabName = "datatable", icon = icon("th"))
)
)
),
dashboardBody(
tabItems(
# Content of first tab
tabItem(tabName = "visualization",
h2("Visualization using Plot_ly"),
fluidRow(
box(plotlyOutput("Trialplot")),
box(
title = "Age range",
status="primary",
solidHeader=TRUE,
collapsible=TRUE,
sliderInput("slider", "Please select the age range to be seen:", 0, max(dataset$Age), c(0,max(dataset$Age)/2), ticks=FALSE)
)
),
box(plotlyOutput("Trialplot"))
),
# Content of second tab
tabItem(tabName = "datatable",
h2("Data table")
)
)
)
)
server = shinyServer(function(input, output,session) {
output$Trialplot<-renderPlotly({
dataset_filtered<-dataset[dataset$Age ==input$slider,]
plot_ly(data = dataset_filtered, x = ~Fare, y = ~get(input$slider) ,
color=~Survived,
colors=pal,
type='scatter',
marker=list(size=10),
hoverinfo='text',
text=~paste("Name:",Name,", Age:",Age,", Fare",Fare)
)%>%
layout(title="Age vs.Fare")})
}
)
shinyApp(ui,server)
提前致谢。
你不应该在你闪亮的应用程序中调用 plotlyOutput("Trialplot")
两次,另外,sliderInput
有两个值,即滑块的最小值和最大值,所以你不能使用这样的 dataset$Age == input$slider
:
我尝试重现您的代码,现在它对我有用,下次尝试 post 您的数据并输出 dput(data)
函数。
library(shiny)
pacman::p_load(ggplot2,dplyr,tidyr, rpart, shiny, shinydashboard, plotly, DT)
#load Titanic dataset
dataset<-read.csv("titanic.csv")
ui <- fluidPage(
dashboardHeader(
title = "My trial",
titleWidth = 300
),
dashboardSidebar(
sidebarMenu(
sidebarMenu(
menuItem("Visualization", tabName = "visualization", icon = icon("dashboard")),
menuItem("Data Table", tabName = "datatable", icon = icon("th"))
)
)
),
dashboardBody(
tabItems(
# Content of first tab
tabItem(tabName = "visualization",
h2("Visualization using Plot_ly"),
fluidRow(
# box(plotlyOutput("Trialplot")),
box(
title = "Age range",
status="primary",
solidHeader=TRUE,
collapsible=TRUE,
sliderInput("slider", "Please select the age range to be seen:", 0, max(dataset$Age,na.rm = T), c(0,max(dataset$Age,na.rm = T)/2), ticks=FALSE)
)
),
box(plotlyOutput("Trialplot"))
),
# Content of second tab
tabItem(tabName = "datatable",
h2("Data table")
)
)
)
)
server = shinyServer(function(input, output,session) {
observe({
print(input$slider)
})
output$Trialplot<-renderPlotly({
dataset_filtered<-dataset[dataset$Age >= input$slider[1] & dataset$Age <= input$slider[2],]
plot_ly(data = dataset_filtered, x = ~Fare, y = ~Age ,
color=~Survived,
colors=NULL,
type='scatter',
marker=list(size=10),
hoverinfo='text',
text=~paste("Name:",Name,", Age:",Age,", Fare",Fare)
)%>%
layout(title="Age vs.Fare")
})
}
)
shinyApp(ui,server)