【问题标题】:Using an input from Shiny Widget in Reshape Cast()在 Reshape Cast() 中使用来自 Shiny Widget 的输入
【发布时间】:2023-04-09 09:56:01
【问题描述】:

我试图在我闪亮的网络应用程序中创建一种数据透视表,用户可以在其中选择他们想要相互转换的变量。我发现的问题是,当我尝试将 cast 与“input$columns”一起使用时,我得到以下信息:错误:转换公式包含在熔融数据中找不到的变量:input$columns。 input$columns 应该是在我的数据集中找到的三个列名之一,所以我不确定问题可能是什么。我附上了代码的简化版本,试图找出问题所在。

用户界面

 library(shiny)

    shinyUI(fluidPage(

     titlePanel("Reactivity"),


     sidebarLayout(
       sidebarPanel(
       textInput("caption", "Caption:", "Column Name:"),

       selectInput("columns", "Choose a dataset:", 
                  choices = c("Publisher", "Region", "Representative")),

       numericInput("obs", "Number of observations to view:", 10)
      ),


     mainPanel(
      h3(textOutput("caption")), 

      verbatimTextOutput("summary"), 

      dataTableOutput('view')
     )
    )
    ))

服务器

library(shiny)
library(datasets)

shinyServer(function(input, output) {

  datasetInput <- reactive({
    switch(input$columns,
           "Publisher" = Publisher,
           "Region" = Region,
           "Representative" = Representative)
  })

  output$caption <- renderText({
    input$caption
  })

  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  output$view <- renderDataTable({
    test <- input$columns
    m.book.sales <- melt(book.sales)
    cast(m.book.sales, test ~ variable, sum)
  })
})

和数据集

> head(book.sales)
  Representative Region      Month Publisher    Subject Sales Margin Quantity
1            Raj      S 1997-01-01      SAGE Management 135.0  63.45        9
2            Raj      S 1997-01-01 Routledge Statistics 120.0  48.00        6
3            Raj      S 1997-01-01   Penguin  Economics  54.0  22.68        4
4            Raj      S 1997-01-01 Routledge    Fiction 234.0 128.70        9
5           Soni      S 1997-01-01   Penguin   Politics  54.0  22.68        4
6           Soni      S 1997-01-01      SAGE   Politics 185.4 100.12       12

【问题讨论】:

    标签: r shiny reshape


    【解决方案1】:

    您需要传递一个有效的公式。 test 是一个变量,不能在您的公式调用中使用。您可以使用paste 构建一个论坛并使用as.formula 进行转换:

     output$view <- renderDataTable({
       test <- input$columns
       m.book.sales <- melt(book.sales)
       myFormula <- as.formula(paste0(test, " ~ variable"))
       cast(m.book.sales, myFormula, sum)
     })
    

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 1970-01-01
      • 2018-11-03
      • 1970-01-01
      • 2014-10-01
      • 2017-04-10
      • 2015-09-08
      • 1970-01-01
      • 2014-03-01
      相关资源
      最近更新 更多