【问题标题】:Changing text and plots with selected input in R shiny在 R Shiny 中使用选定的输入更改文本和绘图
【发布时间】:2021-12-23 04:35:00
【问题描述】:

我想根据为绘图选择的参数更改框中的文本。让文字和情节同时变化

所以在那个工作示例中:

如果下拉菜单选择 Sepal.width,我希望有 textoutput: "bliblablub"。

如果下拉菜单选择 Sepal.Length,我希望有 textoutput: "some text"。

如果下拉菜单选择 Petal.Length,我希望有文本输出:“完成不同的东西”。

如果下拉菜单选择 Petal.Width,我希望有 textoutput: "new stuff"。

正如我在原始数据框中的 15 个参数中一样,我也希望获得处理每个参数的文本输入的提示。理想情况下,我会从第二个数据框中获取文本(具有相同的列名?)。

到目前为止,我还不明白如何组合选择输入、文本框和绘图框。

非常感谢您的帮助!

library(shiny)
library(shinydashboard)
#data
dat <-iris

#producing parameters for select input
param <- colnames(dat)
param <- param[1:4]


#ui
Header<- dashboardHeader(title = "Iris")

Sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Parametro", tabName = "parametro", icon = icon("car"))
  )
)

Body <- dashboardBody(
  tabItems(
    tabItem(tabName = "parametro",
            column(width = 12,
                   box(
                     plotOutput("boxplot_species")),
                   fluidRow(
                     box(
                       selectInput("parametro", "Parametros:",
                                   param), width = 6),
                     box(
                       textOutput("description"))
                   )    
            ))
  ))

ui <- dashboardPage(Header,
                    Sidebar,
                    Body)

server <- function(input, output) {
  output$description <- renderText({
    "bliblablub"
  })
  output$boxplot_species <- renderPlot({
    ggplot(dat) +
      aes(x = Species, fill = Species) +
      aes_string(y = input$parametro) +
      geom_boxplot()
  })
}
shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    您可以使用switch 根据下拉选择更新文本并将aes_string 替换为.data,因为aes_string 已弃用。

    library(shiny)
    library(shinydashboard)
    library(ggplot2)
    
    Header<- dashboardHeader(title = "Iris")
    
    Sidebar <- dashboardSidebar(
      sidebarMenu(
        menuItem("Parametro", tabName = "parametro", icon = icon("car"))
      )
    )
    
    Body <- dashboardBody(
      tabItems(
        tabItem(tabName = "parametro",
                column(width = 12,
                       box(
                         plotOutput("boxplot_species")),
                       fluidRow(
                         box(
                           selectInput("parametro", "Parametros:",
                                       param), width = 6),
                         box(
                           textOutput("description"))
                       )    
                ))
      ))
    
    ui <- dashboardPage(Header,
                        Sidebar,
                        Body)
    
    server <- function(input, output) {
      output$description <- renderText({
        text <- switch(input$parametro,
                Sepal.Length = 'x', 
                Sepal.Width = 'y',
                Petal.Length = 'other',
                Petal.Width  = 'more'
        )
        paste(text, "is blabla")
      })
      output$boxplot_species <- renderPlot({
        ggplot(dat) +
          aes(x = Species, y = .data[[input$parametro]], fill = Species) +
          geom_boxplot()
      })
    }
    shinyApp(ui, server)
    


    为了保持代码干净,您还可以使用Species 和相应的值创建一个查找数据框。

    lookup_df <- data.frame(Species = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"), 
                            value = c('x', 'y', 'other', 'more'))
    

    然后将description代码改为-

     output$description <- renderText({
        lookup_df$value[match(input$parametro, lookup_df$Species)]
      })
    

    【讨论】:

    • 谢谢!但后来我的问题有点误导。我希望文本完全不同 - 所以文本输入需要存储在其他地方。所以例如对于花瓣长度:“x”,花瓣宽度:“y”,萼片长度:“Z”和萼片宽度:“其他”
    • @Annika 检查更新的答案。
    • 好的,不,我想我必须更新问题......忘记“是blabla”。每个参数的文本完全不同。 Sepal.Width 的文本输出可以是“它是花的一部分”,而 Sepal.Length 的文本输出可以是“圣诞快乐”。它只是随着下拉菜单而变化。
    • @Annika 这就是我的回答。如果您不想要"is blabla",只需将其删除。将 paste(text, "is blabla") 替换为 text。在switch 中,您可以根据需要为每个Species 更改文本。我用过xyothermore,你可以用bliblablub"some text"等替换它。你试过了吗?
    • 你是对的!非常感谢!
    猜你喜欢
    • 2019-07-27
    • 2022-01-15
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 2016-08-14
    • 2019-05-05
    • 2021-11-14
    相关资源
    最近更新 更多