我正在使用 Shiny 构建一个简单的仪表板以用于我的工作。一切都很好,直到我发现我无法传递选择的输入以用作 ggplotgeom_col()
交互参数中的参数。
我的目的是根据所选值更改交互图,checkboxGroup
其中所选值将用作 ggplot 中 fill=interaction(....) 的参数。
我在这个阶段遇到了问题:
ggplot(data= opsdata2, aes(x=reorder(GUDANG,KUANTUM/1000), y=KUANTUM/1000,
fill=interaction(get(input$cekgr_fill),sep = "*")
))
'get(input$cekgr_fill)' 仅传递第一个参数,而我的目的是使用 input$cekgr_fill 中的至少 2 个参数构建交互条形图,例如:'fill=interaction(JENIS,TH_ADA, sep="*") '。
'get(input$cekgr_fill)' 只传递第一个参数,即:JENIS,并忽略 TH_ADA。
你能帮帮我吗?谢谢你。
这是我的代码:
shinyUI(dashboardPage(
#Nama Dashboard
dashboardHeader(title = "OPERASIONAL"),
dashboardSidebar(
checkboxGroupInput("cekgr_gudang", label = h4("Gudang"),
choiceNames = list("Karanganyar","Binong",
"Rancaudik","Tanjungrasa",
"Ciwangi"),
choiceValues = list("Karanganyar","Binong",
"Rancaudik","Tanjungrasa",
"Ciwangi")
),
checkboxGroupInput("cekgr_komoditas", label = h4("Komoditas"),
choices = c("Beras","Minyak Goreng",
"Terigu","Gula","Gabah", "Ketan")
),
checkboxGroupInput("cekgr_tahun","Tahun",
choices = c("2018","2019","2020","2021")
),
checkboxGroupInput("cekgr_opsional", label = h4("Opsional"),
choices = c("Fumigasi", "Kondisi kualitas")
),
checkboxGroupInput("cekgr_fill", label = h4("Fill Grafik"),
choiceNames = c("JENIS","TAHUN"),
choiceValues = c("JENIS", "TH_ADA")
),
actionButton("OK","Sikaaat")
),
## BODY
dashboardBody(
fluidRow(
column(
width = 12,
box(title = "Grafik Yang Kamu Minta ",
solidHeader = T,
width = 8, height = 500,
collapsible = T,
plotOutput("grafik1"),
textOutput("PilihanGudang"),
textOutput("PilihanKomoditas"),
textOutput("PilihanTahun"),
textOutput("FillGrafik"))
)
))))
library(shiny)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
#### Pilihan-pilihan
observeEvent(input$OK,{
opsdata1 <- read_xlsx("~/Documents/App-Dashboard-Ops/data_Feb_11.xlsx")
View(opsdata1)
output$PilihanGudang <- renderText({
gudangterpilih <- paste(input$cekgr_gudang,collapse = ", ")
paste("Gudang : ", gudangterpilih)})
output$PilihanKomoditas <- renderText({
komoditasterpilih <- paste(input$cekgr_komoditas, collapse = ", ")
paste("Komoditas : ", komoditasterpilih)})
output$PilihanTahun <- renderText({
tahunterpilih <- paste(input$cekgr_tahun, collapse = ", ")
paste("Tahun : ", tahunterpilih)})
output$FillGrafik <- renderText({
fillterpilih <- paste(input$cekgr_fill, collapse = ", ")
paste("Fill : ", fillterpilih)})
opsdata2 <- opsdata1 %>%
filter(GUDANG %in% input$cekgr_gudang) %>%
filter(JENIS %in% input$cekgr_komoditas) %>%
filter(TH_ADA %in% input$cekgr_tahun)
View(opsdata2)
output$grafik1 <- renderPlot({
ggplot(data= opsdata2, aes(x=reorder(GUDANG,KUANTUM/1000), y=KUANTUM/1000,
fill=interaction(get(input$cekgr_fill),sep = "*")
)) +
geom_col() + coord_flip() +
scale_y_continuous(labels = unit_format(unit = "Ton")) +
labs(x="",y="",fill="") +
theme_clean() + theme(legend.position = "top")
})
})
})
here is my data :
structure(list(GUDANG = c("Karanganyar", "Karanganyar", "Karanganyar", "Karanganyar", "Rancaudik", "Rancaudik", "Rancaudik", "Tanjungrasa", "Tanjungrasa", "Tanjungrasa", "Tanjungrasa", "Binong", "Binong", "Binong", "Binong", "Binong", "Binong", "Binong", "Ciwangi", "Ciwangi", "Ciwangi"), UNIT = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), TUMPUKAN = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), JENIS = c("Beras", "Beras", "Beras", "Beras", "Beras", "Beras", "Beras", "Beras", "Beras", "Beras", "Ketan", "Beras", "Beras", "Beras", "Beras", "Beras", "Beras", "Ketan", "Beras", "Beras", "Beras"), PSO_KOM_HGL = c("PSO", "PSO", "KOM", "KOM", "PSO", "PSO", "PSO", "PSO", "PSO", "KOM", "KOM", "PSO", "PSO", "PSO", "HGL", "KOM", "PSO", "KOM", "PSO", "PSO", "PSO" ), TH_ADA = c(2020, 2019, 2020, 2020, 2020, 2019, 2018, 2020, 2019, 2020, 2020, 2020, 2019, 2020, 2020, 2018, 2018, 2020, 2019, 2018, 2018), KUALITAS = c("Med_20%", "Med_20%", "Kom_10%", "Kom_10%", "Med_20%", "Med_20%", "Med_5%", "Med_20%", "Med_20%", "Kom_10%", "Kom_15%", "Med_20%", "Med_20%", "Kom_15%", "Kom_15%", "Kom_15%", "Med_5%", "Kom_15%", "Med_20%", "Med_5%", "Med_15%"), KEMASAN = c(50, 50, 10, 25, 50, 50, 50, 50, 50, 10, 25, 50, 50, 10, 50, 5, 50, 25, 50, 50, 50), MEREK = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "WJ", NA, NA, NA, NA, "IBU", NA, "WJ", NA, NA, NA), NEGARA = c("Indonesia", "Indonesia", "Indonesia", "Indonesia", "Indonesia", "Indonesia", "Thailand", "Indonesia", "Indonesia", "Indonesia", "Indonesia", "Indonesia", "Indonesia", "Indonesia", "Indonesia", "Indonesia", "Thailand", "Indonesia", "Indonesia", "Vietnam", "Vietnam"), EXP = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), KONDISI = c("Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik", "Baik"), KUANTUM = c(10000, 107500, 12810, 4150, 65000, 4391000, 222850, 320000, 3193550, 2580, 37500, 30000, 2513060, 184720, 2040, 182200, 177270, 20000, 529400, 103500, 449755)), row.names = c(NA, -21L), class = c("tbl_df", "tbl", "data.frame"))