简体   繁体   English

如何在 Shinydashboard 中创建信息框作为操作按钮?

[英]How to create Infobox as actionbutton in Shinydashboard?

I have Shinydashboard which basically take input file from user and displays 2 plots at top and datatable at the bottom of the dashboard.我有Shinydashboard ,它基本上从用户那里获取输入文件,并在顶部显示 2 个图,在仪表板底部显示数据表。 Next, I added infobox at the top of the Box1 so that when users clicks on infobox, the plot2 gets updated after user clicks on infobox with new plot, otherwise dashboard displays default plot.接下来,我在 Box1 的顶部添加了信息框,以便当用户单击信息框时,在用户单击信息框后使用新的 plot 更新 plot2,否则仪表板显示默认的 plot。 Below is reproducible example.以下是可重现的示例。 I am following gogol comment/code here .我在这里关注 gogol 评论/代码。 However, I am not sure how to proceed with infobox coding for server side as the question was related to Valuebox?但是,我不确定如何进行服务器端的信息框编码,因为问题与 Valuebox 相关?

Overall, ask is If user clicks on "Infobox" then plot 2 (Box2 in this case) will get updated with other plot (ex. hp vs weight) otherwise the plot2 will be default.总体而言,如果用户单击“信息框”,则 plot 2(在本例中为 Box2)将使用其他 plot(例如 hp 与重量)进行更新,否则 plot2 将是默认值。 In this case, it will be Pressure vs temperature plot.在这种情况下,它将是压力与温度 plot。 Also, If the plot2 is updated then when user clicks on plot2 the updated plot should get displayed in Modal dialog otherwise the default plot should get displayed in modal dialog.此外,如果 plot2 已更新,则当用户单击 plot2 时,更新后的 plot 应显示在模态对话框中,否则默认 plot 应显示在模态对话框中。

Thanks in advance for your time and efforts!提前感谢您的时间和努力!

library(shiny)
library(shinydashboard)
library(dplyr)
library(ggplot2)
library(shinyBS)
library(DT)

ui<-dashboardPage(
  dashboardHeader(title="Missing",titleWidth = 230),
  dashboardSidebar(
    fileInput("file1", "Upload CSV File below",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv")
    )),
  dashboardBody(
    fluidRow(
      tags$head(tags$style(HTML('.info-box {min-height: 45px;} .info-box-icon {height: 45px; line-height: 45px;} .info-box-content {padding-top: 0px; padding-bottom: 0px;}'))),
      infoBox(" ", fill = TRUE,width = 7,value = tags$p("Infobox", style = "font-size: 100%;")),
      infoBoxOutput("Infobox"),
      div(id="popme1", box(plotOutput("Plot1"),collapsible = TRUE,title="Plot 1",solidHeader = TRUE,status = "primary")),
      bsModal("modalExample1", "Plot1", "popme1", size = "large", plotOutput("Plot11")),
      div(id="popme2", box(plotOutput("Plot2"),collapsible=TRUE,title="Plot 2",solidHeader = TRUE,status = "primary")),
      bsModal("modalExample2", "Plot2", "popme2", size = "large", plotOutput("Plot22")),
      div(id="popme3", fluidRow(column(width=8,box(DTOutput("Missing_datatable"), width = NULL,collapsible = TRUE)) )),
      bsModal("modalExample3", "Data Table", "popme3", size = "large", DTOutput("Missing_datatable2"))
    )
  )
)

server<- function(input, output,session) {
  
  output$Plot1 <- renderPlot({
    plot(cars)
  })
  output$Plot11 <- renderPlot({
    plot(cars)
  })
  output$Plot22 <- renderPlot({ plot(pressure)})
  
  output$Plot2 <- renderPlot({ plot(pressure) })
  
  output$Missing_datatable <- renderDT({iris[1:7,]})
  output$Missing_datatable2 <- renderDT({iris[1:7,]})
}

# Run the application 
shinyApp(ui = ui, server = server)

We can use actionLink and wrap it around infoBox .我们可以使用actionLink并将其包裹在infoBox周围。 This will generate an input in the example below named input$info_clk which starts at 0 and gos up with each click.这将在下面的示例中生成一个名为input$info_clk的输入,它从0开始并随着每次点击而上升。 To turn this into an control-flow we use the remainder of the devision with 2 in an if statement if(input$info_clk %% 2) :为了将其转换为控制流,我们在 if 语句if(input$info_clk %% 2)中使用了2的其余部分:

library(shiny)
library(shinydashboard)
library(dplyr)
library(ggplot2)
library(shinyBS)
library(DT)

ui<-dashboardPage(
  dashboardHeader(title="Missing",titleWidth = 230),
  dashboardSidebar(
    fileInput("file1", "Upload CSV File below",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
                ".csv")
    )),
  dashboardBody(
    fluidRow(
      
      tags$head(
        tags$style(HTML('.info-box {min-height: 45px;} .info-box-icon {height: 45px; line-height: 45px;} .info-box-content {padding-top: 0px; padding-bottom: 0px;}')
                   )
        ),
      
      actionLink("info_clk",
        infoBox(" ", fill = TRUE, width = 7, value = tags$p("Infobox", style = "font-size: 100%;"))
        ),
      
      # infoBoxOutput("Infobox"),
      
      div(id="popme1", box(plotOutput("Plot1"),collapsible = TRUE,title="Plot 1",solidHeader = TRUE,status = "primary")),
      bsModal("modalExample1", "Plot1", "popme1", size = "large", plotOutput("Plot11")),
      
      div(id="popme2", box(plotOutput("Plot2"),collapsible=TRUE,title="Plot 2",solidHeader = TRUE,status = "primary")),
      bsModal("modalExample2", "Plot2", "popme2", size = "large", plotOutput("Plot22")),
      
      div(id="popme3", fluidRow(column(width=8,box(DTOutput("Missing_datatable"), width = NULL,collapsible = TRUE)) )),
      bsModal("modalExample3", "Data Table", "popme3", size = "large", DTOutput("Missing_datatable2"))
      
    )
  )
)

server<- function(input, output,session) {
  
  output$Plot1 <- output$Plot11 <- renderPlot({
    plot(cars)
  })
  
  output$Plot2 <- output$Plot22 <- renderPlot({
    
    if (input$info_clk %% 2L) {
      plot(mtcars$wt, mtcars$hp)
    } else {
      plot(pressure)
    }
    })
  
    output$Missing_datatable <- renderDT({iris[1:7,]})
  output$Missing_datatable2 <- renderDT({iris[1:7,]})
}

# Run the application 
shinyApp(ui = ui, server = server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM