R:Shiny - 无功输出在 grid.arrange(from gridExtra) 中不起作用

标签 r shiny gridextra

我正在使用 gridExtra 结合 Ggplot 的条形图制作摘要图。

问题是我想让摘要对 radioButtons 输入有反应。

我想要实现的目标:

enter image description here

我有什么:

enter image description here

我确定是因为使用了我在服务器中使用的“input$radio”部分。R:

shiny::runApp()

Listening on http://127.0.0.1:7548
Warning in grob$wrapvp <- vp : Coercing LHS to a list
Error in gList(structure(list("tbl.precio.nuevo", wrapvp = structure(list( : 
  only 'grobs' allowed in "gList"

有什么办法解决这个问题吗?

我的代码:

用户界面:

library(shiny)


shinyUI(fluidPage(
        includeCSS("D:\\RCoursera\\r-s-l\\www\\rsl.css"),
        titlePanel("R - Tvs"),
        sidebarLayout(
                sidebarPanel(
                        p("This dashboards muestra los tvs en la sección"),
                        p("La data se obtuvo de esta sección: \n",
                          a("R TV todas", 
                            href = "http://www")),
                        img(src="", height = 60, width = 120),
                        br(),
                        br(),
                        p("Se consideraron los siguientes atributos: marca, producto y precio")



                ),
                mainPanel(
                        tabsetPanel(
                                tabPanel("Por marca", radioButtons("radio", label = h6("Precio actual vs precio antes"),
                                                                                        choices = list("Precio actual" = "tbl.precio.nuevo", "Precio antes" = "tbl.precio.antes"),
                                                                                        selected = "tbl.precio.nuevo", inline = T), plotOutput("plot1")),
                                tabPanel("Por rango de precio", plotOutput("plot2"),
                                         dataTableOutput('mytable')),
                                tabPanel("Dif. descuento %", plotOutput("plot3"))
                        )
                )
        )
))

服务器.R:

library(shiny)
library(dplyr)
library(ggplot2)
library(scales)
library(gridExtra)


setwd("D:\\RCoursera\\r-s-l")

r.tv <- read.csv("D:\\RCoursera\\R\\r-tv.csv", stringsAsFactors = F)




r.tv.cantidad <- ripley.tv  %>%
        group_by(marca) %>%
        summarise(cantidad = length(marca))




####




tbl.precio.nuevo <- tableGrob(t(prettyNum(summary(r.tv$precio.nuevo), big.mark = ",")))

tbl.precio.antes <- tableGrob(t(prettyNum(summary(r.tv$precio.antes), big.mark = ",")))





####






# Define server logic required to draw a histogram
shinyServer(function(input, output) {

        # Expression that generates a histogram. The expression is
        # wrapped in a call to renderPlot to indicate that:
        #
        #  1) It is "reactive" and therefore should re-execute automatically
        #     when inputs change
        #  2) Its output type is a plot




        output$plot1 <- renderPlot({
                ### Gráficos 


                ### Gráfico 1:






               g <- ggplot(r.tv.cantidad, aes(x=marca, y= cantidad)) + 
                        geom_bar(stat = "identity", width = .7, fill= "lightblue") +
                        labs(title = "",
                             x = "", y = "") +
                        geom_text(aes(label=cantidad), vjust=-0.2, size = 06)  +
                        theme(axis.text.x = element_text(colour="grey10",size=12,hjust=.5,vjust=.5,face="plain"),
                              axis.text.y = element_text(colour="grey10",size=12,,hjust=1,vjust=0,face="plain"),  
                              axis.title.x = element_text(colour="grey40",size=14,angle=0,hjust=.5,vjust=0,face="plain"),
                              axis.title.y = element_text(colour="grey40",size=14,angle=90,hjust=.5,vjust=.5,face="plain"),
                              plot.title = element_text(vjust=2)) +
                       scale_y_continuous(limits = c(0, 50))


               grid.arrange(input$radio, g,
                            nrow=2,
                            as.table=TRUE,
                            heights=c(1,3))





        })






})

我的数据:

r.tv <- structure(list(marca = c("SAMSUNG", "SAMSUNG", "PANASONIC", "HAIER", 
"HAIER", "HAIER", "HAIER", "HAIER", "HAIER", "HAIER", "PANASONIC", 
"HAIER", "SAMSUNG", "SAMSUNG", "SAMSUNG", "SAMSUNG", "SAMSUNG", 
"SAMSUNG", "SAMSUNG", "SAMSUNG", "SAMSUNG", "SAMSUNG", "SAMSUNG", 
"LG", "LG", "LG", "LG", "LG", "LG", "LG", "LG", "LG", "LG", "LG", 
"LG", "LG", "PANASONIC", "PANASONIC", "LG", "LG", "LG", "SONY", 
"SONY", "SONY", "SAMSUNG", "SAMSUNG", "SAMSUNG", "SAMSUNG", "SAMSUNG", 
"SAMSUNG", "SONY", "SONY", "SAMSUNG", "SAMSUNG", "LG", "LG", 
"LG", "SONY", "SAMSUNG", "AOC", "PANASONIC", "SONY", "LG", "AOC", 
"LG", "SONY", "SONY", "SONY", "SONY", "SONY", "SONY", "SHARP", 
"SHARP", "SHARP", "SAMSUNG", "LG", "LG", "LG", "LG", "SAMSUNG", 
"SONY", "LG", "LG", "LG", "LG", "LG", "PANASONIC", "SONY", "SHARP", 
"SONY", "PANASONIC", "SONY", "SAMSUNG", "AOC", "PANASONIC", "SHARP", 
"AOC"), producto = c("SAMSUNG SMART TV LED FHD 48\" 3D 48J6400", 
"SAMSUNG SMART TV LED FHD 40\" 40J5300", "PANASONIC TV LED FULL HD 40'' TC-40CS600L", 
"HAIER TELEVISOR LED LE28F6600 28\"", "HAIER SMART TV 40\" HD LE40K5000N", 
"HAIER TV LED HD 32'' LE32B7000", "HAIER SMART TV  32'' LE32K5000N", 
"HAIER TV LED FHD 55\" - LE55B8000", "HAIER TV LED LE40B8000 FULL HD 40\"", 
"HAIER TV LE24B8000 LED HD 24\" - NEGRO", "PANASONIC TV LED FULL HD 42'' TC-42AS610", 
"HAIER TELEVISOR LED LE50K5000N 50\"", "SAMSUNG SMART TV LED UHD 40\" 40JU6500", 
"SAMSUNG SMART TV ULTRA HD 48'' 48JU6500", "SAMSUNG SMART TV 50JU6500 LED UHD 50\" - NEGRO", 
"SAMSUNG SMART TV ULTRA HD 55'' 3D 55JS9000", "SAMSUNG SMART TV LED UHD 55\" 55JU6500", 
"SAMSUNG SMART TV ULTRA HD 55'' 55JU6700", "SAMSUNG SMART TV CURVO 55JU7500 LED UHD 55\" 3D - NEGRO", 
"SAMSUNG SMART TV ULTRA HD 65'' 3D 65JS9000", "SAMSUNG SMART TV 65JU6500 LED UHD 65\"", 
"SAMSUNG SMART TV ULTRA HD 65'' 65JU7500", "SAMSUNG SMART TV LED UHD 75\" 75JU6500", 
"LG SMART TV WEB OS 40\" FULL HD 40LF6350", "LG SMART TV 3D 42\" FULL HD 42LF6400", 
"LG TV LED 42\" FULL HD CINEMA 3D 42LF6450", "LG TV LED 49\" FULL HD CINEMA 3D 49LF6450", 
"LG SMART TV LF6400 49\" FULL HD 3D", "LG TV 43UF6750 43\" ULTRA HD 4K", 
"LG TV 49\" ULTRA HD 4K 49UF6750", "LG TV LED 49\" ULTRA HD SMART UF6900", 
"LG SMART TV 49UF7700 49\" ULTRA HD 4K", "LG SMART TV 49UF8500 49\" ULTRA HD 4K 3D", 
"LG TV LED 55\" CINEMA 3D SMART TV 55UF7700", "LG SMART TV 65UF7700 65\" ULTRA HD 4K", 
"LG SMART TV 55UF8500 55\" ULTRA HD 4K 3D", "PANASONIC TV LED 55\" ULTRA HD 4K SMART TC-55CX640W", 
"PANASONIC TV LED 50\" ULTRA HD 4K SMART TC-50CX640W", "LG SMART TV 70UF7700 3D ULTRA HD 70\"", 
"LG TV LED CURVO 65\" ULTRA HD 4K CINEMA SMART UG8700", "LG TV LED 60\" FULL HD SMART LF6350", 
"SONY SMART TV KDL-50FA95C 50\" FULL HD 3D", "SONY SMART TV KDL50W805C 50\" FULL HD 3D", 
"SONY TV LED 40\" FULL HD KDL-40R354B", "SAMSUNG SMART TV LED FULL HD 40'' 40J5500", 
"SAMSUNG SMART TV LED FULL HD 50'' 50J5500", "SAMSUNG TV LED HD 32'' 32JH4005", 
"SAMSUNG SMART TV LED FULL HD 50\" 50J5300", "SAMSUNG SMART TV LED 48\" FULL HD 48J5300", 
"SAMSUNG SMART TV FULL HD 40'' 3D 40J6400", "SONY TV LED 32\" HD SMART KDL-32R505C", 
"SONY TV LED 40\" SMART FULL HD KDL-40R555C - NEGRO", "SAMSUNG SMART TV LED FHD 55\" 3D 55J6400", 
"SAMSUNG TV 40JH5005 LED FHD 40\" - NEGRO", "LG TV 43\" FULL HD 43LF5410", 
"LG SMART TV 32LF585B LED HD 32\" - BLANCO", "LG TV LED 49\" FULL HD SMART 49LF5900", 
"SONY SMART TV 65\" FULL HD 3D KDL-65W855C", "SAMSUNG SMART TV LED FHD 48\" UN48J6500", 
"AOC TV LED 40\" FULL HD LE40F1551", "PANASONIC TV LED 32'' SMART HD TC-32AS600L", 
"SONY TV LED 32'' HD KDL-32R304B", "LG TV OLED 55\" SMART 3D FULL HD 55EC9300 PLATEADO", 
"AOC TV LED HD 32'' LE32W454F", "LG TV LED 58\" ULTRA HD SMART 58UF8300", 
"SONY TV LED 55\" FULL HD SMART 3D KDL-55W805C", "SONY TV LED 49\" ULTRA HD 4K XBR-49X835C", 
"SONY TV LED 55\" ULTRA HD 4K XBR-55X855C", "SONY TV LED ULTRA DELGADO 55\" ULTRA HD 4K XBR-65X905C", 
"SONY TV LED 75\" ULTRA HD 4K 3D XBR-75X945C", "SONY TV LED ULTRA DELGADO 55\" ULTRA HD 4K XBR-55X905C", 
"SHARP SMART TV LED 60'' ULTRA HD 4K LC60UE30U", "SHARP SMART TV LED 70'' ULTRA HD 4K LC70UE30U", 
"SHARP SMART TV LED 80'' ULTRA HD 4K LC80UE30U", "SAMSUNG SMART TV LED FULL HD 48'' 48J5500", 
"LG SMART TV CURVO 79UG8800 79\" ULTRA HD 4K 3D", "LG SMART TV 65UF9500 65\" ULTRA HD 4K 3D", 
"LG SMART TV 65UF8500 65\" ULTRA HD 4K 3D", "LG SMART TV 55UF9500 55\" ULTRA HD 4K 3D", 
"SAMSUNG SMART TV LED HD 32\" 32J4300", "SONY TV LED 48\" SMART FULL HD KDL-48R555C - NEGRO", 
"LG SMART TV 55UG8700 55\" ULTRA HD 4K 3D", "LG SMART TV 60UF8500 60\" ULTRA HD 4K 3D", 
"LG SMART TV 55LF6500 55\" FULL HD 3D", "LG TV 32LF550B 32\" HD", 
"LG TV LED 47\" FULL HD 47LB5610", "PANASONIC TV LED FULL HD 50'' TC-50AS600L", 
"SONY TV SMART LED 55\" UHD 3D XBR-55X855B", "SHARP TV LED FULL HD 4K LC70SQ17U 70''", 
"SONY TV LED SMART UHD 79\" XBR-79X905B", "PANASONIC TV LED FULL HD 40'' TC-40A400L", 
"SONY TV LED SMART UHD 70\" XBR-70X855B", "SAMSUNG SMART TV UHD 55'' 3D CURVO 55HU8700", 
"AOC TV FULL HD LE40D3142 40\" - NEGRO", "PANASONIC TELEVISOR LED 42\" TC-42AS650L", 
"SHARP SMART TV LCD FHD 70\" LC70LE660", "AOC TV LED FULL HD 58'' LE58D3140"
), pulgadas = c(48L, 40L, 40L, 28L, 40L, 32L, 32L, 55L, 40L, 
24L, 42L, 50L, 40L, 48L, 50L, 55L, 55L, 55L, 55L, 65L, 65L, 65L, 
75L, 40L, 42L, 42L, 49L, 49L, 43L, 49L, 49L, 49L, 49L, 55L, 65L, 
55L, 55L, 50L, 70L, 65L, 60L, 50L, 50L, 40L, 40L, 50L, 32L, 50L, 
48L, 40L, 32L, 40L, 55L, 40L, 43L, 32L, 49L, 65L, 48L, 40L, 32L, 
32L, 55L, 32L, 58L, 55L, 49L, 55L, 55L, 75L, 55L, 60L, 70L, 80L, 
48L, 79L, 65L, 65L, 55L, 32L, 48L, 55L, 60L, 55L, 32L, 47L, 50L, 
55L, 70L, 79L, 40L, 70L, 55L, 40L, 42L, 70L, 58L), precio.antes = c(2799L, 
1799L, 1699L, 599L, 1299L, 699L, 999L, 1999L, 999L, 499L, 1899L, 
1799L, 2499L, 3999L, 3699L, 10999L, 4299L, 5499L, 6999L, 14999L, 
8999L, 9999L, 14599L, 1999L, 2299L, 2299L, 2899L, 2999L, 2299L, 
23992L, 3599L, 3799L, 4799L, 4999L, 8499L, 5999L, 4999L, 3999L, 
11999L, 10999L, 4399L, 4499L, 3799L, 1399L, 2299L, 2799L, 999L, 
2199L, 2299L, 2299L, 1299L, 1699L, 3499L, 1399L, 1549L, 1299L, 
2399L, 6499L, 2999L, 999L, 1249L, 999L, 14999L, 799L, 5999L, 
4499L, 4999L, 6499L, 12999L, 24999L, 8999L, 5999L, 7599L, 14999L, 
2499L, 29999L, 13999L, 9999L, 9699L, 1299L, 2399L, 6999L, 7999L, 
3699L, 999L, 1899L, 2999L, 7999L, 8499L, 24999L, 1399L, 13999L, 
8499L, 999L, 2599L, 5799L, 2399L), precio.nuevo = c(2299, 1399, 
1299, 549, 1099, 629, 799, 1699, 849, 439, 1499, 1549, 1759.2, 
2099.3, 2309.3, 7699.3, 2799.3, 3639.3, 4899.3, 10499.3, 5109.3, 
6999.3, 10219.3, 1399, 1599, 1599, 2199, 2199, 1299, 23992, 2299, 
2299, 2899, 2999, 5999, 3899, 4999, 3999, 8999, 6999, 4099, 3999, 
3499, 1299, 1799, 2399, 799, 2199, 1799, 1999, 1199, 1599, 2999, 
1199, 1399, 1099, 1999, 5999, 2799, 999, 1199, 949, 7999, 799, 
5299, 4299, 3999, 5999, 11999, 23999, 7999, 5699, 7599, 14499, 
2399, 29999, 11999, 8999, 7499, 1099, 2199, 6599, 7099, 3599, 
899, 1599, 2199, 4999, 6499, 19999, 1399, 9999, 5999, 999, 2599, 
5699, 2399), dif.precios = c(500, 400, 400, 50, 200, 70, 200, 
300, 150, 60, 400, 250, 739.8, 1899.7, 1389.7, 3299.7, 1499.7, 
1859.7, 2099.7, 4499.7, 3889.7, 2999.7, 4379.7, 600, 700, 700, 
700, 800, 1000, 0, 1300, 1500, 1900, 2000, 2500, 2100, 0, 0, 
3000, 4000, 300, 500, 300, 100, 500, 400, 200, 0, 500, 300, 100, 
100, 500, 200, 150, 200, 400, 500, 200, 0, 50, 50, 7000, 0, 700, 
200, 1000, 500, 1000, 1000, 1000, 300, 0, 500, 100, 0, 2000, 
1000, 2200, 200, 200, 400, 900, 100, 100, 300, 800, 3000, 2000, 
5000, 0, 4000, 2500, 0, 0, 100, 0), dif.porcentual = c(17.86, 
22.23, 23.54, 8.35, 15.4, 10.01, 20.02, 15.01, 15.02, 12.02, 
21.06, 13.9, 29.6, 47.5, 37.57, 30, 34.88, 33.82, 30, 30, 43.22, 
30, 30, 30.02, 30.45, 30.45, 24.15, 26.68, 43.5, 0, 36.12, 39.48, 
39.59, 40.01, 29.42, 35.01, 0, 0, 25, 36.37, 6.82, 11.11, 7.9, 
7.15, 21.75, 14.29, 20.02, 0, 21.75, 13.05, 7.7, 5.89, 14.29, 
14.3, 9.68, 15.4, 16.67, 7.69, 6.67, 0, 4, 5.01, 46.67, 0, 11.67, 
4.45, 20, 7.69, 7.69, 4, 11.11, 5, 0, 3.33, 4, 0, 14.29, 10, 
22.68, 15.4, 8.34, 5.72, 11.25, 2.7, 10.01, 15.8, 26.68, 37.5, 
23.53, 20, 0, 28.57, 29.42, 0, 0, 1.72, 0), rangos = c("S/.1500 - S/.2500", 
"S/.500 - S/.1500", "S/.500 - S/.1500", "S/.500 - S/.1500", "S/.500 - S/.1500", 
"S/.500 - S/.1500", "S/.500 - S/.1500", "S/.1500 - S/.2500", 
"S/.500 - S/.1500", "< S/.500", "S/.500 - S/.1500", "S/.1500 - S/.2500", 
"S/.1500 - S/.2500", "S/.1500 - S/.2500", "S/.1500 - S/.2500", 
"> S/.4,500", "S/.2500 - S/.3500", "S/.3500 - S/.4500", "> S/.4,500", 
"> S/.4,500", "> S/.4,500", "> S/.4,500", "> S/.4,500", "S/.500 - S/.1500", 
"S/.1500 - S/.2500", "S/.1500 - S/.2500", "S/.1500 - S/.2500", 
"S/.1500 - S/.2500", "S/.500 - S/.1500", "> S/.4,500", "S/.1500 - S/.2500", 
"S/.1500 - S/.2500", "S/.2500 - S/.3500", "S/.2500 - S/.3500", 
"> S/.4,500", "S/.3500 - S/.4500", "> S/.4,500", "S/.3500 - S/.4500", 
"> S/.4,500", "> S/.4,500", "S/.3500 - S/.4500", "S/.3500 - S/.4500", 
"S/.2500 - S/.3500", "S/.500 - S/.1500", "S/.1500 - S/.2500", 
"S/.1500 - S/.2500", "S/.500 - S/.1500", "S/.1500 - S/.2500", 
"S/.1500 - S/.2500", "S/.1500 - S/.2500", "S/.500 - S/.1500", 
"S/.1500 - S/.2500", "S/.2500 - S/.3500", "S/.500 - S/.1500", 
"S/.500 - S/.1500", "S/.500 - S/.1500", "S/.1500 - S/.2500", 
"> S/.4,500", "S/.2500 - S/.3500", "S/.500 - S/.1500", "S/.500 - S/.1500", 
"S/.500 - S/.1500", "> S/.4,500", "S/.500 - S/.1500", "> S/.4,500", 
"S/.3500 - S/.4500", "S/.3500 - S/.4500", "> S/.4,500", "> S/.4,500", 
"> S/.4,500", "> S/.4,500", "> S/.4,500", "> S/.4,500", "> S/.4,500", 
"S/.1500 - S/.2500", "> S/.4,500", "> S/.4,500", "> S/.4,500", 
"> S/.4,500", "S/.500 - S/.1500", "S/.1500 - S/.2500", "> S/.4,500", 
"> S/.4,500", "S/.3500 - S/.4500", "S/.500 - S/.1500", "S/.1500 - S/.2500", 
"S/.1500 - S/.2500", "> S/.4,500", "> S/.4,500", "> S/.4,500", 
"S/.500 - S/.1500", "> S/.4,500", "> S/.4,500", "S/.500 - S/.1500", 
"S/.2500 - S/.3500", "> S/.4,500", "S/.1500 - S/.2500")), .Names = c("marca", 
"producto", "pulgadas", "precio.antes", "precio.nuevo", "dif.precios", 
"dif.porcentual", "rangos"), class = "data.frame", row.names = c(NA, 
-97L))      

最佳答案

我测试了它,这有效:

# add after g
gg <- ggplotGrob(g)
if(input$radio=="tbl.precio.nuevo"){
    g1 <- gtable::gtable_add_grob(gg, grobs = tbl.precio.nuevo 
                              ,t=6, l=6, b=5, r=2
                              )
} else {
    g1 <- gtable::gtable_add_grob(gg, grobs = tbl.precio.antes 
                                  ,t=6, l=6, b=5, r=2
    )
}
grid::grid.draw(g1)

所有文件在:gitlab

关于R:Shiny - 无功输出在 grid.arrange(from gridExtra) 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33040827/

相关文章:

r - 按行填充缺失值(右/左)

r - 在 R 中创建不同维度的矩阵向量

r - ggplot2:使用grid.arrange()作为do.call()的参数定义图版面

python - rpy内存泄漏

从 numericInput() 中删除向上/向下箭头 R Shiny

r - 与 Shiny 的仪表板情节集成

r - Flexdashboards 和 Leaflet 以及使用 Highcharts 进行标记点击

r - 如何将颜色更改为 textGrob 项目

r - 在排列格罗布之后拆分或删除图形

r - 同一数据集中定义行之间的 T 检验