enter code here
我正在努力創建一個閃亮的反應儀表板,其中 y 軸將根據用戶通過“y = input$column1”的輸入。我能夠繪制條形圖,但我想查看每個條形圖頂部的值。樣本資料 | 季度| P_活動 | A_CP| B_CP 1 101 1600 2000 2 102 1230 2400 3 103 1500 2600
服務器腳本
function(input, output){
pData1 <- reactive({
filteredData <- subset(allData, P_Activity == input$P)
final <- data.table(filteredData)[,list(A_CP = sum(A_CP)),
by = list(quarter)]
return(final)
})
output$plot1 <- renderPlot({
plot <- ggplot(data = pData1(), aes_string(x= "quarter", y = input$column1))
plot <- plot geom_col(fill = "blue")
plot
})
}
用戶界面腳本
dashboardPage(
dashboardHeader(title = "SSSS"),
dashboardSidebar(
sidebarMenu(
menuItem(text ="Summary",tabName = "plots"),
selectInput(inputId = "P", label = "Select a P:", choices = unique(allData$P_Activity), selected = "101"),
selectInput(inputId = "column1", label = "Outcome", choices = c("A_CP"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "plots",
plotOutput("plot1",width =350, height =200)
)
)
)
)
資料框有 3 個屬性:Quarter、A_CP、B_CP,其中 A_CP 繪制為 input$column1 對季度。
我已經搜索了很多,但由于 y 是一個反應變數,因此找到解決方案似乎有點少見。任何幫助將非常感激。非常感謝。
uj5u.com熱心網友回復:
我假設您正在追求這樣的事情(我已經清理了您的代碼)。
library(shiny)
library(shinydashboard)
library(data.table)
# Sample data
allData <- data.frame(
quarter = 1:3,
P_Activity = as.character(c(101, 102, 103)),
A_CP = c(1600, 1230, 1500),
B_CP = c(2000, 2400, 2600))
# UI
ui <- dashboardPage(
dashboardHeader(title = "SSSS"),
dashboardSidebar(
sidebarMenu(
menuItem(text ="Summary", tabName = "plots"),
selectInput(
inputId = "P",
label = "Select a P:",
choices = unique(allData$P_Activity),
selected = "101"),
selectInput(
inputId = "column1",
label = "Outcome",
choices = c("A_CP")))
),
dashboardBody(
tabItems(
tabItem(
tabName = "plots",
plotOutput("plot1", width =350, height = 200)))
)
)
# Server
server <- function(input, output){
pData1 <- reactive({
filteredData <- subset(allData, P_Activity == input$P)
data.table(filteredData)[ , list(A_CP = sum(A_CP)), by = list(quarter)]
})
output$plot1 <- renderPlot({
ggplot(
data = pData1(),
aes_string(x = "quarter", y = input$column1, label = input$column1))
geom_col(fill = "blue")
geom_text(vjust = -0.5)
scale_y_continuous(expand = expansion(mult = c(0, 0.2)))
})
}
# Run
shinyApp(ui = ui, server = server)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/488013.html
