gpt4 book ai didi

由于某种原因,R Shiny 的仪表板未显示图形。不会让我在一个 ggplot 渲染中添加 2 个 react 表达式

转载 作者:行者123 更新时间:2023-12-02 02:44:06 25 4
gpt4 key购买 nike

虽然我的图表不起作用,但我正在尝试获得一个 Shiny 的仪表板。

我有一个 data.frame,它包含一个日期格式的 Date 列和 4 个数字列类型,它们是 ISA、NonISA、FixedRateInterest 和 VariableRateInterest,并填充了 N/A 值为 0。

我有一个带有日期范围 slider 的复选框,我可以让图表呈现,但是当我尝试添加 DateRangeslider 时,它不起作用并抛出以下错误:

Unknown input:data.frame

这是我创建仪表板的代码。谁能帮我理解为什么它不起作用?我知道它与 ggplot 线有关,但不确定是什么。

谢谢

isa <- read.csv("isa.csv")
isa$Date <- paste0(isa$Date, "01")
isa$Date <- as.character(isa$Date)
isa$Date <- as.Date(isa$Date, format = "%Y%m%d")
date <- isa$Date


# Define UI ----
ui <- fluidPage(
titlePanel("Customer"),
sidebarLayout(
sidebarPanel(
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
img(src = "Logo.jpg", height = 100, width = 150)),
fluidRow(
h1("Choose Dataset"),
br(),
br(),
column(1,
checkboxGroupInput(inputId = "product",
h3("Product"),
choices = c('ISA',
"Non-ISA",
"Fixed",
"Variable"),
selected = 1)),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),
br(),

column(2,
dateRangeInput(inputId = "dates", h3("Date Range"),
start = min(isa$Date),
end = max(isa$Date),
min = min(isa$Date),
max = max(isa$Date),
separator = "-", format = "yyyy/mm/dd")

))),
br(),
br(),
br(),



mainPanel(
plotOutput("barplot")
)
)
)
# Define server logic ----
server <- function(input, output) {




dataInput <- reactive({

switch(input$product,
"ISA" = ISA,
"Non-ISA" = isa$NonISA,
"Fixed" = isa$FixedRateInterest,
"Variable" = isa$VariableRateInterest)

就是在这里好像失败了

      })
dateInputx <- reactive({
isa[isa$Date >= input$dates[1] & isa$Date <= input$dates[2],]
})


output$barplot <- renderPlot({

并且无法识别 ggplot 上的 x 轴

ggplot(data = dataInput(), aes_string(x= dateInputx(), y = dataInput())) +
geom_line(stat ="identity")
})

}


# Run the app ----
shinyApp(ui = ui, server = server)

最佳答案

ggplot 中的data 参数应该是数据框而不是列。然后,将 aes_string() 与相应的列名一起使用。我试图在下面制作一个可重现的例子:

library(shiny)
library(dplyr)
library(ggplot2)

ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput('ycol','Y axis',choices = c('City Mileage','Highway Mileage')) #
),
mainPanel(
plotOutput('plot')
)
)
)

server <- function(input,output){
data(mpg) # Replace mpg with your dataframe of choice(isa in your case)

dataInput <- reactive({ # Required y column name as in dataframe
switch(input$ycol,
"City Mileage" = 'cty',
"Highway Mileage" = 'hwy')

})


output$plot <- renderPlot({
# Use aes_string for column names stored in strings
p <- ggplot(data=mpg,aes_string(x='displ',y=dataInput()))+
geom_line()

p
})
}

shinyApp(ui,server)

关于由于某种原因,R Shiny 的仪表板未显示图形。不会让我在一个 ggplot 渲染中添加 2 个 react 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56891440/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com