【问题标题】:Selecting specific csv files based on user input in shiny web appliction在闪亮的 Web 应用程序中根据用户输入选择特定的 csv 文件
【发布时间】:2018-06-18 09:24:44
【问题描述】:

我有一个书名下拉列表,用户可以在其中选择所需的书名。它在 ui.R 中的代码看起来像

selectInput(‘x’, label = ‘Choose the book’, choices = c(‘book1_name’,
‘book2_name’,
‘book3_name’),
selected = ‘book1_name’)

在我的应用程序文件夹中,每本书都有两个 csv 文件。我想根据下拉菜单中的用户选择读取两个变量中对应的两个csv文件。比如:

if(input$x==‘book1_name’){
data1<- read.csv(‘1_1.csv’)
data2 <- read.csv(‘1_2.csv’)
}
else if{
  (input$x==‘book2_name’){
    data1<- read.csv(‘2_1.csv’)
    data2 <- read.csv(‘2_2.csv’)

}

然后使用 data1 和 data2 进行进一步的计算。 我在 server.R 中的代码应该是什么 我尝试使用 eventReactive 但无法获得正确的脚本。

【问题讨论】:

    标签: shiny shiny-server shinydashboard shinyjs


    【解决方案1】:

    您没有提供可重现的示例,因此我使用了 mtcarsiris 数据集。您可以在全局环境中读取自己的数据集。为了示例,我使用renderPlot 函数向您展示它的工作原理。这里有两个建议:

    选项 1:

    library(shiny)
    library(tidyr)
    
    ui = pageWithSidebar(
      headerPanel('testing'),
      sidebarPanel(
        selectInput('x', label = 'Choose the book', choices = c('book1_name', 'book2_name'), selected = 'book1_name')
      ),
      mainPanel(
        plotOutput('plot1')
      )
    )
    
      server = function(input, output) {
    
    
        output$plot1 <- renderPlot({
    
    
          if (input$x == 'book1_name') {
            data1 <- iris
            data2 <- mtcars
            rownames(data2) <- NULL
    
            data1a <- data1[, 1:2]
            data2a <- data2[, 1:2]
    
            par(mfrow = c(1, 2)) 
            plot(data1a) #
            plot(data2a)  #
          } 
    
          if (input$x == 'book2_name') {
            data1 <- mtcars
            data2 <- iris
           # rownames(data2) <- NULL
    
            data1a <- data1[, 1:2]
            data2a <- data2[, 1:2]
    
            par(mfrow = c(1, 2)) 
            plot(data1a) #
            plot(data2a)  #
          } 
    
    
        })
    
      }
    
      shinyApp(ui = ui, server = server)
    

    使用选项 1,您可以在渲染函数中选择数据集(可能是另一个计算)。

    选项 2:

    library(shiny)
    
    ui = pageWithSidebar(
      headerPanel('testing'),
      sidebarPanel(
        selectInput('x', label = 'Choose the book', choices = c('book1_name', 'book2_name'), selected = 'book1_name')
      ),
      mainPanel(
        plotOutput('plot1')
      )
    )
    
    server = function(input, output) {
    
    
      data1 <- reactive({
        if (input$x == 'book1_name') {
          data1 <- iris
        } else {
          data1 <- mtcars
        }
      })
    
      data2 <- reactive({
        if (input$x == 'book1_name') {
          data2 <- mtcars
        } else {
          data1 <- iris
        }
      })
    
    
      output$plot1 <- renderPlot({
    
        data1 <- data1()
        data2 <- data2()
    
          data1a <- data1[, 1:2]
          data2a <- data2[, 1:2]
    
          par(mfrow = c(1, 2)) 
          plot(data1a) #
          plot(data2a)  #
    
    
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    使用选项 2,您可以选择渲染函数之外的数据集。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 1970-01-01
      • 2021-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      • 2021-09-23
      • 2017-05-11
      相关资源
      最近更新 更多